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