diff --git a/.github/workflows/mirror_noir_subrepo.yml b/.github/workflows/mirror_noir_subrepo.yml index e4c0a47ccbd..7d8cb1ed5b7 100644 --- a/.github/workflows/mirror_noir_subrepo.yml +++ b/.github/workflows/mirror_noir_subrepo.yml @@ -39,6 +39,33 @@ jobs: fetch-depth: 0 token: ${{ secrets.AZTEC_BOT_GITHUB_TOKEN }} + + - name: Setup env + run: | + set -xue # print commands + # Enable gh executable. We spread out the API requests between the github actions bot token, and aztecbot + export GH_TOKEN="${{ secrets.GITHUB_TOKEN }}" + # Do we have a PR active? + PR_URL=$(gh pr list --repo noir-lang/noir --head aztec-packages --json url --jq ".[0].url") + echo "PR_URL=$PR_URL" >> $GITHUB_ENV + + # compute_commit_message: Create a filtered git log for release-please changelog / metadata + function compute_commit_message() { + # Get the last sync PR's last commit state + LAST_MERGED_PR_HASH=`gh pr list --repo=noir-lang/noir --state merged --head aztec-packages --json headRefOid --jq=.[0].headRefOid` + # Use a commit heuristic where we look at when .gitrepo first started to look at that commit state (through a push) + COMMIT_HEURISTIC=$(git log -p -S"$LAST_MERGED_PR_HASH" --reverse --source -- noir/.gitrepo | grep -m 1 '^commit' | awk '{print $2}' || true) + if [[ " $COMMIT_HEURISTIC" = "" ]] ; then + # It it fails, just use our gitrepo parent commit (last time we pushed or pulled) + COMMIT_HEURISTIC=$BASE_AZTEC_COMMIT + fi + # Create a filtered git log for release-please changelog / metadata + RAW_MESSAGE=$(git log --pretty=format:"%s" $COMMIT_HEURISTIC..HEAD -- noir/ ':!noir/.gitrepo' | grep -v 'git subrepo' || true) + # Fix Aztec PR links and output message + echo "$RAW_MESSAGE" | sed -E 's/\(#([0-9]+)\)/(https:\/\/github.com\/AztecProtocol\/aztec-packages\/pull\/\1)/g' + } + echo "$(compute_commit_message)" >> .COMMIT_MESSAGE + # We push using git subrepo (https://github.com/ingydotnet/git-subrepo) # and push all Aztec commits as a single commit with metadata. - name: Push to branch @@ -48,47 +75,70 @@ jobs: export GH_TOKEN="${{ secrets.GITHUB_TOKEN }}" SUBREPO_PATH=noir BRANCH=aztec-packages + if [[ "$PR_URL" == "" ]]; then + # if no staging branch, we can overwrite + STAGING_BRANCH=$BRANCH + else + # otherwise we first reset our staging branch + STAGING_BRANCH=$BRANCH-staging + fi # identify ourselves, needed to commit git config --global user.name AztecBot git config --global user.email tech@aztecprotocol.com BASE_NOIR_COMMIT=`git config --file=noir/.gitrepo subrepo.commit` BASE_AZTEC_COMMIT=`git config --file=noir/.gitrepo subrepo.parent` - # Fix PR branch + + # clone noir repo for manipulations, we use aztec bot token for writeability git clone https://x-access-token:${{ secrets.AZTEC_BOT_GITHUB_TOKEN }}@github.com/noir-lang/noir.git noir-repo - cd noir-repo - git checkout $BRANCH || git checkout -b $BRANCH - git reset --hard "$BASE_NOIR_COMMIT" - # Reset our branch to our expected target - git push origin $BRANCH --force - cd .. - # Get the last sync PR's last commit state - LAST_MERGED_PR_HASH=`gh pr list --repo=noir-lang/noir --state merged --head aztec-packages --json headRefOid --jq=.[0].headRefOid` - # Use a commit heuristic where we look at when .gitrepo first started to look at that commit state (through a push) - COMMIT_HEURISTIC=$(git log -p -S"$LAST_MERGED_PR_HASH" --reverse --source -- noir/.gitrepo | grep -m 1 '^commit' | awk '{print $2}' || true) - if [[ " $COMMIT_HEURISTIC" = "" ]] ; then - # It it fails, just use our gitrepo parent commit (last time we pushed or pulled) - COMMIT_HEURISTIC=$BASE_AZTEC_COMMIT - fi - # Create a filtered git log for release-please changelog / metadata - MESSAGE=$(git log --pretty=format:"%s" $COMMIT_HEURISTIC..HEAD -- noir/ ':!noir/.gitrepo' | grep -v 'git subrepo' || true) - # Fix Aztec PR links - MESSAGE=$(echo "$MESSAGE" | sed -E 's/\(#([0-9]+)\)/(https:\/\/github.com\/AztecProtocol\/aztec-packages\/pull\/\1)/g') - git commit --allow-empty -m"chore: Sync to noir-lang/noir" -m"$MESSAGE" - COMMIT=$(git rev-parse HEAD) - # Now push to it with subrepo with computed commit messages - if ./scripts/git_subrepo.sh push $SUBREPO_PATH --squash --branch=$BRANCH; then - git reset $COMMIT - git commit --amend -am "$(git log -1 --pretty=%B) [skip ci]" - git push - else - echo "Problems syncing noir. We may need to pull the subrepo." - exit 1 + + # reset_pr: Reset aztec-packages staging. If no PR, this is the PR branch. + function reset_noir_staging_branch() { + cd noir-repo + git checkout $STAGING_BRANCH || git checkout -b $STAGING_BRANCH + git reset --hard "$BASE_NOIR_COMMIT" + # Reset our branch to our expected target + git push origin $STAGING_BRANCH --force + cd .. + } + # force_sync_staging: Push to our aztec-packages staging branch. + function force_sync_staging() { + MESSAGE=$(cat .COMMIT_MESSAGE) + git commit --allow-empty -m"chore: Sync to noir-lang/noir" -m"$MESSAGE" + COMMIT=$(git rev-parse HEAD) + # Now push to it with subrepo with computed commit messages + if ./scripts/git-subrepo/lib/git-subrepo push $SUBREPO_PATH --squash --branch=$STAGING_BRANCH; then + git reset $COMMIT + git commit --allow-empty --amend -am "$(git log -1 --pretty=%B) [skip ci]" + git push + else + echo "Problems syncing noir. We may need to pull the subrepo." + exit 1 + fi + } + # merge_staging_branch: Merge our staging branch into aztec-packages. + function merge_staging_branch() { + # Fix PR branch + cd noir-repo + git fetch # see recent change + git checkout $BRANCH || git checkout -b $BRANCH + git merge -Xtheirs $STAGING_BRANCH + git push origin $BRANCH + cd .. + } + reset_noir_staging_branch + force_sync_staging + if [[ "$PR_URL" != "" ]]; then + merge_staging_branch fi + + - name: Update PR + run: | + set -xue # print commands + MESSAGE=$(cat .COMMIT_MESSAGE) # Formatted for updating the PR, overrides for release-please commit message parsing PR_BODY="""BEGIN_COMMIT_OVERRIDE $MESSAGE END_COMMIT_OVERRIDE""" - PR_URL=$(gh pr list --repo noir-lang/noir --head aztec-packages --json url --jq ".[0].url") # for cross-opening PR in noir repo, we use aztecbot's token export GH_TOKEN=${{ secrets.AZTEC_BOT_GITHUB_TOKEN }} if [[ "$PR_URL" == "" ]]; then