diff --git a/.changeset/large-pears-wonder.md b/.changeset/large-pears-wonder.md new file mode 100644 index 000000000..80656f48b --- /dev/null +++ b/.changeset/large-pears-wonder.md @@ -0,0 +1,5 @@ +--- +"lingo.dev": patch +--- + +[Refactor]: This PR creates a git-utils file in the /action/src to remove the redundancy inside the gitConfig() function of Github, Gitlab and Bitbucket. This PR refactors the code to reduce redundancy of the code. diff --git a/action/src/platforms/bitbucket.ts b/action/src/platforms/bitbucket.ts index 04f1058fc..f056ca04c 100644 --- a/action/src/platforms/bitbucket.ts +++ b/action/src/platforms/bitbucket.ts @@ -2,6 +2,7 @@ import { execSync } from "child_process"; import bbLib from "bitbucket"; import Z from "zod"; import { PlatformKit } from "./_base.js"; +import { configureBitbucketProxy } from "./git-utils.js"; const { Bitbucket } = bbLib; @@ -92,14 +93,13 @@ export class BitbucketPlatformKit extends PlatformKit { } async gitConfig() { - execSync("git config --unset http.${BITBUCKET_GIT_HTTP_ORIGIN}.proxy", { - stdio: "inherit", - }); - execSync("git config http.${BITBUCKET_GIT_HTTP_ORIGIN}.proxy http://host.docker.internal:29418/", { - stdio: "inherit", - }); + const origin = process.env.BITBUCKET_GIT_HTTP_ORIGIN || "origin"; + const proxyUrl = "http://host.docker.internal:29418/"; + + // Using the imported configureBitbucketProxy function + configureBitbucketProxy(origin, proxyUrl); } - + get platformConfig() { const env = Z.object({ BITBUCKET_BRANCH: Z.string(), diff --git a/action/src/platforms/git-utils.ts b/action/src/platforms/git-utils.ts new file mode 100644 index 000000000..d3f60f9ad --- /dev/null +++ b/action/src/platforms/git-utils.ts @@ -0,0 +1,34 @@ +import { execSync } from "child_process"; + +export function configureGitCredentials(token: string, repoUrl: string): boolean { + if (!token) { + console.error("Missing token. Unable to configure Git credentials."); + return false; + } + try { + execSync(`git remote set-url origin ${repoUrl}`, { stdio: "inherit" }); + console.log("Git credentials configured successfully."); + return true; + } catch (error) { + if (error instanceof Error) { + console.error(`Failed to configure git credentials: ${error.message}`); + } else { + console.error("An unknown error occurred while configuring Git credentials.", error); + } + return false; + } +} + +export function configureBitbucketProxy(origin: string, proxyUrl: string): void { + try { + execSync(`git config --unset http.${origin}.proxy`, { stdio: "inherit" }); + execSync(`git config http.${origin}.proxy ${proxyUrl}`, { stdio: "inherit" }); + console.log("Bitbucket proxy configured successfully."); + } catch (error) { + if (error instanceof Error) { + console.error(`Failed to configure Bitbucket proxy: ${error.message}`); + } else { + console.error("An unknown error occurred while configuring the Bitbucket proxy.", error); + } + } +} diff --git a/action/src/platforms/github.ts b/action/src/platforms/github.ts index 461241603..42e0d442f 100644 --- a/action/src/platforms/github.ts +++ b/action/src/platforms/github.ts @@ -2,6 +2,7 @@ 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; @@ -72,13 +73,15 @@ export class GitHubPlatformKit extends PlatformKit { async gitConfig() { const { ghToken, repositoryOwner, repositoryName } = this.platformConfig; const { processOwnCommits } = this.config; - + 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 repoUrl = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`; + + const success = configureGitCredentials(ghToken, repoUrl); + if (!success) { + console.error("Failed to configure GitHub credentials."); + } } } diff --git a/action/src/platforms/gitlab.ts b/action/src/platforms/gitlab.ts index f2be0adf1..ebdb1389e 100644 --- a/action/src/platforms/gitlab.ts +++ b/action/src/platforms/gitlab.ts @@ -2,7 +2,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: "" }); export class GitlabPlatformKit extends PlatformKit { @@ -91,11 +91,21 @@ 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 { glToken, repositoryOwner, repositoryName } = this.platformConfig; + + if (!glToken) { + console.error("GitLab token is missing."); + return; + } + + const repoUrl = `https://oauth2:${glToken}@gitlab.com/${repositoryOwner}/${repositoryName}.git`; + const success = configureGitCredentials(glToken, repoUrl); + + if (!success) { + console.error("Failed to configure GitLab credentials."); + } } + buildPullRequestUrl(pullRequestNumber: number): string { return `https://gitlab.com/${this.platformConfig.repositoryOwner}/${this.platformConfig.repositoryName}/-/merge_requests/${pullRequestNumber}`;