Skip to content

Commit

Permalink
[.github/scripts/auto-backport.py] improve label logic
Browse files Browse the repository at this point in the history
In this commit there few improvments to the backport automation process
based on the work done in #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
  • Loading branch information
yaronkaikov committed Sep 19, 2024
1 parent fe06c8a commit d465204
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 10 additions & 2 deletions .github/scripts/auto-backport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import os
import re
import sys
import tempfile
import logging

Expand All @@ -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()


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]

Expand Down
15 changes: 13 additions & 2 deletions .github/workflows/add-label-when-promoted.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit d465204

Please sign in to comment.