From 9c8e2435b1e636613ff702167779e81f90624650 Mon Sep 17 00:00:00 2001 From: sudhanshu20204 Date: Wed, 26 Mar 2025 00:17:55 +0530 Subject: [PATCH 1/4] fix(git): add more robust error handling for Git operation --- action/src/flows/pull-request.ts | 28 +++++++++++++++++++++------- action/src/platforms/bitbucket.ts | 2 +- action/src/platforms/git-utils.ts | 16 ++++++++++++++++ action/src/platforms/github.ts | 9 +++++---- action/src/platforms/gitlab.ts | 11 +++++------ 5 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 action/src/platforms/git-utils.ts diff --git a/action/src/flows/pull-request.ts b/action/src/flows/pull-request.ts index c4ac306bb..ab199177c 100644 --- a/action/src/flows/pull-request.ts +++ b/action/src/flows/pull-request.ts @@ -61,7 +61,7 @@ export class PullRequestFlow extends InBranchFlow { 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}`, + `Checking for existing PR with head ${i18nBranchName} and base ${this.platformKit.platformConfig.baseBranchName}` ); const existingPrNumber = await this.platformKit.getOpenPullRequestNumber({ branch: i18nBranchName, @@ -91,7 +91,9 @@ export class PullRequestFlow extends InBranchFlow { 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)}`, + 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}`); } @@ -107,10 +109,22 @@ export class PullRequestFlow extends InBranchFlow { } private createI18nBranch(i18nBranchName: string) { - execSync(`git fetch origin ${this.platformKit.platformConfig.baseBranchName}`, { stdio: "inherit" }); - execSync(`git checkout -b ${i18nBranchName} origin/${this.platformKit.platformConfig.baseBranchName}`, { - stdio: "inherit", - }); + 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() { @@ -141,7 +155,7 @@ export class PullRequestFlow extends InBranchFlow { const targetFiles = ["i18n.lock"]; const targetFileNames = execSync( `npx lingo.dev@latest show files --target ${this.platformKit.platformConfig.baseBranchName}`, - { encoding: "utf8" }, + { encoding: "utf8" } ) .split("\n") .filter(Boolean); diff --git a/action/src/platforms/bitbucket.ts b/action/src/platforms/bitbucket.ts index 04f1058fc..817e2d5c8 100644 --- a/action/src/platforms/bitbucket.ts +++ b/action/src/platforms/bitbucket.ts @@ -49,7 +49,7 @@ export class BitbucketPlatformKit extends PlatformKit { // https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get return values?.find( ({ source, destination }) => - source?.branch?.name === branch && destination?.branch?.name === this.platformConfig.baseBranchName, + source?.branch?.name === branch && destination?.branch?.name === this.platformConfig.baseBranchName ); }) .then((pr) => pr?.id); diff --git a/action/src/platforms/git-utils.ts b/action/src/platforms/git-utils.ts new file mode 100644 index 000000000..edbacc305 --- /dev/null +++ b/action/src/platforms/git-utils.ts @@ -0,0 +1,16 @@ +import { execSync } from "child_process"; + +export function configureGitCredentials(token: string | undefined, repoUrl: string) { + if (!token) { + console.warn("No token provided. Skipping Git credential configuration."); + return false; + } + try { + execSync(`git remote set-url origin ${repoUrl}`, { stdio: "inherit" }); + + return true; + } catch (error: any) { + console.error(`Failed to configure Git credentials: ${error.message}`); + return false; + } +} diff --git a/action/src/platforms/github.ts b/action/src/platforms/github.ts index 461241603..87557be1e 100644 --- a/action/src/platforms/github.ts +++ b/action/src/platforms/github.ts @@ -1,7 +1,8 @@ import { Octokit } from "octokit"; import { PlatformKit } from "./_base.js"; import Z from "zod"; -import { execSync } from "child_process"; + +import { configureGitCredentials } from "./git-utils.js"; export class GitHubPlatformKit extends PlatformKit { private _octokit?: Octokit; @@ -76,9 +77,9 @@ export class GitHubPlatformKit extends PlatformKit { if (ghToken && processOwnCommits) { console.log("Using provided GH_TOKEN. This will trigger your CI/CD pipeline to run again."); - execSync(`git remote set-url origin https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`, { - stdio: "inherit", - }); + const URL = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`; + + configureGitCredentials(ghToken, URL); } } diff --git a/action/src/platforms/gitlab.ts b/action/src/platforms/gitlab.ts index f2be0adf1..1f457235c 100644 --- a/action/src/platforms/gitlab.ts +++ b/action/src/platforms/gitlab.ts @@ -1,7 +1,7 @@ import { Gitlab } from "@gitbeaker/rest"; import Z from "zod"; import { PlatformKit } from "./_base.js"; -import { execSync } from "child_process"; +import { configureGitCredentials } from "./git-utils.js"; const gl = new Gitlab({ token: "" }); @@ -81,7 +81,7 @@ export class GitlabPlatformKit extends PlatformKit { title, { description: body, - }, + } ); return mr.iid; } @@ -91,10 +91,9 @@ export class GitlabPlatformKit extends PlatformKit { } gitConfig(): Promise | void { - const url = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; - execSync(`git remote set-url origin ${url}`, { - stdio: "inherit", - }); + const URL = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; + + configureGitCredentials(this.platformConfig.glToken, URL); } buildPullRequestUrl(pullRequestNumber: number): string { From 6d3272ce5571e19e4ba36c97c081850e0bdf6a07 Mon Sep 17 00:00:00 2001 From: sudhanshu20204 Date: Wed, 26 Mar 2025 22:27:43 +0530 Subject: [PATCH 2/4] revert: multi-platform refactoring to align with issue changes requested --- action/src/platforms/git-utils.ts | 16 ---------------- action/src/platforms/github.ts | 8 +++++--- action/src/platforms/gitlab.ts | 8 +++++--- 3 files changed, 10 insertions(+), 22 deletions(-) delete mode 100644 action/src/platforms/git-utils.ts diff --git a/action/src/platforms/git-utils.ts b/action/src/platforms/git-utils.ts deleted file mode 100644 index edbacc305..000000000 --- a/action/src/platforms/git-utils.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { execSync } from "child_process"; - -export function configureGitCredentials(token: string | undefined, repoUrl: string) { - if (!token) { - console.warn("No token provided. Skipping Git credential configuration."); - return false; - } - try { - execSync(`git remote set-url origin ${repoUrl}`, { stdio: "inherit" }); - - return true; - } catch (error: any) { - console.error(`Failed to configure Git credentials: ${error.message}`); - return false; - } -} diff --git a/action/src/platforms/github.ts b/action/src/platforms/github.ts index 87557be1e..0ebf355e2 100644 --- a/action/src/platforms/github.ts +++ b/action/src/platforms/github.ts @@ -2,7 +2,7 @@ import { Octokit } from "octokit"; import { PlatformKit } from "./_base.js"; import Z from "zod"; -import { configureGitCredentials } from "./git-utils.js"; +import { execSync } from "child_process"; export class GitHubPlatformKit extends PlatformKit { private _octokit?: Octokit; @@ -77,9 +77,11 @@ export class GitHubPlatformKit extends PlatformKit { if (ghToken && processOwnCommits) { console.log("Using provided GH_TOKEN. This will trigger your CI/CD pipeline to run again."); - const URL = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`; + const url = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`; - configureGitCredentials(ghToken, URL); + execSync(`git remote set-url origin ${url}`, { + stdio: "inherit", + }); } } diff --git a/action/src/platforms/gitlab.ts b/action/src/platforms/gitlab.ts index 1f457235c..bff85084b 100644 --- a/action/src/platforms/gitlab.ts +++ b/action/src/platforms/gitlab.ts @@ -1,7 +1,7 @@ import { Gitlab } from "@gitbeaker/rest"; import Z from "zod"; import { PlatformKit } from "./_base.js"; -import { configureGitCredentials } from "./git-utils.js"; +import { execSync } from "child_process"; const gl = new Gitlab({ token: "" }); @@ -91,9 +91,11 @@ export class GitlabPlatformKit extends PlatformKit { } gitConfig(): Promise | void { - const URL = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; + const url = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; - configureGitCredentials(this.platformConfig.glToken, URL); + execSync(`git remote set-url origin ${url}`, { + stdio: "inherit", + }); } buildPullRequestUrl(pullRequestNumber: number): string { From 1e69d1a9f3c4f83fedde795e30ec74de19dac303 Mon Sep 17 00:00:00 2001 From: sudhanshu20204 Date: Thu, 27 Mar 2025 22:38:16 +0530 Subject: [PATCH 3/4] fix(action): refactor gitConfig to reduce redundancy for GitHub, GitLab, and BitBucket --- action/src/platforms/_base.ts | 9 ++++++++- action/src/platforms/github.ts | 4 +--- action/src/platforms/gitlab.ts | 7 +++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/action/src/platforms/_base.ts b/action/src/platforms/_base.ts index d1e1fe7b2..8a58a9aa0 100644 --- a/action/src/platforms/_base.ts +++ b/action/src/platforms/_base.ts @@ -1,3 +1,4 @@ +import { execSync } from "child_process"; import Z from "zod"; const defaultMessage = "feat: update translations via @lingodotdev"; @@ -23,7 +24,13 @@ export abstract class PlatformKit | void {} + gitConfig(token?: string, repoUrl?: string) { + if (token && repoUrl) { + execSync(`git remote set-url origin ${repoUrl}`, { + stdio: "inherit", + }); + } + } get config() { const env = Z.object({ diff --git a/action/src/platforms/github.ts b/action/src/platforms/github.ts index 0ebf355e2..0c87a5f40 100644 --- a/action/src/platforms/github.ts +++ b/action/src/platforms/github.ts @@ -79,9 +79,7 @@ export class GitHubPlatformKit extends PlatformKit { const url = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`; - execSync(`git remote set-url origin ${url}`, { - stdio: "inherit", - }); + super.gitConfig(ghToken, url); } } diff --git a/action/src/platforms/gitlab.ts b/action/src/platforms/gitlab.ts index bff85084b..1172379fa 100644 --- a/action/src/platforms/gitlab.ts +++ b/action/src/platforms/gitlab.ts @@ -91,11 +91,10 @@ export class GitlabPlatformKit extends PlatformKit { } gitConfig(): Promise | void { - const url = `https://oauth2:${this.platformConfig.glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; + const glToken = this.platformConfig.glToken; + const url = `https://oauth2:${glToken}@gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}.git`; - execSync(`git remote set-url origin ${url}`, { - stdio: "inherit", - }); + super.gitConfig(glToken, url); } buildPullRequestUrl(pullRequestNumber: number): string { From 0658bc84e7ea0a5d3fc88cb62a4e67bd6e4e6930 Mon Sep 17 00:00:00 2001 From: sudhanshu20204 Date: Thu, 27 Mar 2025 22:42:27 +0530 Subject: [PATCH 4/4] refactor: remove unnecessary imports --- action/src/platforms/github.ts | 2 -- action/src/platforms/gitlab.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/action/src/platforms/github.ts b/action/src/platforms/github.ts index 0c87a5f40..4a8234b13 100644 --- a/action/src/platforms/github.ts +++ b/action/src/platforms/github.ts @@ -2,8 +2,6 @@ import { Octokit } from "octokit"; import { PlatformKit } from "./_base.js"; import Z from "zod"; -import { execSync } from "child_process"; - export class GitHubPlatformKit extends PlatformKit { private _octokit?: Octokit; diff --git a/action/src/platforms/gitlab.ts b/action/src/platforms/gitlab.ts index 1172379fa..787a909ea 100644 --- a/action/src/platforms/gitlab.ts +++ b/action/src/platforms/gitlab.ts @@ -1,7 +1,6 @@ import { Gitlab } from "@gitbeaker/rest"; import Z from "zod"; import { PlatformKit } from "./_base.js"; -import { execSync } from "child_process"; const gl = new Gitlab({ token: "" });