-
Notifications
You must be signed in to change notification settings - Fork 4
Update automerge workflow to support file_use_base and new branching pattern #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Copyright (c) 2024, NVIDIA CORPORATION. | ||
| # Copyright (c) 2024-2025, NVIDIA CORPORATION. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
|
|
@@ -28,6 +28,11 @@ on: | |
| branch: # github.event.pull_request.base.ref | ||
| required: true | ||
| type: string | ||
| file_use_base: | ||
| description: 'file/path that require to use content of BASE' | ||
| default: '' | ||
| required: false | ||
| type: string | ||
| secrets: | ||
| token: | ||
| required: true | ||
|
|
@@ -38,47 +43,114 @@ jobs: | |
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| # TODO: make this step as a shared action | ||
| - name: set HEAD ENV | ||
| run: | | ||
| echo "HEAD=${{ inputs.branch }}" >> $GITHUB_ENV | ||
| echo "DELETE_HEAD=False" >> $GITHUB_ENV | ||
|
|
||
| - name: Generate target branch | ||
| id: generate_version | ||
| run: | | ||
| current_branch="${{ inputs.branch }}" | ||
| version=${current_branch#branch-} | ||
|
|
||
| IFS='.' read -r -a parts <<< "$version" | ||
| year=${parts[0]} | ||
| month=${parts[1]} | ||
| month=$((10#$month + 2)) | ||
| if [ $month -gt 12 ]; then | ||
| month=$((month - 12)) | ||
| year=$((year + 1)) | ||
| # https://github.com/NVIDIA/spark-rapids-common/issues/34 | ||
| if [[ "${{ inputs.branch }}" == release/* ]]; then | ||
| echo "BASE=main" >> $GITHUB_ENV | ||
| else # maintain compatibility support for branch-* | ||
| current_branch="${{ inputs.branch }}" | ||
| version=${current_branch#branch-} | ||
|
|
||
| IFS='.' read -r -a parts <<< "$version" | ||
| year=${parts[0]} | ||
| month=${parts[1]} | ||
| month=$((10#$month + 2)) | ||
| if [ $month -gt 12 ]; then | ||
| month=$((month - 12)) | ||
| year=$((year + 1)) | ||
| fi | ||
|
|
||
| next_release=$(printf "%02d.%02d" $year $month) | ||
| echo "Next release is $next_release" | ||
| echo "BASE=branch-$next_release" >> $GITHUB_ENV | ||
| fi | ||
|
|
||
| next_release=$(printf "%02d.%02d" $year $month) | ||
| echo "Next release is $next_release" | ||
| echo "target_branch=branch-$next_release" >> $GITHUB_ENV | ||
| - name: Check for existing v*.0 tag | ||
| run: | | ||
| branch="${{ inputs.branch }}" | ||
| if [[ "$branch" == release/* ]]; then | ||
| version="${branch#release/}" | ||
| elif [[ "$branch" == branch-* ]]; then | ||
| version="${branch#branch-}" | ||
| else | ||
| echo "Unsupported branch ${{ inputs.branch }}..." | ||
| exit 1 | ||
| fi | ||
| tag_name="v${version}.0" | ||
| echo "Checking for tag $tag_name..." | ||
|
|
||
| CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${{ secrets.token }}" \ | ||
| https://api.github.com/repos/${{ inputs.owner }}/${{ inputs.repo }}/git/ref/tags/${tag_name}) | ||
| if [ "$CODE" -eq 200 ]; then | ||
| echo "Tag $tag_name exists. Skipping merge..." | ||
| echo "continue_merge=false" >> $GITHUB_ENV | ||
| else | ||
| echo "Proceeding with merge..." | ||
| echo "continue_merge=true" >> $GITHUB_ENV | ||
| fi | ||
|
|
||
| # TODO: make this step as a shared action | ||
| - name: Check if target branch exists | ||
| id: check_branch | ||
| if: env.continue_merge == 'true' | ||
| run: | | ||
| CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | ||
| https://api.github.com/repos/${{ inputs.owner }}/${{ inputs.repo }}/branches/${{ env.target_branch }}) | ||
| https://api.github.com/repos/${{ inputs.owner }}/${{ inputs.repo }}/branches/${{ env.BASE }}) | ||
| echo "Response code: $CODE..." | ||
|
|
||
| if [ $CODE -eq 200 ]; then | ||
| echo "branch_exists=true" >> $GITHUB_ENV | ||
| echo "continue_merge=true" >> $GITHUB_ENV | ||
| else | ||
| echo "branch_exists=false" >> $GITHUB_ENV | ||
| echo "Failed to find ${BRANCH_NAME}. Skip auto-merge..." | ||
| echo "continue_merge=false" >> $GITHUB_ENV | ||
| echo "Failed to find $BASE. Skip auto-merge..." | ||
| fi | ||
|
|
||
| - name: prepare code base if $FILE_USE_BASE has content | ||
| uses: actions/checkout@v4 | ||
| if: ${{ inputs.file_use_base != '' && env.continue_merge == 'true' }} | ||
| with: | ||
| ref: ${{ env.HEAD }} # force to fetch from the latest upstream instead of PR ref | ||
| token: ${{ secrets.token }} | ||
|
|
||
| - name: push intermediate branch for auto-merge | ||
| if: ${{ inputs.file_use_base != '' && env.continue_merge == 'true' }} | ||
| run: | | ||
| git config user.name "spark-rapids automation" | ||
| git config user.email "[email protected]" | ||
|
|
||
| git fetch origin "${HEAD}" "${BASE}" | ||
| git checkout -b ${INTERMEDIATE_HEAD} origin/${HEAD} | ||
|
|
||
| # Sync the $BASE branch with the commits from the $HEAD branch, | ||
| # excluding the paths defined as $FILE_USE_BASE. | ||
| git checkout origin/${BASE} -- ${FILE_USE_BASE} | ||
|
|
||
| # If any $FILE_USE_BASE is updated in the HEAD branch, | ||
| # always change it to the corresponding one from the BASE branch. | ||
| [ ! -z "$(git status --porcelain=v1 --untracked=no)" ] && \ | ||
| git commit -s -am "Auto-merge use ${BASE} versions" | ||
| git push origin ${INTERMEDIATE_HEAD} -f | ||
|
|
||
| # overwrite HEAD env | ||
| echo "HEAD=$INTERMEDIATE_HEAD" >> $GITHUB_ENV | ||
| echo "DELETE_HEAD=True" >> $GITHUB_ENV | ||
| env: | ||
| INTERMEDIATE_HEAD: bot-auto-merge-${{ env.HEAD }} | ||
| FILE_USE_BASE: ${{ inputs.file_use_base }} | ||
|
|
||
| - name: auto-merge job | ||
| if: env.branch_exists == 'true' | ||
| uses: NVIDIA/spark-rapids-common/auto-merge@main | ||
| if: env.continue_merge == 'true' | ||
| uses: NVIDIA/spark-rapids-common/action-helper@main | ||
| with: | ||
| operator: auto-merge | ||
| env: | ||
| OWNER: ${{ inputs.owner }} | ||
| REPO_NAME: ${{ inputs.repo }} | ||
| HEAD: ${{ inputs.branch }} | ||
| BASE: ${{ env.target_branch }} | ||
| AUTOMERGE_TOKEN: ${{ secrets.token }} | ||
| REPO: ${{ inputs.repo }} | ||
| HEAD: ${{ env.HEAD }} | ||
| BASE: ${{ env.BASE }} | ||
| TOKEN: ${{ secrets.token }} | ||
| DELETE_HEAD: ${{ env.DELETE_HEAD }} | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| FROM python:alpine | ||
|
|
||
| WORKDIR / | ||
| COPY python /python | ||
| COPY entrypoint.sh . | ||
| RUN chmod -R +x /python /entrypoint.sh | ||
| RUN pip install requests | ||
|
|
||
| ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| name: 'action helper' | ||
| description: 'helper for github-related operations' | ||
| inputs: | ||
| operator: | ||
| required: true | ||
| description: 'specify operator, e.g. auto-merge' | ||
| runs: | ||
| using: 'docker' | ||
| image: 'Dockerfile' | ||
| args: | ||
| - ${{ inputs.operator }} |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/bin/sh -l | ||
| # | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| set -e | ||
|
|
||
| if [ $# -ne 1 ]; then | ||
| echo "ERROR: invalid number of parameters, should be exact one" | ||
| exit 1 | ||
| fi | ||
|
|
||
| case $1 in | ||
|
|
||
| auto-merge) | ||
| /python/auto-merge --delete_head="${DELETE_HEAD}" | ||
| ;; | ||
|
|
||
| *) | ||
| echo "ERROR: unknown parameter: $1" | ||
| ;; | ||
| esac |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright (c) 2025, NVIDIA CORPORATION. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import sys | ||
| from argparse import ArgumentParser | ||
|
|
||
| from utils import EnvDefault, PullRequest, strtobool | ||
|
|
||
|
|
||
| def main(): | ||
| parser = ArgumentParser(description="Automerge") | ||
| parser.add_argument("--owner", action=EnvDefault, env="OWNER", | ||
| help="github token, will try use env OWNER if empty") | ||
| parser.add_argument("--repo", action=EnvDefault, env="REPO", | ||
| help="repo name, will try use env REPO if empty") | ||
| parser.add_argument("--head", action=EnvDefault, env="HEAD", | ||
| help="HEAD ref, will try use env HEAD if empty") | ||
| parser.add_argument("--base", action=EnvDefault, env="BASE", | ||
| help="Base ref, will try use env BASE if empty") | ||
| parser.add_argument("--token", action=EnvDefault, env="TOKEN", | ||
| help="github token, will try use env TOKEN if empty") | ||
| parser.add_argument("--delete_head", default=False, type=lambda x: bool(strtobool(x)), | ||
| help="if delete HEAD branch after auto-merge") | ||
| args = parser.parse_args() | ||
|
|
||
| pr = PullRequest(head_owner=args.owner, head=args.head, head_token=args.token, | ||
| base_owner=args.owner, repo=args.repo, base=args.base, base_token=args.token) | ||
| try: | ||
| if exist := pr.get_open(): | ||
| number = exist[0].get('number') | ||
| sha = exist[0].get('head').get('sha') | ||
| else: | ||
| params = { | ||
| # head share the same owner/repo with base in auto-merge | ||
| 'title': f"[auto-merge] {pr.head} to {pr.base} [skip ci] [bot]", | ||
| 'head': f"{pr.head_owner}:{pr.head}", | ||
| 'base': pr.base, | ||
| 'body': f"auto-merge triggered by github actions on `{pr.head}` to " | ||
| f"create a PR keeping `{pr.base}` up-to-date. " | ||
| "If this PR is unable to be merged due to conflicts, " | ||
| "it will remain open until manually fix.", | ||
| 'maintainer_can_modify': True | ||
| } | ||
| number, sha, term = pr.create(params) | ||
| if term: | ||
| sys.exit(0) | ||
| pr.auto_merge(number, sha) | ||
| if args.delete_head: | ||
| pr.delete_head() | ||
| except Exception as e: | ||
| print(e) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.