Skip to content

Commit

Permalink
move pr-update workflow logic into shell functions (#55)
Browse files Browse the repository at this point in the history
Change-Id: Id4aba343b780a1d28d20c43f87b004467e360e8e
  • Loading branch information
oliverlee authored Jun 9, 2024
1 parent 03df39a commit e112f75
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 58 deletions.
51 changes: 51 additions & 0 deletions .github/pr-update-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

set -euo pipefail

function gh_api
{
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$@"
}

function pull_requests_with_base
{
base="$1"

gh pr list \
--base "$base" \
--json number \
--jq ".[] | .number"
}

function rebase_onto
{
base="$1"
name=$(git show -s --format='%cn')
email=$(git show -s --format='%ce')

git \
-c user.name="$name" \
-c user.email="$email" \
-c advice.mergeConflict=false \
rebase --onto "$base" HEAD~1
}

function try_update_with
{
pr="$1"
base="$2"

gh pr checkout "$pr" >/dev/null 2>&1

if rebase_onto "$base"; then
git push origin --force-with-lease --quiet
gh pr edit --base "${base#*/}" >/dev/null
echo "...✅"
else
echo "...failed to rebase ❌"
git rebase --abort
fi
}
95 changes: 37 additions & 58 deletions .github/workflows/pr-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ on:
branches: [ main ]
workflow_dispatch:

permissions: write-all

jobs:
update-stacked-pull-requests:
update-stacked:
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v1
Expand All @@ -23,36 +21,24 @@ jobs:
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
head=/repos/${{ github.repository }}/commits/main
message=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--jq ".commit.message" \
$head)
change_id=$(echo "$message" | grep "Change-Id:" | cut -d' ' -f2)
set -a
source .github/pr-update-utils.sh
default_branch=${{ github.event.repository.default_branch }}
child_pr=$(gh pr list --base $change_id --json number --jq ".[] | .number")
git fetch origin "$default_branch" --depth 1 --quiet
if [[ "$child_pr" ]]; then
while IFS= read -r number; do
echo updating PR $number
gh pr checkout $number
git fetch origin main --depth 1
base_change_id=$(\
gh_api --jq ".commit.message" "/repos/${{ github.repository }}/commits/${default_branch}" \
| grep "Change-Id:" \
| cut -d' ' -f2)
if GIT_COMMITTER_NAME=$(git show -s --format='%cn') EMAIL=$(git show -s --format='%ce') \
git rebase --onto origin/main HEAD~1; then
git push origin --force-with-lease
gh pr edit --base main
else
echo ...failed to rebase
git reset --hard HEAD
fi
done <<< "$child_pr"
fi
pull_requests_with_base "$base_change_id" \
| xargs -I {} bash -c '\
set -euo pipefail; \
echo "updating PR {}"; \
try_update_with "{}" "origin/${default_branch}" '
update-out-of-date-pull-requests:
update-out-of-date:
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v1
Expand All @@ -67,39 +53,32 @@ jobs:
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
head=/repos/${{ github.repository }}/commits/main
set -a
source .github/pr-update-utils.sh
default_branch=${{ github.event.repository.default_branch }}
main_sha=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--jq ".sha" \
$head)
git fetch origin "$default_branch" --depth 1 --quiet
pull_requests=$(gh pr list --base main --json number --jq ".[] | .number")
head_commit=$(gh_api --jq ".sha" "/repos/${{ github.repository }}/commits/${default_branch}")
if [[ "$pull_requests" ]]; then
git fetch origin main --depth 1
function update
{
number="$1"
echo -n "checking if PR $number is up to date "
while IFS= read -r number; do
echo checking if PR $number is out of date
pr_head_sha=$(gh pr view $number --json commits --jq ".commits | last | .oid")
pr_head_commit=$(gh pr view $number --json commits --jq ".commits | last | .oid")
pr_status=$(gh_api --jq ".status" /repos/${{ github.repository }}/compare/$head_commit...$pr_head_commit)
pr_status=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--jq ".status" \
/repos/${{ github.repository }}/compare/$main_sha...$pr_head_sha)
if [[ "$pr_status" == "ahead" ]]; then
echo "✅"
else
echo "";
try_update_with "$number" "origin/${default_branch}"
fi
if [[ "$pr_status" != "identical" && "$pr_status" != "active" ]]; then
gh pr checkout $number
}
if GIT_COMMITTER_NAME=$(git show -s --format='%cn') EMAIL=$(git show -s --format='%ce') \
git rebase --onto origin/main HEAD~1; then
git push origin --force-with-lease
else
echo ...failed to rebase
git reset --hard HEAD
fi
fi
done <<< "$pull_requests"
fi
pull_requests_with_base "$default_branch" \
| xargs -I {} bash -c '\
set -euo pipefail; \
update "{}" '

0 comments on commit e112f75

Please sign in to comment.