Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 21 additions & 7 deletions action/src/flows/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for running the prettier!

);
const existingPrNumber = await this.platformKit.getOpenPullRequestNumber({
branch: i18nBranchName,
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion action/src/platforms/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class BitbucketPlatformKit extends PlatformKit<BitbucketConfig> {
// 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);
Expand Down
16 changes: 16 additions & 0 deletions action/src/platforms/git-utils.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's not add a new utils file. Parent PlatformKit class has gitConfig() method that can be used for any common logic using super.gitConfig(...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Issue #573 mentioned creating a new file, so should I revert the multi-platform refactoring change?

Copy link
Copy Markdown
Contributor

@mathio mathio Mar 26, 2025

Choose a reason for hiding this comment

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

I am not saying not to do it, just to do it a bit differently than what the issue suggests.

However lets keep the PRs one issue at a time. This looks good now.

Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 5 additions & 4 deletions action/src/platforms/github.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's use lowercase "url" for variables like this:

Suggested change
const URL = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`;
const url = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`;


configureGitCredentials(ghToken, URL);
}
}

Expand Down
11 changes: 5 additions & 6 deletions action/src/platforms/gitlab.ts
Original file line number Diff line number Diff line change
@@ -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: "" });

Expand Down Expand Up @@ -81,7 +81,7 @@ export class GitlabPlatformKit extends PlatformKit {
title,
{
description: body,
},
}
);
return mr.iid;
}
Expand All @@ -91,10 +91,9 @@ export class GitlabPlatformKit extends PlatformKit {
}

gitConfig(): Promise<void> | 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 {
Expand Down