Last active
November 3, 2024 03:35
-
-
Save kb10uy/5ae301e8fd5075906ca2526034238802 to your computer and use it in GitHub Desktop.
GitHub の PR 説明文に !automerge:2024-11-01 のようなディレクティブが記述されていたらその日時以降にマージするやつ
This file contains 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 | |
# GitHub の PR 説明文に !automerge:2024-11-01 のようなディレクティブが記述されていたらその日時以降にマージするやつ | |
# | |
# 必須環境: | |
# * ログイン済みの GitHub CLI | |
# * Ripgrep | |
# | |
# 使い方: | |
# 1. 自動マージさせたい PR のコメントに !automerge:XXXXXXXX と書いておく。XXXXXXXX は RFC3339 形式なら可 | |
# 2. ./automerge.bash user/repository を定期的に実行すればいい感じに自動でマージされる | |
# | |
# License: CC0-1.0 | |
if (( $# < 1 )); then | |
echo 'Usage: automerge.bash USER/REPOSITORY' | |
exit 1 | |
fi | |
readonly GH_PR_COMMAND="gh pr -R $1" | |
readonly NOW_DATETIME=$(date -Is) | |
readonly NOW_UNIXTIME=$(date -d "${NOW_DATETIME}" '+%s') | |
echo "Base datetime: ${NOW_DATETIME} (unixtime: ${NOW_UNIXTIME})" | |
echo | |
# 全 PR の情報を取得 | |
readonly PR_JSON_PATH=$(mktemp) | |
${GH_PR_COMMAND} list --json 'number,title,body' | jq 'map({(.number | tostring): .}) | add' > "${PR_JSON_PATH}" | |
# 各 PR 番号ごとに !automerge の内容をチェック | |
for pr_number in $(jq -r 'keys | .[]' "${PR_JSON_PATH}"); do | |
title=$(jq -r ".\"${pr_number}\".title" "${PR_JSON_PATH}") | |
echo "Checking PR #${pr_number}: ${title}" | |
# !automerge:YYYY-MM-DD が指定されていなければスキップ | |
due_datetime=$(jq -r ".\"${pr_number}\".body" "${PR_JSON_PATH}" | rg -r '$1' '^\s*!automerge:\s*([0-9+\-:T]+)\s*$') | |
if [[ -z "${due_datetime}" ]]; then | |
echo 'Auto-merge date not set.' | |
continue | |
fi | |
due_epoch=$(date -d "${due_datetime}" '+%s') | |
echo "Auto-merge date: ${due_datetime} (unixtime: ${due_epoch})" | |
if (( "${due_epoch}" < "${NOW_UNIXTIME}" )); then | |
echo 'This PR is now to be merged!' | |
${GH_PR_COMMAND} merge -m "${pr_number}" | |
else | |
echo 'This PR will be kept...' | |
fi | |
echo | |
done | |
rm "${PR_JSON_PATH}" | |
echo 'Done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment