git-backporting/src/service/git/git-util.ts
2024-02-23 15:13:34 +01:00

80 lines
No EOL
2.8 KiB
TypeScript

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";
/**
* Infer the remote GIT service to interact with based on the provided
* pull request URL
* @param prUrl provided pull request URL
* @returns {GitClientType}
*/
export const inferGitClient = (prUrl: string): GitClientType => {
const stdPrUrl = prUrl.toLowerCase().trim();
if (stdPrUrl.includes(GitClientType.GITHUB.toString())) {
return GitClientType.GITHUB;
} else if (stdPrUrl.includes(GitClientType.GITLAB.toString())) {
return GitClientType.GITLAB;
} else if (stdPrUrl.includes(GitClientType.CODEBERG.toString())) {
return GitClientType.CODEBERG;
}
throw new Error(`Remote git service not recognized from pr url: ${prUrl}`);
};
/**
* Infer the host git service from the pull request url
* @param prUrl pull/merge request url
* @param apiVersion the api version, ignored in case of public github
* @returns api URL like https://api.github.com or https://gitlab.com/api/v4
*/
export const inferGitApiUrl = (prUrl: string, apiVersion = "v4"): string => {
const url = new URL(prUrl);
const baseUrl = `${url.protocol}//${url.host}`;
if (baseUrl.includes(PUBLIC_GITHUB_URL) || baseUrl.includes(PUBLIC_GITHUB_API)) {
return PUBLIC_GITHUB_API;
}
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 !== ""];
};