diff --git a/.github/workflows/create_ifu_tag.yml b/.github/workflows/create_ifu_tag.yml new file mode 100644 index 0000000000000..b6d69f6516ba8 --- /dev/null +++ b/.github/workflows/create_ifu_tag.yml @@ -0,0 +1,116 @@ +name: Create git tags for IFU PRs + +on: + pull_request: + types: [closed] + +permissions: + contents: write # create/push tags + pull-requests: write # edit PR body + +jobs: + tag-ifu: + # Only proceed if: merged AND title has both markers + if: > + github.event.pull_request.merged == true && + contains(github.event.pull_request.title, '[AUTOGENERATED]') && + contains(github.event.pull_request.title, 'IFU') + runs-on: ubuntu-latest + + steps: + - name: Checkout base repo (full history) + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.base.ref }} + fetch-depth: 0 + + - name: Configure Git user + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + - name: Derive key SHAs (rocm base, upstream main, merge) + id: shas + shell: bash + run: | + set -euo pipefail + + PR_NUM="${{ github.event.pull_request.number }}" + UPSTREAM_BASE_REF="${{ github.event.pull_request.base.ref }}" #Refers to upstream branch + HEAD_SHA="${{ github.event.pull_request.head.sha }}" #Refers to HEAD commit in PR branch + MERGE_SHA="${{ github.event.pull_request.merge_commit_sha }}" #Refers to merge commit that landed the PR + + # The ROCm base commit is the first parent of the merge commit that landed the PR + # (i.e., the base branch tip BEFORE this PR merged). + ROCM_BASE_SHA=$(git rev-parse "${MERGE_SHA}^1") #Tip Of Tree of rocm branch pre-IFU merge + + # Add and fetch upstream to identify the upstream/main commit that HEAD integrated. + git remote add upstream "https://github.com/pytorch/pytorch.git" + git fetch upstream "$UPSTREAM_BASE_REF" + + # Heuristic: the upstream commit integrated by the PR's head is the merge-base + # between the PR head commit and upstream/main as fetched now. + # This gives you the exact upstream commit (or the best common ancestor) that HEAD included. + UPSTREAM_MAIN_SHA=$(git merge-base "${HEAD_SHA}" "upstream/$UPSTREAM_BASE_REF") #Tip of Tree of upstream branch that we pulled locally + echo "PR_NUM=$PR_NUM" + echo "UPSTREAM_BASE_REF=$UPSTREAM_BASE_REF" + echo "HEAD_SHA=$HEAD_SHA" + echo "MERGE_SHA=$MERGE_SHA" + echo "ROCM_BASE_SHA=$ROCM_BASE_SHA" + echo "UPSTREAM_MAIN_SHA=$UPSTREAM_MAIN_SHA" + + + echo "PR_NUM=$PR_NUM" >> "$GITHUB_OUTPUT" + echo "UPSTREAM_BASE_REF=$UPSTREAM_BASE_REF" >> "$GITHUB_OUTPUT" + echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_OUTPUT" + echo "MERGE_SHA=$MERGE_SHA" >> "$GITHUB_OUTPUT" + echo "ROCM_BASE_SHA=$ROCM_BASE_SHA" >> "$GITHUB_OUTPUT" + echo "UPSTREAM_MAIN_SHA=$UPSTREAM_MAIN_SHA" >> "$GITHUB_OUTPUT" + + - name: Extract tag base from PR title + id: tagname + run: | + TITLE="${{ github.event.pull_request.title }}" + # Remove everything up to and including "[AUTOGENERATED]" + BASE_TAG=$(echo "$TITLE" | sed -E 's/^\[AUTOGENERATED\][[:space:]]*//') + + echo "BASE_TAG=$BASE_TAG" + echo "PRE_TAG=${BASE_TAG}_pre" + echo "POST_TAG=${BASE_TAG}_post" + + echo "BASE_TAG=$BASE_TAG" >> $GITHUB_OUTPUT + echo "PRE_TAG=${BASE_TAG}_pre" >> $GITHUB_OUTPUT + echo "POST_TAG=${BASE_TAG}_post" >> $GITHUB_OUTPUT + + - name: Create pre/post tags + shell: bash + run: | + set -euo pipefail + echo "Tagging:" + echo " ${{ steps.tagname.outputs.PRE_TAG }} @ ${{ steps.shas.outputs.ROCM_BASE_SHA }}" + echo " ${{ steps.tagname.outputs.POST_TAG }} @ ${{ steps.shas.outputs.MERGE_SHA }}" + + git tag -a "${{ steps.tagname.outputs.PRE_TAG }}" -m "IFU pre (PR #${{ steps.shas.outputs.PR_NUM }})" "${{ steps.shas.outputs.ROCM_BASE_SHA }}" + git tag -a "${{ steps.tagname.outputs.POST_TAG }}" -m "IFU post (PR #${{ steps.shas.outputs.PR_NUM }})" "${{ steps.shas.outputs.MERGE_SHA }}" + + #Force pushing is safe. If we land a new PR, we'd wanna retag a commit if we have to. + git push origin "refs/tags/${{ steps.tagname.outputs.PRE_TAG }}" -f + git push origin "refs/tags/${{ steps.tagname.outputs.POST_TAG }}" -f + + - name: Append rocm_base & upstream_main to PR body + env: + GH_TOKEN: ${{ secrets.IFU_GITHUB_TOKEN }} + shell: bash + run: | + set -euo pipefail + # Read current body + PR="${{ steps.shas.outputs.PR_NUM }}" + CURR=$(gh api repos/${{ github.repository }}/pulls/$PR --jq .body) + APPEND=$'\n'"rocm_base: ${{ steps.shas.outputs.ROCM_BASE_SHA }}"$'\n'"upstream_main: ${{ steps.shas.outputs.UPSTREAM_MAIN_SHA }}"$'\n' + NEW_BODY="${CURR}${APPEND}" + + # Write to a temp file and update PR body + printf '%s' "$NEW_BODY" > body.txt + gh api --method PATCH -H "Accept: application/vnd.github+json" \ + repos/${{ github.repository }}/pulls/$PR -F body=@body.txt +