From 3d7c7b4647a41b352986f7c66680e570dac3a667 Mon Sep 17 00:00:00 2001 From: Yaron Kaikov Date: Thu, 26 Dec 2024 17:07:52 +0200 Subject: [PATCH] ./github/scripts/auto-backport: align process to source available and more Syncing a few changes that have already been implemented in scylla-pkg: * remove condition for enterprise - since we are now running always from the `scylla-machine-image` repo * run backport process from stable branch (which is `master`) * improve conflict cases - by adding a comment to notify the PR owner * pass label for each pull_request_target trigger --- .github/scripts/auto-backport.py | 26 ++++++++++++------- .../workflows/add-label-when-promoted.yaml | 21 +++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/.github/scripts/auto-backport.py b/.github/scripts/auto-backport.py index 2ecec04c1..727b98ca1 100755 --- a/.github/scripts/auto-backport.py +++ b/.github/scripts/auto-backport.py @@ -29,6 +29,7 @@ def parse_args(): parser.add_argument('--commits', default=None, type=str, help='Range of promoted commits.') parser.add_argument('--pull-request', type=int, help='Pull request number to be backported') parser.add_argument('--head-commit', type=str, required=is_pull_request(), help='The HEAD of target branch after the pull request specified by --pull-request is merged') + parser.add_argument('--label', type=str, required=is_pull_request(), help='Backport label name when --pull-request is defined') return parser.parse_args() @@ -47,7 +48,11 @@ def create_pull_request(repo, new_branch_name, base_branch_name, pr, backport_pr ) logging.info(f"Pull request created: {backport_pr.html_url}") backport_pr.add_to_assignees(pr.user) - backport_pr.add_to_labels("conflicts") if is_draft else None + if is_draft: + backport_pr.add_to_labels("conflicts") + pr_comment = f"@{pr.user.login} - This PR has conflicts, therefore it was moved to `draft` \n" + pr_comment += "Please resolve them and mark this PR as ready for review" + backport_pr.create_issue_comment(pr_comment) logging.info(f"Assigned PR to original author: {pr.user}") return backport_pr except GithubException as e: @@ -141,14 +146,17 @@ def main(): closed_prs = [pr] for pr in closed_prs: - labels = [label.name for label in pr.labels] - backport_labels = [label for label in labels if backport_label_pattern.match(label)] - if promoted_label not in labels: - print(f'no {promoted_label} label: {pr.number}') - continue - if not backport_labels: - print(f'no backport label: {pr.number}') - continue + if args.pull_request: + backport_labels = [args.label] + else: + labels = [label.name for label in pr.labels] + backport_labels = [label for label in labels if backport_label_pattern.match(label)] + if promoted_label not in labels: + print(f'no {promoted_label} label: {pr.number}') + continue + if not backport_labels: + print(f'no backport label: {pr.number}') + continue commits = get_pr_commits(repo, pr, stable_branch, start_commit) logging.info(f"Found PR #{pr.number} with commit {commits} and the following labels: {backport_labels}") for backport_label in backport_labels: diff --git a/.github/workflows/add-label-when-promoted.yaml b/.github/workflows/add-label-when-promoted.yaml index f2046353b..d248464ba 100644 --- a/.github/workflows/add-label-when-promoted.yaml +++ b/.github/workflows/add-label-when-promoted.yaml @@ -5,10 +5,12 @@ on: branches: - master - next-*.* - - enterprise pull_request_target: types: [labeled] - branches: [master, next, enterprise] + branches: [master, next] + +env: + DEFAULT_BRANCH: 'master' jobs: check-commit: @@ -20,20 +22,11 @@ jobs: - name: Dump GitHub context env: GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - name: Set Default Branch - id: set_branch - run: | - if [[ "${{ github.repository }}" == *enterprise* ]]; then - echo "stable_branch=enterprise" >> $GITHUB_OUTPUT - else - echo "stable_branch=master" >> $GITHUB_OUTPUT - fi - name: Checkout repository uses: actions/checkout@v4 with: repository: ${{ github.repository }} - ref: ${{ env.stable_branch }} + ref: ${{ env.DEFAULT_BRANCH }} token: ${{ secrets.AUTO_BACKPORT_TOKEN }} fetch-depth: 0 # Fetch all history for all tags and branches - name: Set up Git identity @@ -49,7 +42,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} run: python .github/scripts/search_commits.py --commits ${{ github.event.before }}..${{ github.sha }} --repository ${{ github.repository }} --ref ${{ github.ref }} - name: Run auto-backport.py when promotion completed - if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', steps.set_branch.outputs.stable_branch) + if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', env.DEFAULT_BRANCH) env: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} run: python .github/scripts/auto-backport.py --repo ${{ github.repository }} --base-branch ${{ github.ref }} --commits ${{ github.event.before }}..${{ github.sha }} @@ -68,4 +61,4 @@ jobs: if: github.event_name == 'pull_request_target' && steps.check_label.outputs.backport_label == 'true' && github.event.pull_request.merged == true env: GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }} - run: python .github/scripts/auto-backport.py --repo ${{ github.repository }} --base-branch ${{ github.ref }} --pull-request ${{ github.event.pull_request.number }} --head-commit ${{ github.event.pull_request.base.sha }} + run: python .github/scripts/auto-backport.py --repo ${{ github.repository }} --base-branch ${{ github.ref }} --pull-request ${{ github.event.pull_request.number }} --head-commit ${{ github.event.pull_request.base.sha }} --label ${{ github.event.label.name }}