Skip to content

Commit e5a2948

Browse files
committed
fix: consolidate release scripts and shared git utilities
1 parent 6038488 commit e5a2948

File tree

3 files changed

+151
-240
lines changed

3 files changed

+151
-240
lines changed

.github/scripts/create-platform-release-pr.sh

Lines changed: 30 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@
1717
# new_version_number - Build version for mobile platform (optional, required for mobile)
1818
# git_user_name - Git user name for commits (optional, defaults to 'metamaskbot')
1919
# git_user_email - Git user email for commits (optional, defaults to '[email protected]')
20+
#
21+
# Environment:
22+
# BASE_BRANCH - The base branch for the release PR (defaults to 'main')
23+
# GITHUB_TOKEN - Token for GitHub CLI operations (falls back to gh auth config)
24+
# TEST_ONLY - When set to "true", uses test branch prefixes and skips changelog PR
25+
# GITHUB_REPOSITORY_URL - Full HTTPS URL for the repository (required for changelog generation when TEST_ONLY is not "true")
2026

2127
set -e
2228
set -u
2329
set -o pipefail
2430

31+
# Sourcing helper functions
32+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
33+
# shellcheck source=.github/scripts/utils.sh
34+
source "${SCRIPT_DIR}/utils.sh"
35+
2536
# Input assignments (quoted args prevent shifting). Use defaults only for optional args.
2637
PLATFORM="${1}"
2738
PREVIOUS_VERSION_REF="${2:-}"
@@ -32,6 +43,7 @@ NEW_VERSION="${NEW_VERSION//[[:space:]]/}"
3243
NEW_VERSION_NUMBER="${4:-}"
3344
GIT_USER_NAME="${5:-metamaskbot}"
3445
GIT_USER_EMAIL="${6:-metamaskbot@users.noreply.github.com}"
46+
BASE_BRANCH="${BASE_BRANCH:-main}"
3547

3648
# Log assigned variables for debugging (after defaults and trimming)
3749
echo "Assigned variables:"
@@ -127,101 +139,6 @@ get_version_bump_branch_name() {
127139
# Main workflow functions
128140
# -----------------------
129141

130-
# Helper function to check if branch exists and checkout/create it
131-
checkout_or_create_branch() {
132-
local branch_name="$1"
133-
local base_branch="${2:-}" # Optional base branch for new branches
134-
135-
echo "Checking for existing branch ${branch_name}"
136-
137-
if git show-ref --verify --quiet "refs/heads/${branch_name}" || git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
138-
echo "Branch ${branch_name} already exists, checking it out"
139-
if git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
140-
git fetch origin "${branch_name}"
141-
git checkout "${branch_name}"
142-
else
143-
git checkout "${branch_name}"
144-
fi
145-
else
146-
echo "Creating new branch ${branch_name}"
147-
if [[ -n "$base_branch" ]]; then
148-
git checkout "$base_branch"
149-
git pull origin "$base_branch"
150-
fi
151-
git checkout -b "${branch_name}"
152-
fi
153-
154-
echo "Branch ${branch_name} ready"
155-
}
156-
157-
# Helper function to push branch with error handling
158-
push_branch_with_handling() {
159-
local branch_name="$1"
160-
161-
echo "Pushing changes to the remote.."
162-
if ! git push --set-upstream origin "${branch_name}"; then
163-
echo "No changes to push to ${branch_name}"
164-
# Check if branch exists remotely
165-
if git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
166-
echo "Branch ${branch_name} already exists remotely"
167-
else
168-
echo "Error: Failed to push and branch doesn't exist remotely"
169-
exit 1
170-
fi
171-
fi
172-
}
173-
174-
# Helper function to create PR if it doesn't exist
175-
create_pr_if_not_exists() {
176-
local branch_name="$1"
177-
local title="$2"
178-
local body="$3"
179-
local base_branch="${4:-main}"
180-
local labels="${5:-}"
181-
local search_method="${6:-head}" # "head" or "search"
182-
183-
echo "Creating PR for ${branch_name}.."
184-
185-
# Check if PR already exists using different methods
186-
local pr_exists=false
187-
if [[ "$search_method" == "search" ]]; then
188-
if gh pr list --search "head:${branch_name}" --json number --jq 'length' | grep -q "1"; then
189-
pr_exists=true
190-
fi
191-
else
192-
if gh pr list --head "${branch_name}" --json number --jq 'length' | grep -q "1"; then
193-
pr_exists=true
194-
fi
195-
fi
196-
197-
if $pr_exists; then
198-
echo "PR for branch ${branch_name} already exists"
199-
else
200-
# Build command array with conditional label inclusion
201-
local gh_cmd=(gh pr create --draft --title "${title}" --body "${body}" --base "${base_branch}" --head "${branch_name}")
202-
203-
# Add labels only if provided (GitHub CLI doesn't accept empty label values)
204-
if [[ -n "${labels:-}" ]]; then
205-
gh_cmd+=(--label "${labels}")
206-
fi
207-
208-
# Execute the command
209-
# echo "Executing: ${gh_cmd[@]}"
210-
"${gh_cmd[@]}"
211-
echo "PR Created: ${title}"
212-
fi
213-
}
214-
215-
# Configure git for automation
216-
configure_git() {
217-
echo "Configuring git.."
218-
git config user.name "${GIT_USER_NAME}"
219-
git config user.email "${GIT_USER_EMAIL}"
220-
221-
echo "Fetching from remote..."
222-
git fetch
223-
}
224-
225142
# Create release branch, update versions, and create PR
226143
create_release_pr() {
227144
local platform="$1"
@@ -231,7 +148,7 @@ create_release_pr() {
231148
local changelog_branch_name="$5"
232149

233150
echo "Checking out the release branch: ${release_branch_name}"
234-
git checkout "${release_branch_name}"
151+
checkout_or_create_branch "${release_branch_name}" "${BASE_BRANCH}"
235152

236153
echo "Release Branch Checked Out"
237154
echo "version : ${new_version}"
@@ -342,19 +259,6 @@ create_changelog_pr() {
342259
local release_branch_name="$4"
343260
local changelog_branch_name="$5"
344261

345-
# Use helper function for branch checkout/creation
346-
checkout_or_create_branch "${changelog_branch_name}"
347-
348-
# Generate Changelog and Test Plan
349-
echo "Generating changelog for ${platform}.."
350-
yarn auto-changelog update --rc \
351-
--repo "${GITHUB_REPOSITORY_URL}" \
352-
--currentVersion "${new_version}" \
353-
--autoCategorize \
354-
--useChangelogEntry \
355-
--useShortPrLink \
356-
--requirePrNumbers
357-
358262
# Skip commits.csv for hotfix releases (previous_version_ref is literal "null")
359263
# - When we create a new major/minor release, we fetch all commits included in the release, by fetching the diff between HEAD and previous version reference.
360264
# - When we create a new hotfix release, there are no commits included in the release by default (they will be cherry-picked one by one). So we don't have previous version reference, which is why the value is set to 'null'.
@@ -399,33 +303,22 @@ create_changelog_pr() {
399303
cd ../
400304
fi
401305

402-
# Skipping Google Sheets update since there is no need for it anymore
403-
# TODO: Remove this once the current post-main validation approach is stable
404-
# if [[ "${TEST_ONLY:-false}" == 'false' ]]; then
405-
# echo "Updating release sheet.."
406-
# # Create a new Release Sheet Page for the new version with our commits.csv content
407-
# yarn run update-release-sheet "${platform}" "${new_version}" "${GOOGLE_DOCUMENT_ID}" "./commits.csv" "${PROJECT_GIT_DIR}" "${MOBILE_TEMPLATE_SHEET_ID}" "${EXTENSION_TEMPLATE_SHEET_ID}"
408-
# fi
409-
# Note: Only change directories when we actually entered ./github-tools/
410-
411-
# Commit and Push Changelog Changes (exclude commits.csv)
412-
echo "Adding and committing changes.."
413-
local commit_msg="update changelog for ${new_version}"
414-
if [[ "${previous_version_ref,,}" == "null" ]]; then
415-
commit_msg="${commit_msg} (hotfix - no test plan)"
416-
fi
417-
if ! (git commit -am "${commit_msg}"); then
418-
echo "No changes detected; skipping commit."
419-
fi
420-
421-
local pr_body="This PR updates the change log for ${new_version}."
422-
if [[ "${previous_version_ref,,}" == "null" ]]; then
423-
pr_body="${pr_body} (Hotfix - no test plan generated.)"
424-
fi
425-
426-
# Use helper functions for push and PR creation
427-
push_branch_with_handling "${changelog_branch_name}"
428-
create_pr_if_not_exists "${changelog_branch_name}" "release: ${changelog_branch_name}" "${pr_body}" "${release_branch_name}" "" "search"
306+
# Delegate changelog update and PR creation to the shared update-release-changelog.sh script
307+
echo "Updating changelog and creating PR.."
308+
309+
# Export git identity for the shared script
310+
export GIT_AUTHOR_NAME="${GIT_USER_NAME}"
311+
export GIT_AUTHOR_EMAIL="${GIT_USER_EMAIL}"
312+
313+
# Call the shared script
314+
# The script is located in the same directory as this one
315+
"${SCRIPT_DIR}/update-release-changelog.sh" \
316+
"${release_branch_name}" \
317+
"${platform}" \
318+
"${GITHUB_REPOSITORY_URL}" \
319+
"${previous_version_ref}" \
320+
"${changelog_branch_name}" \
321+
"${new_version}"
429322

430323
echo "Changelog PR Ready"
431324
}
@@ -547,7 +440,7 @@ main() {
547440
release_branch_name=$(get_release_branch_name "$NEW_VERSION")
548441
changelog_branch_name="release/${NEW_VERSION}-Changelog"
549442
version_bump_branch_name=$(get_version_bump_branch_name "$next_version") # Execute main workflow
550-
configure_git
443+
configure_git "${GIT_USER_NAME}" "${GIT_USER_EMAIL}"
551444

552445
# Step 1: Create release branch and PR
553446
create_release_pr "$PLATFORM" "$NEW_VERSION" "$NEW_VERSION_NUMBER" "$release_branch_name" "$changelog_branch_name"

.github/scripts/update-release-changelog.sh

Lines changed: 30 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
# 3. repository_url - Full HTTPS URL for the invoking repository.
1111
#
1212
# Optional arguments:
13-
# 4. previous_version_ref - Previous version reference (branch/tag/SHA). Defaults to literal "null"
14-
# so that commits.csv generation is skipped, matching hotfix behaviour.
13+
# 4. previous_version_ref - Previous version reference (branch/tag/SHA). Defaults to "null"
14+
# (Hotfix mode) if omitted. If provided as an empty string,
15+
# it indicates a regular release.
16+
# 5. changelog_branch - Specific name for the changelog branch. If not provided, it will be
17+
# determined automatically (checking for existing release/ or chore/ branches).
18+
# 6. version - The semantic version (e.g., 6.20.0). If not provided, it will be
19+
# derived from the release_branch name.
1520
#
1621
# Environment (optional):
1722
# GITHUB_TOKEN - Token for GitHub CLI operations (falls back to gh auth config)
@@ -21,109 +26,22 @@
2126

2227
set -euo pipefail
2328

29+
# Sourcing helper functions
30+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
31+
# shellcheck source=.github/scripts/utils.sh
32+
source "${SCRIPT_DIR}/utils.sh"
33+
2434
RELEASE_BRANCH="${1:?release branch is required}"
2535
PLATFORM="${2:-extension}"
2636
REPOSITORY_URL="${3:?repository url is required}"
27-
PREVIOUS_VERSION_REF="${4:-null}"
37+
PREVIOUS_VERSION_REF="${4-null}"
38+
CHANGELOG_BRANCH_INPUT="${5:-}"
39+
VERSION_INPUT="${6:-}"
2840

2941
AUTHOR_NAME="${GIT_AUTHOR_NAME:-metamaskbot}"
3042
AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-metamaskbot@users.noreply.github.com}"
3143
TEST_ONLY="${TEST_ONLY:-false}"
3244

33-
# --- Helper functions copied or adapted from create-platform-release-pr.sh ---
34-
35-
configure_git() {
36-
# Configure git identity and fetch remote refs so subsequent operations run
37-
# with the same context as create-platform-release-pr.sh.
38-
echo "Configuring git.."
39-
git config user.name "${AUTHOR_NAME}"
40-
git config user.email "${AUTHOR_EMAIL}"
41-
42-
echo "Fetching from remote..."
43-
git fetch
44-
}
45-
46-
checkout_or_create_branch() {
47-
# Ensure a branch exists locally for the changelog workflow. If it already
48-
# exists locally or remotely, check it out; otherwise create it (optionally
49-
# from a provided base branch) so changelog updates have the proper base.
50-
local branch_name="$1"
51-
local base_branch="${2:-}"
52-
53-
echo "Checking for existing branch ${branch_name}"
54-
55-
if git show-ref --verify --quiet "refs/heads/${branch_name}" || git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
56-
echo "Branch ${branch_name} already exists, checking it out"
57-
if git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
58-
git fetch origin "${branch_name}"
59-
git checkout "${branch_name}"
60-
else
61-
git checkout "${branch_name}"
62-
fi
63-
else
64-
echo "Creating new branch ${branch_name}"
65-
if [[ -n "${base_branch}" ]]; then
66-
git checkout "${base_branch}"
67-
git pull origin "${base_branch}"
68-
fi
69-
git checkout -b "${branch_name}"
70-
fi
71-
72-
echo "Branch ${branch_name} ready"
73-
}
74-
75-
push_branch_with_handling() {
76-
# Push changelog updates upstream, tolerating no-op pushes while still
77-
# surfacing failures when the remote branch is missing.
78-
local branch_name="$1"
79-
80-
echo "Pushing changes to the remote.."
81-
if ! git push --set-upstream origin "${branch_name}"; then
82-
echo "No changes to push to ${branch_name}"
83-
if git ls-remote --heads origin "${branch_name}" | grep -q "${branch_name}"; then
84-
echo "Branch ${branch_name} already exists remotely"
85-
else
86-
echo "Error: Failed to push and branch doesn't exist remotely"
87-
exit 1
88-
fi
89-
fi
90-
}
91-
92-
create_pr_if_not_exists() {
93-
# Guard against duplicate changelog PRs by checking existing PRs before
94-
# opening a draft that targets the release branch.
95-
local branch_name="$1"
96-
local title="$2"
97-
local body="$3"
98-
local base_branch="${4:-main}"
99-
local labels="${5:-}"
100-
local search_method="${6:-head}"
101-
102-
echo "Creating PR for ${branch_name}.."
103-
104-
local pr_exists=false
105-
if [[ "${search_method}" == "search" ]]; then
106-
if gh pr list --search "head:${branch_name}" --json number --jq 'length' | grep -q "1"; then
107-
pr_exists=true
108-
fi
109-
else
110-
if gh pr list --head "${branch_name}" --json number --jq 'length' | grep -q "1"; then
111-
pr_exists=true
112-
fi
113-
fi
114-
115-
if ${pr_exists}; then
116-
echo "PR for branch ${branch_name} already exists"
117-
else
118-
local gh_cmd=(gh pr create --draft --title "${title}" --body "${body}" --base "${base_branch}" --head "${branch_name}")
119-
if [[ -n "${labels}" ]]; then
120-
gh_cmd+=(--label "${labels}")
121-
fi
122-
"${gh_cmd[@]}"
123-
echo "PR Created: ${title}"
124-
fi
125-
}
126-
12745
# -----------------------------------------------------------------
12846
# -----------------------------------------------------------------
12947

@@ -200,20 +118,29 @@ commit_and_push_changelog() {
200118
# -----------------------------------------------------------------
201119

202120
# Derive the semantic version from the branch naming convention (release/x.y.z only).
203-
if [[ "${RELEASE_BRANCH}" =~ ^release/([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
204-
VERSION="${BASH_REMATCH[1]}"
121+
if [[ -n "${VERSION_INPUT}" ]]; then
122+
VERSION="${VERSION_INPUT}"
205123
else
206-
echo "Release branch '${RELEASE_BRANCH}' does not match known patterns." >&2
207-
exit 1
124+
if [[ "${RELEASE_BRANCH}" =~ ^release/([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
125+
VERSION="${BASH_REMATCH[1]}"
126+
else
127+
echo "Release branch '${RELEASE_BRANCH}' does not match known patterns and no version was provided." >&2
128+
exit 1
129+
fi
208130
fi
209131

210132
GITHUB_REPOSITORY_URL="${REPOSITORY_URL}"
211133

212-
configure_git
134+
configure_git "${AUTHOR_NAME}" "${AUTHOR_EMAIL}"
213135

214136
ensure_release_branch "${RELEASE_BRANCH}"
215137

216-
CHANGELOG_BRANCH=$(determine_changelog_branch "${VERSION}")
138+
if [[ -n "${CHANGELOG_BRANCH_INPUT}" ]]; then
139+
CHANGELOG_BRANCH="${CHANGELOG_BRANCH_INPUT}"
140+
else
141+
CHANGELOG_BRANCH=$(determine_changelog_branch "${VERSION}")
142+
fi
143+
217144
checkout_or_create_branch "${CHANGELOG_BRANCH}" "${RELEASE_BRANCH}"
218145

219146
echo "Generating changelog for ${PLATFORM} ${VERSION}.."

0 commit comments

Comments
 (0)