mirror of
https://github.com/renovatebot/renovate.git
synced 2026-11-02 10:33:39 +00:00
87c5e8e022
Co-authored-by: Rhys Arkins <rhys@arkins.net>
42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import { logger } from '../../logger';
|
|
import { getHttpUrl } from './url';
|
|
|
|
/**
|
|
* Add authorization to a Git Url and returns a new environment variables object
|
|
* @returns a new NodeJS.ProcessEnv object without modifying any input parameters
|
|
*/
|
|
export function getGitAuthenticatedEnvironmentVariables(
|
|
gitUrl: string,
|
|
token: string,
|
|
environmentVariables?: NodeJS.ProcessEnv
|
|
): NodeJS.ProcessEnv {
|
|
// check if the environmentVariables already contain a GIT_CONFIG_COUNT or if the process has one
|
|
const gitConfigCountEnvVariable =
|
|
environmentVariables?.GIT_CONFIG_COUNT || process.env.GIT_CONFIG_COUNT;
|
|
let gitConfigCount = 0;
|
|
if (gitConfigCountEnvVariable) {
|
|
// passthrough the gitConfigCountEnvVariable environment variable as start value of the index count
|
|
gitConfigCount = parseInt(gitConfigCountEnvVariable, 10);
|
|
if (Number.isNaN(gitConfigCount)) {
|
|
logger.warn(
|
|
`Found GIT_CONFIG_COUNT env variable, but couldn't parse the value to an integer: ${process.env.GIT_CONFIG_COUNT}. Ignoring it.`
|
|
);
|
|
gitConfigCount = 0;
|
|
}
|
|
}
|
|
|
|
const gitUrlWithToken = getHttpUrl(gitUrl, token);
|
|
|
|
// create a shallow copy of the environmentVariables as base so we don't modify the input parameter object
|
|
// add the two new config key and value to the returnEnvironmentVariables object
|
|
// increase the CONFIG_COUNT by one and add it to the object
|
|
const returnEnvironmentVariables = {
|
|
...environmentVariables,
|
|
[`GIT_CONFIG_KEY_${gitConfigCount}`]: `url.${gitUrlWithToken}.insteadOf`,
|
|
[`GIT_CONFIG_VALUE_${gitConfigCount}`]: gitUrl,
|
|
GIT_CONFIG_COUNT: (gitConfigCount + 1).toString(),
|
|
};
|
|
|
|
return returnEnvironmentVariables;
|
|
}
|