Skip to content

Commit c2fd75a

Browse files
Merge pull request #1257 from LedgerHQ/cev/manual_trigger
Add possibility to trig cherry-pick manually
2 parents 064612c + f66bbf8 commit c2fd75a

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

.github/workflows/auto-cherry-pick.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
description: 'Pull Request number to process'
1010
required: true
1111
type: number
12+
target_branch:
13+
description: 'Target Branch to apply cherry_pick (API_LEVEL_xxx)'
14+
required: true
15+
type: string
1216
defaults:
1317
run:
1418
shell: bash
@@ -48,14 +52,18 @@ jobs:
4852
git config --global url.https://github.com/.insteadOf [email protected]:
4953
git config --global url.https://oauth2:${{ secrets.GITHUB_TOKEN }}@github.com/.insteadOf https://github.com/
5054
51-
- name: Set PR ID
52-
id: pr_id_step
55+
- name : PR check
5356
run: |
57+
ARGS=(-t ${{ secrets.GITHUB_TOKEN }} -r ./)
58+
5459
# Define the variable PR_ID based on the trigger
5560
if [ "${{ github.event_name }}" == "pull_request" ]; then
56-
echo "PR_ID=${{ github.event.number }}" >> $GITHUB_OUTPUT
61+
ARGS+=(-p ${{ github.event.number }})
5762
else
58-
echo "PR_ID=${{ github.event.inputs.pr_number }}" >> $GITHUB_OUTPUT
63+
ARGS+=(-p ${{ inputs.pr_number }})
64+
if [ -n "${{ inputs.target_branch }}" ]; then
65+
ARGS+=(-b ${{ inputs.target_branch }})
66+
fi
5967
fi
60-
- name : PR check
61-
run: .github/workflows/scripts/pr_check.py -t ${{ secrets.GITHUB_TOKEN }} -r ./ -p ${{ steps.pr_id_step.outputs.PR_ID }}
68+
69+
.github/workflows/scripts/pr_check.py "${ARGS[@]}"

.github/workflows/scripts/pr_check.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def init_parser() -> ArgumentParser:
3838
parser.add_argument("--token", "-t", required=True, help="GitHub Access Token")
3939
parser.add_argument("--repo_path", "-r", help="Cloned repository path")
4040
parser.add_argument("--pull", "-p", type=int, required=True, help="Pull Request number")
41+
parser.add_argument("--branch", "-b", help="target branch (manual selection)")
4142
parser.add_argument("--dry_run", "-d", action='store_true', help="Dry Run mode")
4243
parser.add_argument("--verbose", "-v", action='store_true', help="Verbose mode")
4344
return parser
@@ -58,6 +59,21 @@ def set_logging(verbose: bool = False) -> None:
5859
logger.addHandler(handler)
5960

6061

62+
def set_gh_summary(value: str) -> None:
63+
"""Sets a summary status for a GitHub Actions workflow.
64+
This is used to write the summary of the workflow run.
65+
66+
Args:
67+
value: Summary content
68+
"""
69+
70+
# Check if the GITHUB_STEP_SUMMARY environment variable is set
71+
gh = os.environ.get("GITHUB_STEP_SUMMARY")
72+
if gh:
73+
with open(gh, "a", encoding="utf-8") as outfile:
74+
outfile.write(f"{value}\n")
75+
76+
6177
# ===============================================================================
6278
# Sub functions to retrieve objects
6379
# ===============================================================================
@@ -116,6 +132,7 @@ def get_commits_to_cherry_pick(pull: PullRequest) -> List[str]:
116132
logger.error("No commits found. Exiting.")
117133
sys.exit(1)
118134
logger.info("Found commits: %s", commits)
135+
set_gh_summary(f":loudspeaker: Found {len(commits)} commits")
119136
return commits
120137

121138

@@ -181,7 +198,7 @@ def cherry_pick_commits(local_repo: Repo,
181198

182199
for sha in commits:
183200
try:
184-
logger.info("Cherry-picking %s onto %s", sha, auto_branch)
201+
logger.info("Cherry-picking %s", sha)
185202
local_repo.git.cherry_pick('-x', sha)
186203
except GitCommandError as err:
187204
logger.error("Failed to apply cherry-pick of %s:\n%s", sha, err)
@@ -208,7 +225,7 @@ def cherry_pick_commits(local_repo: Repo,
208225
def create_pull_request_if_needed(git_repo: Repository,
209226
auto_branch: str,
210227
target_br: str,
211-
pull_number: int) -> None:
228+
pull_number: int) -> bool:
212229
"""Create a pull request if one does not already exist."""
213230
pr_auto_title = f"[AUTO_UPDATE] Branch {target_br}"
214231
prs = git_repo.get_pulls(state="open", base=target_br)
@@ -240,6 +257,7 @@ def create_pull_request_if_needed(git_repo: Repository,
240257
existing_pr.number, pull_number)
241258
except Exception as e:
242259
logger.error("Failed to update existing PR: %s", e)
260+
return False
243261
else:
244262
try:
245263
# Create a new pull request with the auto_update branch as
@@ -253,7 +271,7 @@ def create_pull_request_if_needed(git_repo: Repository,
253271
)
254272
except:
255273
logger.error("Failed to create PR!")
256-
sys.exit(1)
274+
return False
257275

258276
try:
259277
# Assign the PR to the original author
@@ -263,6 +281,7 @@ def create_pull_request_if_needed(git_repo: Repository,
263281
except:
264282
logger.error("Failed to assign PR to original author: %s", original_author)
265283
logger.info("Created PR for branch '%s': %s", auto_branch, new_pr.html_url)
284+
return True
266285

267286

268287
# ===============================================================================
@@ -294,8 +313,15 @@ def main():
294313
# Retrieve PR object
295314
pull = get_pull_request(git_repo, args.pull)
296315

297-
# Search target branches from PR description
298-
target_branches = get_target_branches(pull)
316+
if args.branch:
317+
# check branch naming
318+
if not args.branch.startswith("API_LEVEL_"):
319+
logger.error("Invalid branch: %s", args.branch)
320+
sys.exit(1)
321+
target_branches = [args.branch]
322+
else:
323+
# Search target branches from PR description
324+
target_branches = get_target_branches(pull)
299325

300326
# Retrieve all available branches
301327
branches = get_all_branches(git_repo)
@@ -341,30 +367,37 @@ def main():
341367

342368
# Check if dedicated auto_update branch exists, else create it
343369
auto_branch, branch_created = create_or_get_branch(local_repo,
344-
target_br,
345-
branches,
346-
args.dry_run)
370+
target_br,
371+
branches,
372+
args.dry_run)
347373

348374
# Cherry-pick the commits onto the auto_update branch
349375
if not cherry_pick_commits(local_repo,
350-
commits,
351-
auto_branch,
352-
default_branch,
353-
branch_created,
354-
args.dry_run):
376+
commits,
377+
auto_branch,
378+
default_branch,
379+
branch_created,
380+
args.dry_run):
355381
result = 1
356-
continue
357382

358-
if not args.dry_run:
383+
if result == 0 and not args.dry_run:
359384
# Create a pull request if needed
360-
create_pull_request_if_needed(git_repo, auto_branch, target_br, args.pull)
385+
if not create_pull_request_if_needed(git_repo, auto_branch, target_br, args.pull):
386+
result = 1
361387

362388
try:
363389
# Return to default branch
364390
local_repo.git.checkout(default_branch)
365391
except GitCommandError as e:
366392
logger.warning("Failed to checkout to default branch (%s): %s", default_branch, e)
367393

394+
# Set result in summary
395+
if result == 0:
396+
set_gh_summary(f":white_check_mark: {target_br}")
397+
else:
398+
set_gh_summary(f":x: {target_br}")
399+
400+
368401
if result != 0:
369402
logger.error("One or more operations failed.")
370403
sys.exit(1)

0 commit comments

Comments
 (0)