Skip to content

Commit 86272c8

Browse files
authored
feat(stable-sync): Move stable sync process within github tools (#82)
* feat: improve the call to execute the script in gh tools * chore: test * chore: fix path * chore: test * chore: fix typo oin path * chore: standarize the file * chore: add step to clean up github tools repo * chore: use baseBranch instead of hardcoded master
1 parent 23704a3 commit 86272c8

2 files changed

Lines changed: 45 additions & 36 deletions

File tree

.github/scripts/stable-sync.js

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,38 @@ async function runGitCommands() {
2020
// Get branch name from command line arguments or use default
2121
const branchName = process.argv[2] || 'stable-main';
2222

23-
// Get base stable branch from environment variable (defaults to 'master' for backward compatibility of extension)
24-
const baseBranch = process.env.BASE_BRANCH || 'master';
23+
// Get base stable branch from environment variable (defaults to 'stable' for backward compatibility for mobile)
24+
const baseBranch = process.env.BASE_BRANCH || 'stable';
2525

2626
// Check if CREATE_BRANCH environment variable exists and is set to true
2727
const shouldPushBranch = (process.env.CREATE_BRANCH || 'false').toLowerCase() === 'true';
2828

2929
try {
3030
try {
3131
// Check if the branch already exists
32-
const { stdout: branchExists } = await exec(
33-
//`git rev-parse --quiet --verify ${branchName}`,
34-
`git ls-remote origin ${branchName}`,
35-
);
32+
const { stdout: branchExists } = await exec(`git ls-remote origin ${branchName}`);
3633
if (branchExists.trim()) {
37-
// Branch exists, so simply check it out
34+
// Branch exists, check it out
3835
await exec(`git checkout ${branchName}`);
3936
await exec(`git pull origin ${branchName}`);
4037
console.log(`Checked out branch: ${branchName}`);
4138
} else {
42-
throw new Error(
43-
'git rev-parse --quiet --verify failed. Branch hash empty',
44-
);
45-
}
46-
} catch (error) {
47-
if (error.stdout === '') {
48-
console.warn(
49-
`Branch does not exist, creating new ${branchName} branch.`,
50-
);
51-
52-
// Branch does not exist, create and check it out
39+
// Branch doesn't exist, create it
40+
console.warn(`Branch does not exist, creating new ${branchName} branch.`);
5341
await exec(`git checkout -b ${branchName}`);
5442
console.log(`Created and checked out branch: ${branchName}`);
55-
} else {
56-
console.error(`Error: ${error.message}`);
57-
process.exit(1);
5843
}
44+
} catch (error) {
45+
// Handle actual git command errors
46+
console.error(`Error: ${error.message}`);
47+
process.exit(1);
5948
}
6049

6150
await exec('git fetch');
6251
console.log('Executed: git fetch');
6352

64-
await exec('git reset --hard origin/stable');
65-
console.log('Executed: git reset --hard origin/stable');
53+
await exec(`git reset --hard origin/${baseBranch}`);
54+
console.log(`Executed: git reset --hard origin/${baseBranch}`);
6655

6756
try {
6857
await exec('git merge origin/main');
@@ -90,31 +79,31 @@ async function runGitCommands() {
9079
await exec('git checkout origin/main -- .');
9180
console.log('Executed: git checkout origin/main -- .');
9281

93-
await exec('git checkout origin/stable -- CHANGELOG.md');
94-
console.log('Executed: git checkout origin/stable -- CHANGELOG.md');
82+
await exec(`git checkout origin/${baseBranch} -- CHANGELOG.md`);
83+
console.log(`Executed: git checkout origin/${baseBranch} -- CHANGELOG.md`);
9584

9685
// Execute mobile-specific commands if REPO is 'mobile'
9786
if (process.env.REPO === 'mobile') {
9887
console.log('Executing mobile-specific commands...');
9988

100-
await exec('git checkout origin/stable -- bitrise.yml');
101-
console.log('Executed: git checkout origin/stable -- bitrise.yml');
89+
await exec(`git checkout origin/${baseBranch} -- bitrise.yml`);
90+
console.log(`Executed: git checkout origin/${baseBranch} -- bitrise.yml`);
10291

103-
await exec('git checkout origin/stable -- android/app/build.gradle');
104-
console.log('Executed: git checkout origin/stable -- android/app/build.gradle');
92+
await exec(`git checkout origin/${baseBranch} -- android/app/build.gradle`);
93+
console.log(`Executed: git checkout origin/${baseBranch} -- android/app/build.gradle`);
10594

106-
await exec('git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj');
107-
console.log('Executed: git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj');
95+
await exec(`git checkout origin/${baseBranch} -- ios/MetaMask.xcodeproj/project.pbxproj`);
96+
console.log(`Executed: git checkout origin/${baseBranch} -- ios/MetaMask.xcodeproj/project.pbxproj`);
10897

109-
await exec('git checkout origin/stable -- package.json');
110-
console.log('Executed: git checkout origin/stable -- package.json');
98+
await exec(`git checkout origin/${baseBranch} -- package.json`);
99+
console.log(`Executed: git checkout origin/${baseBranch} -- package.json`);
111100
}
112101
// Execute extension-specific commands if REPO is 'extension'
113102
else if (process.env.REPO === 'extension') {
114103
console.log('Executing extension-specific commands...');
115104

116105
const { stdout: packageJsonContent } = await exec(
117-
'git show origin/main:package.json',
106+
`git show origin/${baseBranch}:package.json`,
118107
);
119108
const packageJson = JSON.parse(packageJsonContent);
120109
const packageVersion = packageJson.version;
@@ -175,4 +164,4 @@ async function runGitCommands() {
175164
}
176165
}
177166

178-
runGitCommands();
167+
runGitCommands();

.github/workflows/stable-sync.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ on:
2020
type: string
2121
description: 'The name of the stable branch to sync to (e.g., stable, master, main)'
2222
default: 'stable'
23+
github-tools-version:
24+
required: false
25+
type: string
26+
description: 'The version of github-tools to use. Defaults to main.'
27+
default: 'main'
2328
workflow_call:
2429
inputs:
2530
semver-version:
@@ -36,6 +41,11 @@ on:
3641
type: string
3742
description: 'The name of the stable branch to sync to (e.g., stable, master, main)'
3843
default: 'stable'
44+
github-tools-version:
45+
required: false
46+
type: string
47+
description: 'The version of github-tools to use. Defaults to main.'
48+
default: ${{ github.action_ref }}
3949

4050
jobs:
4151
stable-sync:
@@ -45,6 +55,13 @@ jobs:
4555
with:
4656
fetch-depth: 0
4757

58+
- name: Checkout github-tools repository
59+
uses: actions/checkout@v4
60+
with:
61+
repository: MetaMask/github-tools
62+
ref: ${{ inputs.github-tools-version }}
63+
path: github-tools
64+
4865
- name: Setup Node.js Mobile
4966
if: ${{ inputs.repo-type == 'mobile' }}
5067
uses: actions/setup-node@v4
@@ -93,8 +110,11 @@ jobs:
93110
REPO: ${{ inputs.repo-type }} # Default to 'mobile' if not specified
94111
BASE_BRANCH: ${{ inputs.stable-branch-name }}
95112
run: |
96-
node .github/scripts/stable-sync.js "stable-main-${{ inputs.semver-version }}"
113+
# Execute the script from github-tools
114+
node ./github-tools/.github/scripts/stable-sync.js "stable-main-${{ inputs.semver-version }}"
97115
# Check if branch exists remotely
116+
echo "Cleaning up github-tools"
117+
rm -rf github-tools
98118
BRANCH_NAME="stable-main-${{ inputs.semver-version }}"
99119
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
100120
git pull --rebase

0 commit comments

Comments
 (0)