Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-pears-wonder.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 7 additions & 7 deletions action/src/platforms/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,14 +93,13 @@ export class BitbucketPlatformKit extends PlatformKit<BitbucketConfig> {
}

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(),
Expand Down
34 changes: 34 additions & 0 deletions action/src/platforms/git-utils.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
13 changes: 8 additions & 5 deletions action/src/platforms/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
}
}

Expand Down
20 changes: 15 additions & 5 deletions action/src/platforms/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -91,11 +91,21 @@ export class GitlabPlatformKit extends PlatformKit {
}

gitConfig(): Promise<void> | void {
Copy link

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gitConfig function currently has a mixed return type (Promise | void) and relies on console error logging to signal failure. Consider standardizing the return type and error handling approach (for example, by throwing an exception or consistently returning a boolean) across all platform implementations.

Suggested change
gitConfig(): Promise<void> | void {
async gitConfig(): Promise<void> {

Copilot uses AI. Check for mistakes.
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}`;
Expand Down