diff --git a/.github/scripts/stable-sync.js b/.github/scripts/stable-sync.js index 26a9341f..d5544eb9 100644 --- a/.github/scripts/stable-sync.js +++ b/.github/scripts/stable-sync.js @@ -20,8 +20,8 @@ async function runGitCommands() { // Get branch name from command line arguments or use default const branchName = process.argv[2] || 'stable-main'; - // Get base stable branch from environment variable (defaults to 'master' for backward compatibility of extension) - const baseBranch = process.env.BASE_BRANCH || 'master'; + // Get base stable branch from environment variable (defaults to 'stable' for backward compatibility for mobile) + const baseBranch = process.env.BASE_BRANCH || 'stable'; // Check if CREATE_BRANCH environment variable exists and is set to true const shouldPushBranch = (process.env.CREATE_BRANCH || 'false').toLowerCase() === 'true'; @@ -29,40 +29,29 @@ async function runGitCommands() { try { try { // Check if the branch already exists - const { stdout: branchExists } = await exec( - //`git rev-parse --quiet --verify ${branchName}`, - `git ls-remote origin ${branchName}`, - ); + const { stdout: branchExists } = await exec(`git ls-remote origin ${branchName}`); if (branchExists.trim()) { - // Branch exists, so simply check it out + // Branch exists, check it out await exec(`git checkout ${branchName}`); await exec(`git pull origin ${branchName}`); console.log(`Checked out branch: ${branchName}`); } else { - throw new Error( - 'git rev-parse --quiet --verify failed. Branch hash empty', - ); - } - } catch (error) { - if (error.stdout === '') { - console.warn( - `Branch does not exist, creating new ${branchName} branch.`, - ); - - // Branch does not exist, create and check it out + // Branch doesn't exist, create it + console.warn(`Branch does not exist, creating new ${branchName} branch.`); await exec(`git checkout -b ${branchName}`); console.log(`Created and checked out branch: ${branchName}`); - } else { - console.error(`Error: ${error.message}`); - process.exit(1); } + } catch (error) { + // Handle actual git command errors + console.error(`Error: ${error.message}`); + process.exit(1); } await exec('git fetch'); console.log('Executed: git fetch'); - await exec('git reset --hard origin/stable'); - console.log('Executed: git reset --hard origin/stable'); + await exec(`git reset --hard origin/${baseBranch}`); + console.log(`Executed: git reset --hard origin/${baseBranch}`); try { await exec('git merge origin/main'); @@ -90,31 +79,31 @@ async function runGitCommands() { await exec('git checkout origin/main -- .'); console.log('Executed: git checkout origin/main -- .'); - await exec('git checkout origin/stable -- CHANGELOG.md'); - console.log('Executed: git checkout origin/stable -- CHANGELOG.md'); + await exec(`git checkout origin/${baseBranch} -- CHANGELOG.md`); + console.log(`Executed: git checkout origin/${baseBranch} -- CHANGELOG.md`); // Execute mobile-specific commands if REPO is 'mobile' if (process.env.REPO === 'mobile') { console.log('Executing mobile-specific commands...'); - await exec('git checkout origin/stable -- bitrise.yml'); - console.log('Executed: git checkout origin/stable -- bitrise.yml'); + await exec(`git checkout origin/${baseBranch} -- bitrise.yml`); + console.log(`Executed: git checkout origin/${baseBranch} -- bitrise.yml`); - await exec('git checkout origin/stable -- android/app/build.gradle'); - console.log('Executed: git checkout origin/stable -- android/app/build.gradle'); + await exec(`git checkout origin/${baseBranch} -- android/app/build.gradle`); + console.log(`Executed: git checkout origin/${baseBranch} -- android/app/build.gradle`); - await exec('git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj'); - console.log('Executed: git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj'); + await exec(`git checkout origin/${baseBranch} -- ios/MetaMask.xcodeproj/project.pbxproj`); + console.log(`Executed: git checkout origin/${baseBranch} -- ios/MetaMask.xcodeproj/project.pbxproj`); - await exec('git checkout origin/stable -- package.json'); - console.log('Executed: git checkout origin/stable -- package.json'); + await exec(`git checkout origin/${baseBranch} -- package.json`); + console.log(`Executed: git checkout origin/${baseBranch} -- package.json`); } // Execute extension-specific commands if REPO is 'extension' else if (process.env.REPO === 'extension') { console.log('Executing extension-specific commands...'); const { stdout: packageJsonContent } = await exec( - 'git show origin/main:package.json', + `git show origin/${baseBranch}:package.json`, ); const packageJson = JSON.parse(packageJsonContent); const packageVersion = packageJson.version; @@ -175,4 +164,4 @@ async function runGitCommands() { } } -runGitCommands(); \ No newline at end of file +runGitCommands(); diff --git a/.github/workflows/stable-sync.yml b/.github/workflows/stable-sync.yml index 555f5ef6..6dc846e3 100644 --- a/.github/workflows/stable-sync.yml +++ b/.github/workflows/stable-sync.yml @@ -20,6 +20,11 @@ on: type: string description: 'The name of the stable branch to sync to (e.g., stable, master, main)' default: 'stable' + github-tools-version: + required: false + type: string + description: 'The version of github-tools to use. Defaults to main.' + default: 'main' workflow_call: inputs: semver-version: @@ -36,6 +41,11 @@ on: type: string description: 'The name of the stable branch to sync to (e.g., stable, master, main)' default: 'stable' + github-tools-version: + required: false + type: string + description: 'The version of github-tools to use. Defaults to main.' + default: ${{ github.action_ref }} jobs: stable-sync: @@ -45,6 +55,13 @@ jobs: with: fetch-depth: 0 + - name: Checkout github-tools repository + uses: actions/checkout@v4 + with: + repository: MetaMask/github-tools + ref: ${{ inputs.github-tools-version }} + path: github-tools + - name: Setup Node.js Mobile if: ${{ inputs.repo-type == 'mobile' }} uses: actions/setup-node@v4 @@ -93,8 +110,11 @@ jobs: REPO: ${{ inputs.repo-type }} # Default to 'mobile' if not specified BASE_BRANCH: ${{ inputs.stable-branch-name }} run: | - node .github/scripts/stable-sync.js "stable-main-${{ inputs.semver-version }}" + # Execute the script from github-tools + node ./github-tools/.github/scripts/stable-sync.js "stable-main-${{ inputs.semver-version }}" # Check if branch exists remotely + echo "Cleaning up github-tools" + rm -rf github-tools BRANCH_NAME="stable-main-${{ inputs.semver-version }}" if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then git pull --rebase