mirror of
https://github.com/kiegroup/git-backporting.git
synced 2025-06-28 05:33:47 +00:00
fix(gh-96): fix git token parsing (#98)
This commit is contained in:
parent
2c5c54654d
commit
c57fca6bd6
14 changed files with 324 additions and 342 deletions
|
@ -1,8 +1,7 @@
|
|||
import { Args } from "@bp/service/args/args.types";
|
||||
import { AuthTokenId, Configs } from "@bp/service/configs/configs.types";
|
||||
import { Configs } from "@bp/service/configs/configs.types";
|
||||
import LoggerService from "../logger/logger-service";
|
||||
import LoggerServiceFactory from "../logger/logger-service-factory";
|
||||
import { GitClientType } from "../git/git.types";
|
||||
|
||||
/**
|
||||
* Abstract configuration parser class in charge to parse
|
||||
|
@ -35,42 +34,4 @@ import { GitClientType } from "../git/git.types";
|
|||
|
||||
return Promise.resolve(configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the git token from env variable, the default is taken from GIT_TOKEN env.
|
||||
* All specific git env variable have precedence and override the default one.
|
||||
* @param gitType
|
||||
* @returns tuple where
|
||||
* - the first element is the corresponding env value
|
||||
* - the second element is true if the value is not undefined nor empty
|
||||
*/
|
||||
public getGitTokenFromEnv(gitType: GitClientType): string | undefined {
|
||||
let [token] = this.getEnv(AuthTokenId.GIT_TOKEN);
|
||||
let [specToken, specOk]: [string | undefined, boolean] = [undefined, false];
|
||||
if (GitClientType.GITHUB == gitType) {
|
||||
[specToken, specOk] = this.getEnv(AuthTokenId.GITHUB_TOKEN);
|
||||
} else if (GitClientType.GITLAB == gitType) {
|
||||
[specToken, specOk] = this.getEnv(AuthTokenId.GITLAB_TOKEN);
|
||||
} else if (GitClientType.CODEBERG == gitType) {
|
||||
[specToken, specOk] = this.getEnv(AuthTokenId.CODEBERG_TOKEN);
|
||||
}
|
||||
|
||||
if (specOk) {
|
||||
token = specToken;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get process env variable given the input key string
|
||||
* @param key
|
||||
* @returns tuple where
|
||||
* - the first element is the corresponding env value
|
||||
* - the second element is true if the value is not undefined nor empty
|
||||
*/
|
||||
public getEnv(key: string): [string | undefined, boolean] {
|
||||
const val = process.env[key];
|
||||
return [val, val !== undefined && val !== ""];
|
||||
}
|
||||
}
|
|
@ -33,19 +33,9 @@ export default class PullRequestConfigsParser extends ConfigsParser {
|
|||
throw new Error(`The number of backport branch names, if provided, must match the number of target branches or just one, provided ${bpBranchNames.length} branch names instead`);
|
||||
}
|
||||
|
||||
// setup the auth token
|
||||
let token = args.auth;
|
||||
if (token === undefined) {
|
||||
this.logger.info("Auth argument not provided, checking available tokens from env..");
|
||||
token = this.getGitTokenFromEnv(this.gitClient.getClientType());
|
||||
if (!token) {
|
||||
this.logger.info("Git token not set in the env");
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
dryRun: args.dryRun!,
|
||||
auth: token,
|
||||
auth: args.auth, // this has been already pre-processed before parsing configs
|
||||
folder: `${folder.startsWith("/") ? "" : process.cwd() + "/"}${args.folder ?? this.getDefaultFolder()}`,
|
||||
mergeStrategy: args.strategy,
|
||||
mergeStrategyOption: args.strategyOption,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { GitClientType } from "@bp/service/git/git.types";
|
||||
import { AuthTokenId } from "@bp/service/configs/configs.types";
|
||||
|
||||
const PUBLIC_GITHUB_URL = "https://github.com";
|
||||
const PUBLIC_GITHUB_API = "https://api.github.com";
|
||||
|
@ -38,4 +39,42 @@ export const inferGitApiUrl = (prUrl: string, apiVersion = "v4"): string => {
|
|||
}
|
||||
|
||||
return `${baseUrl}/api/${apiVersion}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the git token from env variable, the default is taken from GIT_TOKEN env.
|
||||
* All specific git env variable have precedence and override the default one.
|
||||
* @param gitType
|
||||
* @returns tuple where
|
||||
* - the first element is the corresponding env value
|
||||
* - the second element is true if the value is not undefined nor empty
|
||||
*/
|
||||
export const getGitTokenFromEnv = (gitType: GitClientType): string | undefined => {
|
||||
let [token] = getEnv(AuthTokenId.GIT_TOKEN);
|
||||
let [specToken, specOk]: [string | undefined, boolean] = [undefined, false];
|
||||
if (GitClientType.GITHUB == gitType) {
|
||||
[specToken, specOk] = getEnv(AuthTokenId.GITHUB_TOKEN);
|
||||
} else if (GitClientType.GITLAB == gitType) {
|
||||
[specToken, specOk] = getEnv(AuthTokenId.GITLAB_TOKEN);
|
||||
} else if (GitClientType.CODEBERG == gitType) {
|
||||
[specToken, specOk] = getEnv(AuthTokenId.CODEBERG_TOKEN);
|
||||
}
|
||||
|
||||
if (specOk) {
|
||||
token = specToken;
|
||||
}
|
||||
|
||||
return token;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get process env variable given the input key string
|
||||
* @param key
|
||||
* @returns tuple where
|
||||
* - the first element is the corresponding env value
|
||||
* - the second element is true if the value is not undefined nor empty
|
||||
*/
|
||||
export const getEnv = (key: string): [string | undefined, boolean] => {
|
||||
const val = process.env[key];
|
||||
return [val, val !== undefined && val !== ""];
|
||||
};
|
|
@ -8,7 +8,7 @@ import GitClientFactory from "@bp/service/git/git-client-factory";
|
|||
import { BackportPullRequest, GitClientType, GitPullRequest } from "@bp/service/git/git.types";
|
||||
import LoggerService from "@bp/service/logger/logger-service";
|
||||
import LoggerServiceFactory from "@bp/service/logger/logger-service-factory";
|
||||
import { inferGitClient, inferGitApiUrl } from "@bp/service/git/git-util";
|
||||
import { inferGitClient, inferGitApiUrl, getGitTokenFromEnv } from "@bp/service/git/git-util";
|
||||
|
||||
interface Git {
|
||||
gitClientType: GitClientType;
|
||||
|
@ -63,10 +63,12 @@ export default class Runner {
|
|||
const gitClientType: GitClientType = inferGitClient(args.pullRequest);
|
||||
// the api version is ignored in case of github
|
||||
const apiUrl = inferGitApiUrl(args.pullRequest, gitClientType === GitClientType.CODEBERG ? "v1" : undefined);
|
||||
const gitApi: GitClient = GitClientFactory.getOrCreate(gitClientType, args.auth, apiUrl);
|
||||
const token = this.fetchToken(args, gitClientType);
|
||||
const gitApi: GitClient = GitClientFactory.getOrCreate(gitClientType, token, apiUrl);
|
||||
|
||||
// 3. parse configs
|
||||
this.logger.debug("Parsing configs..");
|
||||
args.auth = token; // override auth
|
||||
const configs: Configs = await new PullRequestConfigsParser().parseAndValidate(args);
|
||||
const backportPRs: BackportPullRequest[] = configs.backportPullRequests;
|
||||
|
||||
|
@ -94,6 +96,27 @@ export default class Runner {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the GIT token from the provided Args obj, if not empty, otherwise fallback
|
||||
* to the environment variables.
|
||||
* @param args input arguments
|
||||
* @param gitType git client type
|
||||
* @returns the provided or fetched token, or undefined if not set anywhere
|
||||
*/
|
||||
fetchToken(args: Args, gitType: GitClientType): string | undefined {
|
||||
let token = args.auth;
|
||||
if (token === undefined) {
|
||||
// try to fetch the auth from env variable
|
||||
this.logger.info("Auth argument not provided, checking available tokens from env..");
|
||||
token = getGitTokenFromEnv(gitType);
|
||||
if (!token) {
|
||||
this.logger.info("Git token not found in the environment");
|
||||
}
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
async executeBackport(configs: Configs, backportPR: BackportPullRequest, git: Git): Promise<void> {
|
||||
this.logger.setContext(backportPR.base);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue