Skip to content

Commit 0191bf9

Browse files
authored
fix(git): add more robust error handling for Git operation (#579)
1 parent 37f012b commit 0191bf9

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

action/src/flows/pull-request.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class PullRequestFlow extends InBranchFlow {
6161
private async ensureFreshPr(i18nBranchName: string) {
6262
// Check if PR exists
6363
this.ora.start(
64-
`Checking for existing PR with head ${i18nBranchName} and base ${this.platformKit.platformConfig.baseBranchName}`,
64+
`Checking for existing PR with head ${i18nBranchName} and base ${this.platformKit.platformConfig.baseBranchName}`
6565
);
6666
const existingPrNumber = await this.platformKit.getOpenPullRequestNumber({
6767
branch: i18nBranchName,
@@ -91,7 +91,9 @@ export class PullRequestFlow extends InBranchFlow {
9191
this.ora.start(`Posting comment about outdated PR ${existingPrNumber}`);
9292
await this.platformKit.commentOnPullRequest({
9393
pullRequestNumber: existingPrNumber,
94-
body: `This PR is now outdated. A new version has been created at ${this.platformKit.buildPullRequestUrl(newPrNumber)}`,
94+
body: `This PR is now outdated. A new version has been created at ${this.platformKit.buildPullRequestUrl(
95+
newPrNumber
96+
)}`,
9597
});
9698
this.ora.succeed(`Posted comment about outdated PR ${existingPrNumber}`);
9799
}
@@ -107,10 +109,22 @@ export class PullRequestFlow extends InBranchFlow {
107109
}
108110

109111
private createI18nBranch(i18nBranchName: string) {
110-
execSync(`git fetch origin ${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" });
111-
execSync(`git checkout -b ${i18nBranchName} origin/${this.platformKit.platformConfig.baseBranchName}`, {
112-
stdio: "inherit",
113-
});
112+
try {
113+
execSync(`git fetch origin ${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" });
114+
execSync(`git checkout -b ${i18nBranchName} origin/${this.platformKit.platformConfig.baseBranchName}`, {
115+
stdio: "inherit",
116+
});
117+
} catch (error) {
118+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
119+
this.ora.fail(`Failed to create branch: ${errorMessage}`);
120+
this.ora.info(`
121+
Troubleshooting tips:
122+
1. Make sure you have permission to create branches
123+
2. Check if the branch already exists locally (try 'git branch -a')
124+
3. Verify connectivity to remote repository
125+
`);
126+
throw new Error(`Branch creation failed: ${errorMessage}`);
127+
}
114128
}
115129

116130
private syncI18nBranch() {
@@ -141,7 +155,7 @@ export class PullRequestFlow extends InBranchFlow {
141155
const targetFiles = ["i18n.lock"];
142156
const targetFileNames = execSync(
143157
`npx lingo.dev@latest show files --target ${this.platformKit.platformConfig.baseBranchName}`,
144-
{ encoding: "utf8" },
158+
{ encoding: "utf8" }
145159
)
146160
.split("\n")
147161
.filter(Boolean);

action/src/platforms/bitbucket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class BitbucketPlatformKit extends PlatformKit<BitbucketConfig> {
4949
// https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get
5050
return values?.find(
5151
({ source, destination }) =>
52-
source?.branch?.name === branch && destination?.branch?.name === this.platformConfig.baseBranchName,
52+
source?.branch?.name === branch && destination?.branch?.name === this.platformConfig.baseBranchName
5353
);
5454
})
5555
.then((pr) => pr?.id);

action/src/platforms/github.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Octokit } from "octokit";
22
import { PlatformKit } from "./_base.js";
33
import Z from "zod";
4+
45
import { execSync } from "child_process";
56

67
export class GitHubPlatformKit extends PlatformKit {
@@ -76,7 +77,9 @@ export class GitHubPlatformKit extends PlatformKit {
7677
if (ghToken && processOwnCommits) {
7778
console.log("Using provided GH_TOKEN. This will trigger your CI/CD pipeline to run again.");
7879

79-
execSync(`git remote set-url origin https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`, {
80+
const url = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`;
81+
82+
execSync(`git remote set-url origin ${url}`, {
8083
stdio: "inherit",
8184
});
8285
}

action/src/platforms/gitlab.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class GitlabPlatformKit extends PlatformKit {
8181
title,
8282
{
8383
description: body,
84-
},
84+
}
8585
);
8686
return mr.iid;
8787
}
@@ -92,6 +92,7 @@ export class GitlabPlatformKit extends PlatformKit {
9292

9393
gitConfig(): Promise<void> | void {
9494
const url = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`;
95+
9596
execSync(`git remote set-url origin ${url}`, {
9697
stdio: "inherit",
9798
});

0 commit comments

Comments
 (0)