Created
October 23, 2024 12:56
-
-
Save Hiroshiba/086e5515f4e2896f58b77df19236238f to your computer and use it in GitHub Desktop.
プルリクエストに対してプルリクエストを投げるコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# HEADから過去5回分のコミットを探索し、カウンタープルリクエスト対象のブランチを決定 | |
attempts=5 | |
branches="" | |
for ((i = 0; i < attempts; i++)); do | |
commit_ref="HEAD" | |
for ((j = 0; j < i; j++)); do | |
commit_ref+="^" | |
done | |
branches=$(git branch -r --contains "$commit_ref" | sed 's/\* //g' | tr -d ' ' | grep -vE '^(origin|upstream)/' | grep -vE '^pr/.+/[0-9]+$' || true) | |
if [[ -n "$branches" ]]; then | |
break | |
fi | |
done | |
if [[ -z "$branches" ]]; then | |
echo "Error: 対象のブランチが見つかりません。" | |
exit 1 | |
fi | |
# ブランチの候補が1つだけならそのブランチを使用 | |
if [[ $(echo "$branches" | wc -l) -eq 1 ]]; then | |
pr_branch="$branches" | |
pr_hash=$(git rev-parse "$pr_branch") | |
else | |
# 複数のブランチがある場合は選択 | |
echo "現在のコミットに関連するブランチがあります。どのブランチを使用しますか?" | |
select pr_branch in $branches; do | |
if [[ -n "$pr_branch" ]]; then | |
break | |
else | |
echo "無効な選択です。もう一度選んでください。" | |
fi | |
done | |
fi | |
if [[ "$pr_branch" =~ ^([^/]+)/(.+)$ ]]; then | |
pr_owner="${BASH_REMATCH[1]}" | |
pr_branch="${BASH_REMATCH[2]}" | |
else | |
echo "Error: ブランチ名の形式が認識できません。期待される形式は pr_owner/pr_branch です。" | |
exit 1 | |
fi | |
# リモートURLから自分の名前とリポジトリ名を抽出 | |
git_remote_url=$(git config --get remote.origin.url) | |
if [[ -z "$git_remote_url" ]]; then | |
echo "Error: Gitリポジトリが見つかりません。" | |
exit 1 | |
fi | |
if [[ "$git_remote_url" =~ ^[email protected]:(.+)/(.+)\.git$ ]]; then | |
my_owner="${BASH_REMATCH[1]}" | |
my_repo="${BASH_REMATCH[2]}" | |
elif [[ "$git_remote_url" =~ ^https://github.com/(.+)/(.+)\.git$ ]]; then | |
my_owner="${BASH_REMATCH[1]}" | |
my_repo="${BASH_REMATCH[2]}" | |
else | |
echo "Error: GitリポジトリのURL形式が認識できません。" | |
exit 1 | |
fi | |
# プルリクエスト用の新しいブランチを作成 | |
my_branch="hiho-counter-pr-$(git rev-parse --short=8 HEAD)" | |
if ! git show-ref --verify --quiet "refs/heads/$my_branch"; then | |
git checkout -b "$my_branch" | |
git push -u origin "$my_branch" | |
fi | |
# 文面を整える | |
upstream_url=$(git config --get remote.upstream.url) | |
if [[ "$upstream_url" =~ ^[email protected]:(.+)/(.+)\.git$ ]]; then | |
upstream_owner="${BASH_REMATCH[1]}" | |
elif [[ "$upstream_url" =~ ^https://github.com/(.+)/(.+)\.git$ ]]; then | |
upstream_owner="${BASH_REMATCH[1]}" | |
else | |
echo "Error: upstreamリポジトリのURL形式が認識できません。" | |
exit 1 | |
fi | |
pr_info=$(gh pr list --repo "$upstream_owner/$my_repo" --search "$pr_hash" --json title,number) | |
pr_number=$(echo "$pr_info" | jq -r '.[0].number') | |
pr_link="https://github.com/$upstream_owner/$my_repo/pull/$pr_number" | |
pr_title="#${pr_number} の変更提案プルリクエスト" | |
pr_body="- ${pr_link} | |
の変更提案プルリクです! | |
mergeしていただければ自動的に元のプルリクに反映されるはずです。 | |
もちろんmergeせず自由に変更いただいても問題ないです!" | |
# 意思の確認 | |
echo "作成するプルリクエストの詳細:" | |
echo "リポジトリ: $pr_owner/$my_repo" | |
echo "ブランチ: $pr_branch" | |
echo "タイトル: $pr_title" | |
echo -e "本文: \n$pr_body" | |
echo "" | |
echo "プルリクエストを作成してもよろしいですか? (y/n)" | |
read -r user_input | |
if [[ "$user_input" != "y" ]]; then | |
echo "プルリクエストの作成をキャンセルしました。" | |
exit 0 | |
fi | |
# プルリクエストを作成 | |
set +e | |
gh pr create --repo "$pr_owner/$my_repo" --head "$my_owner:$my_repo:$my_branch" --base "$pr_branch" --title "$pr_title" --body "$pr_body" | |
if [[ $? -ne 0 ]]; then | |
echo "Error: プルリクエストの作成に失敗しました。" | |
exit 1 | |
fi | |
set -e | |
echo "プルリクエストが正常に作成されました。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment