Compare commits

...

3 Commits

2 changed files with 32 additions and 10 deletions

View File

@ -3,16 +3,6 @@ const {isNil} = require('lodash');
const hostedGitInfo = require('hosted-git-info'); const hostedGitInfo = require('hosted-git-info');
const {verifyAuth} = require('./git'); const {verifyAuth} = require('./git');
const GIT_TOKENS = {
GIT_CREDENTIALS: undefined,
GH_TOKEN: undefined,
GITHUB_TOKEN: undefined,
GL_TOKEN: 'gitlab-ci-token:',
GITLAB_TOKEN: 'gitlab-ci-token:',
BB_TOKEN: 'x-token-auth:',
BITBUCKET_TOKEN: 'x-token-auth:',
};
/** /**
* Determine the the git repository URL to use to push, either: * Determine the the git repository URL to use to push, either:
* - The `repositoryUrl` as is if allowed to push * - The `repositoryUrl` as is if allowed to push
@ -25,6 +15,18 @@ const GIT_TOKENS = {
* @return {String} The formatted Git repository URL. * @return {String} The formatted Git repository URL.
*/ */
module.exports = async ({cwd, env, options: {repositoryUrl, branch}}) => { module.exports = async ({cwd, env, options: {repositoryUrl, branch}}) => {
const GIT_TOKENS = {
GIT_CREDENTIALS: undefined,
GH_TOKEN: undefined,
// GitHub Actions require the "x-access-token:" prefix for git access
// https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation
GITHUB_TOKEN: isNil(env.GITHUB_ACTION) ? undefined : 'x-access-token:',
GL_TOKEN: 'gitlab-ci-token:',
GITLAB_TOKEN: 'gitlab-ci-token:',
BB_TOKEN: 'x-token-auth:',
BITBUCKET_TOKEN: 'x-token-auth:',
};
const info = hostedGitInfo.fromUrl(repositoryUrl, {noGitPlus: true}); const info = hostedGitInfo.fromUrl(repositoryUrl, {noGitPlus: true});
const {protocol, ...parsed} = parse(repositoryUrl); const {protocol, ...parsed} = parse(repositoryUrl);
@ -43,6 +45,13 @@ module.exports = async ({cwd, env, options: {repositoryUrl, branch}}) => {
const envVar = Object.keys(GIT_TOKENS).find(envVar => !isNil(env[envVar])); const envVar = Object.keys(GIT_TOKENS).find(envVar => !isNil(env[envVar]));
const gitCredentials = `${GIT_TOKENS[envVar] || ''}${env[envVar] || ''}`; const gitCredentials = `${GIT_TOKENS[envVar] || ''}${env[envVar] || ''}`;
console.log(`-------- envVar -------- `);
console.log(envVar);
console.log(`-------- GIT_TOKENS[envVar] -------- `);
console.log(GIT_TOKENS[envVar]);
console.log(`-------- env[envVar].substr(5) -------- `);
console.log((env[envVar] || '').substr(5));
if (gitCredentials) { if (gitCredentials) {
// If credentials are set via environment variables, convert the URL to http/https and add basic auth, otherwise return `repositoryUrl` as is // If credentials are set via environment variables, convert the URL to http/https and add basic auth, otherwise return `repositoryUrl` as is
const [match, auth, host, path] = /^(?!.+:\/\/)(?:(.*)@)?(.*?):(.*)$/.exec(repositoryUrl) || []; const [match, auth, host, path] = /^(?!.+:\/\/)(?:(.*)@)?(.*?):(.*)$/.exec(repositoryUrl) || [];

View File

@ -244,6 +244,19 @@ test('Return the "https" formatted URL if "gitCredentials" is defined with "BITB
); );
}); });
test('Return the "https" formatted URL if "GITHUB_ACTION" is set', async t => {
const {cwd} = await gitRepo();
t.is(
await getAuthUrl({
cwd,
env: {...env, GITHUB_ACTION: 'foo', GITHUB_TOKEN: 'token'},
options: {branch: 'master', repositoryUrl: 'git@host.null:owner/repo.git'},
}),
'https://x-access-token:token@host.null/owner/repo.git'
);
});
test('Handle "https" URL with group and subgroup, with "GIT_CREDENTIALS"', async t => { test('Handle "https" URL with group and subgroup, with "GIT_CREDENTIALS"', async t => {
const {cwd} = await gitRepo(); const {cwd} = await gitRepo();