11#! /bin/bash
22
3- # Check if gh is installed
4- if ! command -v gh & > /dev/null; then
5- echo " Error: gh (GitHub CLI) is not installed."
6- exit 1
7- fi
3+ function fail() {
4+ echo " Error: $* "
5+ exit 1
6+ }
87
9- # Check if jq is installed
10- if ! command -v jq & > /dev/null; then
11- echo " Error: jq is not installed."
12- exit 1
13- fi
14-
15- # Check if git is installed
16- if ! command -v git & > /dev/null; then
17- echo " Error: git is not installed."
18- exit 1
19- fi
8+ # Check if tools are installed
9+ command -v gh & > /dev/null || fail " gh (GitHub CLI) is not installed"
10+ command -v jq & > /dev/null || fail " jq is not installed"
11+ command -v git & > /dev/null || fail " git is not installed"
2012
2113# Default values
2214dry_run=true
2315repo=${GITHUB_REPOSITORY:- thunderbird/ thunderbird-android}
2416label=" task: uplift to beta"
2517branch=" beta"
18+ push=false
2619
2720# Parse command-line arguments
2821for arg in " $@ " ; do
@@ -41,26 +34,32 @@ for arg in "$@"; do
4134 branch=" beta"
4235 shift
4336 ;;
37+ --push)
38+ push=true
39+ shift
40+ ;;
4441 * )
45- echo " Unknown argument: $arg "
46- exit 1
42+ fail " Unknown argument: $arg "
4743 ;;
4844 esac
4945done
5046
5147# Check if on the correct branch
5248current_branch=$( git branch --show-current)
5349if [ " $current_branch " != " $branch " ]; then
54- echo " Error: You are not on the $branch branch. Please switch to the $branch branch."
55- exit 1
50+ fail " You are not on the $branch branch. Please switch to the $branch branch."
51+ fi
52+
53+ if [ " $dry_run " = true ]
54+ then
55+ echo " Dry run in progress, to disable pass --no-dry-run"
5656fi
5757
58- echo " Dry run: $dry_run , to disable dry run pass --no-dry-run"
5958echo " Label: \" $label \" "
6059echo " "
6160
6261# Fetch the uplift commits from the GitHub repository
63- json_data=$( gh pr list --repo " $repo " --label " $label " --state closed --json " mergedAt,mergeCommit,number,url" )
62+ json_data=$( gh pr list --repo " $repo " --label " $label " --state merged --json " mergedAt,mergeCommit,number,url,title " | jq -c . )
6463
6564# Sort by mergedAt
6665sorted_commits=$( echo " $json_data " | jq -c ' . | sort_by(.mergedAt) | .[]' )
@@ -72,22 +71,25 @@ if [ -z "$sorted_commits" ]; then
7271fi
7372
7473# Generate git cherry-pick commands
75- for commit in $sorted_commits ; do
74+ while IFS= read -r commit
75+ do
7676 oid=$( echo " $commit " | jq -r ' .mergeCommit.oid' )
7777 pr_number=$( echo " $commit " | jq -r ' .number' )
7878 pr_url=$( echo " $commit " | jq -r ' .url' )
79- echo " Cherry-picking $oid from $pr_url "
79+ pr_title=$( echo " $commit " | jq -r ' .title' )
80+ echo " Cherry-picking $oid from $pr_url ($pr_title )"
8081
8182 if [ " $dry_run " = false ]; then
82- if git cherry-pick -m 1 " $oid " ; then
83- gh pr edit " $pr_number " --remove-label " $label "
84- else
85- echo " Failed to cherry-pick $oid "
86- exit 1
83+ git cherry-pick -m 1 " $oid " || fail " Failed to cherry-pick $oid "
84+ if [ " $push " = true ]; then
85+ git push || fail " Failed to push $oid "
8786 fi
87+
88+ gh pr edit " $pr_number " --repo " $repo " --remove-label " $label " || fail " Failed to remove label from $pr_number "
8889 else
8990 echo " git cherry-pick -m 1 $oid "
90- echo " gh pr edit $pr_number --remove-label \" $label \" "
91+ [ " $push " = true ] && echo git push
92+ echo " gh pr edit $pr_number --repo \" $repo \" --remove-label \" $label \" "
9193 fi
9294 echo " "
93- done
95+ done <<< " $sorted_commits "
0 commit comments