From d46520406d0ffb06112dfc14d24e140887f3c1e5 Mon Sep 17 00:00:00 2001 From: Yaron Kaikov Date: Tue, 17 Sep 2024 22:32:42 +0300 Subject: [PATCH] [.github/scripts/auto-backport.py] improve label logic In this commit there few improvments to the backport automation process based on the work done in https://github.com/scylladb/scylla-machine-image/pull/541 1. Create new parameter `--commit` to be use only when `--pull-request` is provided 2. Trigger workflow when label is `backport/x.y` only, rather then `backport/` as it was defined before. This way we will not trigger to flow when `backport/x.y-done` was added 3. when running on specific PR with `--pull-request` make sure we don't pass an empty commit 4. fix `get_prs_from_commits()` while trying to do `get_commit` on the wrong sha1 value --- .github/scripts/auto-backport.py | 12 ++++++++++-- .github/workflows/add-label-when-promoted.yaml | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/scripts/auto-backport.py b/.github/scripts/auto-backport.py index 44e8f46..11afd29 100755 --- a/.github/scripts/auto-backport.py +++ b/.github/scripts/auto-backport.py @@ -3,6 +3,7 @@ import argparse import os import re +import sys import tempfile import logging @@ -12,12 +13,17 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') +def is_pull_request(): + return '--pull-request' in sys.argv[1:] + + def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--repo', type=str, required=True, help='Github repository name') parser.add_argument('--base-branch', type=str, default='refs/heads/next', help='Base branch') 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') return parser.parse_args() @@ -66,6 +72,8 @@ def get_pr_commits(repo, pr, stable_branch, start_commit=None): # So here, we are validating the correct SHA for each commit so we can cherry-pick if promoted_commit.commit.message.startswith(commit_title): commits.append(promoted_commit.sha) + else: + commits.append(start_commit) elif pr.state == 'closed': events = pr.get_issue_events() @@ -104,7 +112,7 @@ def backport(repo, pr, version, commits, backport_base_branch): def get_prs_from_commits(repo, commits): for sha1 in commits: - commit = repo.get_commit(sha1) + commit = repo.get_commit(sha1.sha) for parent in commit.parents: prs = repo.get_pulls(state="closed", head=parent.sha) if prs: @@ -143,7 +151,7 @@ def main(): prs = get_prs_from_commits(repo, commits) closed_prs = list(prs) if args.pull_request: - start_commit = args.commits + start_commit = args.head_commit pr = repo.get_pull(args.pull_request) closed_prs = [pr] diff --git a/.github/workflows/add-label-when-promoted.yaml b/.github/workflows/add-label-when-promoted.yaml index 61f783f..06cf180 100644 --- a/.github/workflows/add-label-when-promoted.yaml +++ b/.github/workflows/add-label-when-promoted.yaml @@ -42,8 +42,19 @@ jobs: 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 }} + - name: Check if label starts with 'backport/' and contains digits + id: check_label + run: | + label_name="${{ github.event.label.name }}" + if [[ "$label_name" =~ ^backport/[0-9]+\.[0-9]+$ ]]; then + echo "Label matches backport/X.X pattern." + echo "backport_label=true" >> $GITHUB_OUTPUT + else + echo "Label does not match the required pattern." + echo "backport_label=false" >> $GITHUB_OUTPUT + fi - name: Run auto-backport.py when label was added - if: github.event_name == 'pull_request_target' && startsWith(github.event.label.name, 'backport/') && (github.event.pull_request.state == 'closed' && github.event.pull_request.merged == true) + if: github.event_name == 'pull_request_target' && steps.check_label.outputs.backport_label == 'true' && (github.event.pull_request.state == 'closed' && 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 }} --commits ${{ github.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.sha }}