Skip to content

Commit

Permalink
feat: add --git-client to explicitly set the type of forge
Browse files Browse the repository at this point in the history
codeberg is running Forgejo and it may not be possible to infer that
from the URL alone. The same is true for GitHub Enterprise Server.
  • Loading branch information
Jean Daricade committed Mar 22, 2024
1 parent 646d8fe commit 620c485
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
14 changes: 12 additions & 2 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ArgsParser {
dryRun: this.getOrDefault(args.dryRun, false),
auth: this.getOrDefault(args.auth),
folder: this.getOrDefault(args.folder),
gitClient: this.getOrDefault(args.gitClient),
gitUser: this.getOrDefault(args.gitUser),
gitEmail: this.getOrDefault(args.gitEmail),
title: this.getOrDefault(args.title),
Expand Down Expand Up @@ -183,6 +184,7 @@ class CLIArgsParser extends args_parser_1.default {
.option("-pr, --pull-request <pr-url>", "pull request url, e.g., https://github.com/kiegroup/git-backporting/pull/1")
.option("-d, --dry-run", "if enabled the tool does not create any pull request nor push anything remotely")
.option("-a, --auth <auth>", "git authentication string, if not provided fallback by looking for existing env variables like GITHUB_TOKEN")
.option("--git-client <github|gitlab|codeberg>", "git client type, if not set it is infered from --pull-request")
.option("-gu, --git-user <git-user>", "local git user name, default is 'GitHub'")
.option("-ge, --git-email <git-email>", "local git user email, default is 'noreply@github.com'")
.option("-f, --folder <folder>", "local folder where the repo will be checked out, e.g., /tmp/folder")
Expand Down Expand Up @@ -217,6 +219,7 @@ class CLIArgsParser extends args_parser_1.default {
pullRequest: opts.pullRequest,
targetBranch: opts.targetBranch,
folder: opts.folder,
gitClient: opts.gitClient,
gitUser: opts.gitUser,
gitEmail: opts.gitEmail,
title: opts.title,
Expand Down Expand Up @@ -1343,7 +1346,13 @@ class Runner {
this.logger.warn("Dry run enabled");
}
// 2. init git service
const gitClientType = (0, git_util_1.inferGitClient)(args.pullRequest);
let gitClientType;
if (args.gitClient === undefined) {
gitClientType = (0, git_util_1.inferGitClient)(args.pullRequest);
}
else {
gitClientType = args.gitClient;
}
// the api version is ignored in case of github
const apiUrl = (0, git_util_1.inferGitApiUrl)(args.pullRequest, gitClientType === git_types_1.GitClientType.CODEBERG ? "v1" : undefined);
const token = this.fetchToken(args, gitClientType);
Expand Down Expand Up @@ -5882,6 +5891,7 @@ var preservedUrlFields = [
"protocol",
"query",
"search",
"hash",
];

// Create handlers that pass events from native requests
Expand Down Expand Up @@ -6315,7 +6325,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
redirectUrl.protocol !== "https:" ||
redirectUrl.host !== currentHost &&
!isSubdomain(redirectUrl.host, currentHost)) {
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
}

// Evaluate the beforeRedirect callback
Expand Down
13 changes: 11 additions & 2 deletions dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ArgsParser {
dryRun: this.getOrDefault(args.dryRun, false),
auth: this.getOrDefault(args.auth),
folder: this.getOrDefault(args.folder),
gitClient: this.getOrDefault(args.gitClient),
gitUser: this.getOrDefault(args.gitUser),
gitEmail: this.getOrDefault(args.gitEmail),
title: this.getOrDefault(args.title),
Expand Down Expand Up @@ -187,6 +188,7 @@ class GHAArgsParser extends args_parser_1.default {
pullRequest: (0, core_1.getInput)("pull-request"),
targetBranch: (0, core_1.getInput)("target-branch"),
folder: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("folder")),
gitClient: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("git-client")),
gitUser: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("git-user")),
gitEmail: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("git-email")),
title: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("title")),
Expand Down Expand Up @@ -1313,7 +1315,13 @@ class Runner {
this.logger.warn("Dry run enabled");
}
// 2. init git service
const gitClientType = (0, git_util_1.inferGitClient)(args.pullRequest);
let gitClientType;
if (args.gitClient === undefined) {
gitClientType = (0, git_util_1.inferGitClient)(args.pullRequest);
}
else {
gitClientType = args.gitClient;
}
// the api version is ignored in case of github
const apiUrl = (0, git_util_1.inferGitApiUrl)(args.pullRequest, gitClientType === git_types_1.GitClientType.CODEBERG ? "v1" : undefined);
const token = this.fetchToken(args, gitClientType);
Expand Down Expand Up @@ -7613,6 +7621,7 @@ var preservedUrlFields = [
"protocol",
"query",
"search",
"hash",
];

// Create handlers that pass events from native requests
Expand Down Expand Up @@ -8046,7 +8055,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
redirectUrl.protocol !== "https:" ||
redirectUrl.host !== currentHost &&
!isSubdomain(redirectUrl.host, currentHost)) {
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
}

// Evaluate the beforeRedirect callback
Expand Down
1 change: 1 addition & 0 deletions src/service/args/args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default abstract class ArgsParser {
dryRun: this.getOrDefault(args.dryRun, false),
auth: this.getOrDefault(args.auth),
folder: this.getOrDefault(args.folder),
gitClient: this.getOrDefault(args.gitClient),
gitUser: this.getOrDefault(args.gitUser),
gitEmail: this.getOrDefault(args.gitEmail),
title: this.getOrDefault(args.title),
Expand Down
1 change: 1 addition & 0 deletions src/service/args/args.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Args {
dryRun?: boolean, // if enabled do not push anything remotely
auth?: string, // git service auth, like github token
folder?: string, // local folder where the repositories should be cloned
gitClient?: string, // git client
gitUser?: string, // local git user, default 'GitHub'
gitEmail?: string, // local git email, default 'noreply@github.com'
title?: string, // backport pr title, default original pr title prefixed by target branch
Expand Down
2 changes: 2 additions & 0 deletions src/service/args/cli/cli-args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class CLIArgsParser extends ArgsParser {
.option("-pr, --pull-request <pr-url>", "pull request url, e.g., https://github.com/kiegroup/git-backporting/pull/1")
.option("-d, --dry-run", "if enabled the tool does not create any pull request nor push anything remotely")
.option("-a, --auth <auth>", "git authentication string, if not provided fallback by looking for existing env variables like GITHUB_TOKEN")
.option("--git-client <github|gitlab|codeberg>", "git client type, if not set it is infered from --pull-request")
.option("-gu, --git-user <git-user>", "local git user name, default is 'GitHub'")
.option("-ge, --git-email <git-email>", "local git user email, default is 'noreply@github.com'")
.option("-f, --folder <folder>", "local folder where the repo will be checked out, e.g., /tmp/folder")
Expand Down Expand Up @@ -49,6 +50,7 @@ export default class CLIArgsParser extends ArgsParser {
pullRequest: opts.pullRequest,
targetBranch: opts.targetBranch,
folder: opts.folder,
gitClient: opts.gitClient,
gitUser: opts.gitUser,
gitEmail: opts.gitEmail,
title: opts.title,
Expand Down
1 change: 1 addition & 0 deletions src/service/args/gha/gha-args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class GHAArgsParser extends ArgsParser {
pullRequest: getInput("pull-request"),
targetBranch: getInput("target-branch"),
folder: getOrUndefined(getInput("folder")),
gitClient: getOrUndefined(getInput("git-client")),
gitUser: getOrUndefined(getInput("git-user")),
gitEmail: getOrUndefined(getInput("git-email")),
title: getOrUndefined(getInput("title")),
Expand Down
7 changes: 6 additions & 1 deletion src/service/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export default class Runner {
}

// 2. init git service
const gitClientType: GitClientType = inferGitClient(args.pullRequest);
let gitClientType: GitClientType;
if (args.gitClient === undefined) {
gitClientType = inferGitClient(args.pullRequest);
} else {
gitClientType = args.gitClient as GitClientType;

Check warning on line 67 in src/service/runner/runner.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 68 in src/service/runner/runner.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
// the api version is ignored in case of github
const apiUrl = inferGitApiUrl(args.pullRequest, gitClientType === GitClientType.CODEBERG ? "v1" : undefined);
const token = this.fetchToken(args, gitClientType);
Expand Down

0 comments on commit 620c485

Please sign in to comment.