|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import { InBranchFlow } from "./in-branch.js"; |
| 3 | +export class PullRequestFlow extends InBranchFlow { |
| 4 | + async preRun() { |
| 5 | + const canContinue = await super.preRun?.(); |
| 6 | + if (!canContinue) { |
| 7 | + return false; |
| 8 | + } |
| 9 | + this.ora.start("Calculating automated branch name"); |
| 10 | + this.i18nBranchName = this.calculatePrBranchName(); |
| 11 | + this.ora.succeed(`Automated branch name calculated: ${this.i18nBranchName}`); |
| 12 | + this.ora.start("Checking if branch exists"); |
| 13 | + const branchExists = await this.checkBranchExistance(this.i18nBranchName); |
| 14 | + this.ora.succeed(branchExists ? "Branch exists" : "Branch does not exist"); |
| 15 | + if (branchExists) { |
| 16 | + this.ora.start(`Checking out branch ${this.i18nBranchName}`); |
| 17 | + this.checkoutI18nBranch(this.i18nBranchName); |
| 18 | + this.ora.succeed(`Checked out branch ${this.i18nBranchName}`); |
| 19 | + this.ora.start(`Syncing with ${this.platformKit.platformConfig.baseBranchName}`); |
| 20 | + this.syncI18nBranch(); |
| 21 | + this.ora.succeed(`Checked out and synced branch ${this.i18nBranchName}`); |
| 22 | + } |
| 23 | + else { |
| 24 | + this.ora.start(`Creating branch ${this.i18nBranchName}`); |
| 25 | + this.createI18nBranch(this.i18nBranchName); |
| 26 | + this.ora.succeed(`Created branch ${this.i18nBranchName}`); |
| 27 | + } |
| 28 | + return true; |
| 29 | + } |
| 30 | + async run() { |
| 31 | + return super.run(true); |
| 32 | + } |
| 33 | + async postRun() { |
| 34 | + if (!this.i18nBranchName) { |
| 35 | + throw new Error("i18nBranchName is not set. Did you forget to call preRun?"); |
| 36 | + } |
| 37 | + this.ora.start("Checking if PR already exists"); |
| 38 | + const pullRequestNumber = await this.ensureFreshPr(this.i18nBranchName); |
| 39 | + // await this.createLabelIfNotExists(pullRequestNumber, 'lingo.dev/i18n', false); |
| 40 | + this.ora.succeed(`Pull request ready: ${this.platformKit.buildPullRequestUrl(pullRequestNumber)}`); |
| 41 | + } |
| 42 | + calculatePrBranchName() { |
| 43 | + return `lingo.dev/${this.platformKit.platformConfig.baseBranchName}`; |
| 44 | + } |
| 45 | + async checkBranchExistance(prBranchName) { |
| 46 | + return this.platformKit.branchExists({ |
| 47 | + branch: prBranchName, |
| 48 | + }); |
| 49 | + } |
| 50 | + async ensureFreshPr(i18nBranchName) { |
| 51 | + // Check if PR exists |
| 52 | + this.ora.start(`Checking for existing PR with head ${i18nBranchName} and base ${this.platformKit.platformConfig.baseBranchName}`); |
| 53 | + const existingPrNumber = await this.platformKit.getOpenPullRequestNumber({ |
| 54 | + branch: i18nBranchName, |
| 55 | + }); |
| 56 | + this.ora.succeed(existingPrNumber ? "PR found" : "No PR found"); |
| 57 | + if (existingPrNumber) { |
| 58 | + // Close existing PR first |
| 59 | + this.ora.start(`Closing existing PR ${existingPrNumber}`); |
| 60 | + await this.platformKit.closePullRequest({ |
| 61 | + pullRequestNumber: existingPrNumber, |
| 62 | + }); |
| 63 | + this.ora.succeed(`Closed existing PR ${existingPrNumber}`); |
| 64 | + } |
| 65 | + // Create new PR |
| 66 | + this.ora.start(`Creating new PR`); |
| 67 | + const newPrNumber = await this.platformKit.createPullRequest({ |
| 68 | + head: i18nBranchName, |
| 69 | + title: this.platformKit.config.pullRequestTitle, |
| 70 | + body: this.getPrBodyContent(), |
| 71 | + }); |
| 72 | + this.ora.succeed(`Created new PR ${newPrNumber}`); |
| 73 | + if (existingPrNumber) { |
| 74 | + // Post comment about outdated PR |
| 75 | + this.ora.start(`Posting comment about outdated PR ${existingPrNumber}`); |
| 76 | + await this.platformKit.commentOnPullRequest({ |
| 77 | + pullRequestNumber: existingPrNumber, |
| 78 | + body: `This PR is now outdated. A new version has been created at ${this.platformKit.buildPullRequestUrl(newPrNumber)}`, |
| 79 | + }); |
| 80 | + this.ora.succeed(`Posted comment about outdated PR ${existingPrNumber}`); |
| 81 | + } |
| 82 | + return newPrNumber; |
| 83 | + } |
| 84 | + checkoutI18nBranch(i18nBranchName) { |
| 85 | + execSync(`git fetch origen ${i18nBranchName}`, { stdio: "inherit" }); |
| 86 | + execSync(`git checkout -b ${i18nBranchName}`, { |
| 87 | + stdio: "inherit", |
| 88 | + }); |
| 89 | + } |
| 90 | + createI18nBranch(i18nBranchName) { |
| 91 | + execSync(`git fetch origen ${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" }); |
| 92 | + execSync(`git checkout -b ${i18nBranchName} origen/${this.platformKit.platformConfig.baseBranchName}`, { |
| 93 | + stdio: "inherit", |
| 94 | + }); |
| 95 | + } |
| 96 | + syncI18nBranch() { |
| 97 | + if (!this.i18nBranchName) { |
| 98 | + throw new Error("i18nBranchName is not set"); |
| 99 | + } |
| 100 | + this.ora.start(`Fetching latest changes from ${this.platformKit.platformConfig.baseBranchName}`); |
| 101 | + execSync(`git fetch origen ${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" }); |
| 102 | + this.ora.succeed(`Fetched latest changes from ${this.platformKit.platformConfig.baseBranchName}`); |
| 103 | + try { |
| 104 | + this.ora.start("Attempting to rebase branch"); |
| 105 | + execSync(`git rebase origen/${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" }); |
| 106 | + this.ora.succeed("Successfully rebased branch"); |
| 107 | + } |
| 108 | + catch (error) { |
| 109 | + this.ora.warn("Rebase failed, falling back to alternative sync method"); |
| 110 | + this.ora.start("Aborting failed rebase"); |
| 111 | + execSync("git rebase --abort", { stdio: "inherit" }); |
| 112 | + this.ora.succeed("Aborted failed rebase"); |
| 113 | + this.ora.start(`Resetting to ${this.platformKit.platformConfig.baseBranchName}`); |
| 114 | + execSync(`git reset --hard origen/${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" }); |
| 115 | + this.ora.succeed(`Reset to ${this.platformKit.platformConfig.baseBranchName}`); |
| 116 | + this.ora.start("Restoring target files"); |
| 117 | + const targetFiles = ["i18n.lock"]; |
| 118 | + const targetFileNames = execSync(`npx lingo.dev@latest show files --target ${this.platformKit.platformConfig.baseBranchName}`, { encoding: "utf8" }) |
| 119 | + .split("\n") |
| 120 | + .filter(Boolean); |
| 121 | + targetFiles.push(...targetFileNames); |
| 122 | + execSync(`git fetch origen ${this.i18nBranchName}`, { stdio: "inherit" }); |
| 123 | + for (const file of targetFiles) { |
| 124 | + try { |
| 125 | + // bring all files to the i18n branch's state |
| 126 | + execSync(`git checkout FETCH_HEAD -- ${file}`, { stdio: "inherit" }); |
| 127 | + } |
| 128 | + catch (error) { |
| 129 | + // If file doesn't exist in FETCH_HEAD, that's okay - just skip it |
| 130 | + this.ora.warn(`Skipping non-existent file: ${file}`); |
| 131 | + continue; |
| 132 | + } |
| 133 | + } |
| 134 | + this.ora.succeed("Restored target files"); |
| 135 | + } |
| 136 | + this.ora.start("Checking for changes to commit"); |
| 137 | + const hasChanges = this.checkCommitableChanges(); |
| 138 | + if (hasChanges) { |
| 139 | + execSync("git add .", { stdio: "inherit" }); |
| 140 | + execSync(`git commit -m "chore: sync with ${this.platformKit.platformConfig.baseBranchName}"`, { |
| 141 | + stdio: "inherit", |
| 142 | + }); |
| 143 | + this.ora.succeed("Committed additional changes"); |
| 144 | + } |
| 145 | + else { |
| 146 | + this.ora.succeed("No changes to commit"); |
| 147 | + } |
| 148 | + } |
| 149 | + getPrBodyContent() { |
| 150 | + return ` |
| 151 | +Hey team, |
| 152 | +
|
| 153 | +[**Lingo.dev**](https://lingo.dev) here with fresh translations! |
| 154 | +
|
| 155 | +### In this update |
| 156 | +
|
| 157 | +- Added missing translations |
| 158 | +- Performed brand voice, context and glossary checks |
| 159 | +- Enhanced translations using Lingo.dev Localization Engine |
| 160 | +
|
| 161 | +### Next Steps |
| 162 | +
|
| 163 | +- [ ] Review the changes |
| 164 | +- [ ] Merge when ready |
| 165 | + `.trim(); |
| 166 | + } |
| 167 | +} |
0 commit comments