mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-01 07:17:56 +00:00
fix(update/open_pr): scope dedup to changed files
The previous tree-hash dedup compared whole-commit trees, so unrelated drift on main (e.g. a manual edit to any roles/* file) caused the same auto-update delta to appear "new" each daily cron and a fresh PR to open. Now: fingerprint only the files that this run actually committed (git diff --name-only HEAD~1 HEAD), both locally and on each candidate open PR with the same UPDATE_BRANCH_PREFIX. If any open PR carries the identical content for those files, skip push + PR creation entirely. Branch name now carries the fingerprint's short hash so parallel PRs of the same class can coexist (the close-superseded loop is dropped). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
54835d6430
commit
f286ce54dd
1 file changed
+29
-26
@@ -9,7 +9,6 @@ set -euo pipefail
|
||||
|
||||
UPDATE_BASE_BRANCH="${UPDATE_BASE_BRANCH:-master}"
|
||||
UPDATE_BRANCH_SUFFIX="${UPDATE_BRANCH_SUFFIX:-$(date +%Y%m%d)}"
|
||||
BRANCH="${UPDATE_BRANCH_PREFIX}-${UPDATE_BRANCH_SUFFIX}"
|
||||
|
||||
if ! command -v gh >/dev/null 2>&1; then
|
||||
echo "ERROR: gh CLI not found." >&2
|
||||
@@ -28,7 +27,8 @@ else
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
fi
|
||||
git checkout -B "${BRANCH}"
|
||||
|
||||
git checkout --detach HEAD
|
||||
|
||||
if (($# > 0)); then
|
||||
git add -- "$@"
|
||||
@@ -43,8 +43,25 @@ fi
|
||||
|
||||
git commit -m "${UPDATE_COMMIT_MESSAGE}"
|
||||
|
||||
LOCAL_TREE="$(git log -1 --format=%T HEAD)"
|
||||
echo "Local tree: ${LOCAL_TREE}"
|
||||
fingerprint_ref() {
|
||||
local ref="$1"
|
||||
shift
|
||||
if (($# == 0)); then
|
||||
git rev-parse "${ref}^{tree}"
|
||||
return
|
||||
fi
|
||||
local path blob
|
||||
{
|
||||
for path in "$@"; do
|
||||
blob="$(git rev-parse "${ref}:${path}" 2>/dev/null || echo MISSING)"
|
||||
printf '%s\t%s\n' "${blob}" "${path}"
|
||||
done
|
||||
} | sort | git hash-object --stdin
|
||||
}
|
||||
|
||||
mapfile -t CHANGED_FILES < <(git diff --name-only HEAD~1 HEAD)
|
||||
LOCAL_FP="$(fingerprint_ref HEAD "${CHANGED_FILES[@]}")"
|
||||
echo "Local fingerprint: ${LOCAL_FP} over ${#CHANGED_FILES[@]} file(s)"
|
||||
|
||||
mapfile -t OPEN_PRS < <(
|
||||
gh pr list \
|
||||
@@ -53,7 +70,7 @@ mapfile -t OPEN_PRS < <(
|
||||
--base "${UPDATE_BASE_BRANCH}" \
|
||||
--limit 100 \
|
||||
--json number,headRefName \
|
||||
--jq ".[] | select(.headRefName != \"${BRANCH}\") | \"\(.number)\t\(.headRefName)\""
|
||||
--jq ".[] | select(.headRefName | startswith(\"${UPDATE_BRANCH_PREFIX}-\")) | \"\(.number)\t\(.headRefName)\""
|
||||
)
|
||||
|
||||
DUPLICATE_PR=""
|
||||
@@ -66,19 +83,22 @@ for entry in "${OPEN_PRS[@]}"; do
|
||||
echo " open PR #${pr_num} (${pr_branch}): head unfetchable; treating as non-duplicate"
|
||||
continue
|
||||
fi
|
||||
pr_tree="$(git log -1 --format=%T "${pr_head}" 2>/dev/null || true)"
|
||||
echo " open PR #${pr_num} (${pr_branch}): tree ${pr_tree}"
|
||||
if [[ -n "${pr_tree}" && "${pr_tree}" == "${LOCAL_TREE}" ]]; then
|
||||
pr_fp="$(fingerprint_ref "${pr_head}" "${CHANGED_FILES[@]}")"
|
||||
echo " open PR #${pr_num} (${pr_branch}): fingerprint ${pr_fp}"
|
||||
if [[ "${pr_fp}" == "${LOCAL_FP}" ]]; then
|
||||
DUPLICATE_PR="${pr_num}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ -n "${DUPLICATE_PR}" ]]; then
|
||||
echo "Open PR #${DUPLICATE_PR} already carries this exact tree. Skipping push and PR creation."
|
||||
echo "Open PR #${DUPLICATE_PR} already carries this exact change. Skipping push and PR creation."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
BRANCH="${UPDATE_BRANCH_PREFIX}-${UPDATE_BRANCH_SUFFIX}-${LOCAL_FP:0:7}"
|
||||
echo "Pushing change to branch ${BRANCH}"
|
||||
|
||||
git config --local --get-regexp '^includeif\..*\.path$' 2>/dev/null |
|
||||
awk '{print $2}' |
|
||||
sort -u |
|
||||
@@ -115,20 +135,3 @@ else
|
||||
PR_NUMBER="${PR_URL##*/}"
|
||||
echo "Created PR #${PR_NUMBER}: ${PR_URL}"
|
||||
fi
|
||||
|
||||
# Close every other open PR in the same update class as superseded. A
|
||||
# PR is in the same class when its branch starts with the configured
|
||||
# UPDATE_BRANCH_PREFIX, so e.g. an image-versions run only retires
|
||||
# stale image-versions branches and never touches repository-refs,
|
||||
# skills, or human-authored PRs.
|
||||
for entry in "${OPEN_PRS[@]}"; do
|
||||
[[ -z "${entry}" ]] && continue
|
||||
pr_branch="${entry##*$'\t'}"
|
||||
[[ "${pr_branch}" == "${UPDATE_BRANCH_PREFIX}-"* ]] || continue
|
||||
pr_num="${entry%%$'\t'*}"
|
||||
echo "Closing superseded PR #${pr_num} (${pr_branch})"
|
||||
gh pr close "${pr_num}" \
|
||||
--repo "${REPO}" \
|
||||
--delete-branch \
|
||||
--comment "Superseded by #${PR_NUMBER}."
|
||||
done
|
||||
Reference in new issue
Block a user