From c93775cc382ba1ba27a470333c561d27da45d322 Mon Sep 17 00:00:00 2001 From: pvdlg Date: Tue, 1 May 2018 12:03:04 -0400 Subject: [PATCH] feat: add support for Bitbucket token in environment variables --- docs/usage/ci-configuration.md | 13 +++++++------ lib/get-git-auth-url.js | 16 +++++++++++----- test/get-git-auth-url.test.js | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/docs/usage/ci-configuration.md b/docs/usage/ci-configuration.md index acbe5d49..9da9d514 100644 --- a/docs/usage/ci-configuration.md +++ b/docs/usage/ci-configuration.md @@ -10,13 +10,14 @@ See [CI configuration recipes](../recipes/README.md#ci-configurations) for more **semantic-release** requires push access to the project Git repository in order to create [Git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging). The Git authentication can be set with one of the following environment variables: -| Variable | Description | -|------------------------------|-------------------------------------------------------------------------------------------------------------------------------| -| `GH_TOKEN` or `GITHUB_TOKEN` | A GitHub [personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line). | -| `GL_TOKEN` or `GITLAB_TOKEN` | A GitLab [personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html). | -| `GIT_CREDENTIALS` | [URL encoded basic HTTP Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication#URL_encoding) credentials). | +| Variable | Description | +|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------| +| `GH_TOKEN` or `GITHUB_TOKEN` | A GitHub [personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line). | +| `GL_TOKEN` or `GITLAB_TOKEN` | A GitLab [personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html). | +| `BB_TOKEN` or `BITBUCKET_TOKEN` | A Bitbucket [personal access token](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html). | +| `GIT_CREDENTIALS` | [URL encoded basic HTTP Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication#URL_encoding) credentials). | -`GIT_CREDENTIALS` can be the Git username and password in the format `:` or a token for certain Git providers like [Bitbucket](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html). +`GIT_CREDENTIALS` must be the Git username and password in the format `:`. Alternatively the Git authentication can be set up via [SSH keys](../recipes/git-auth-ssh-keys.md). diff --git a/lib/get-git-auth-url.js b/lib/get-git-auth-url.js index b7e8c555..017dfd9e 100644 --- a/lib/get-git-auth-url.js +++ b/lib/get-git-auth-url.js @@ -4,7 +4,15 @@ const gitUrlParse = require('git-url-parse'); const hostedGitInfo = require('hosted-git-info'); const {verifyAuth} = require('./git'); -const GIT_TOKENS = ['GIT_CREDENTIALS', 'GH_TOKEN', 'GITHUB_TOKEN', 'GL_TOKEN', 'GITLAB_TOKEN']; +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: @@ -36,10 +44,8 @@ module.exports = async ({repositoryUrl, branch}) => { // Test if push is allowed without transforming the URL (e.g. is ssh keys are set up) if (!await verifyAuth(repositoryUrl, branch)) { - const envVar = GIT_TOKENS.find(envVar => !isUndefined(process.env[envVar])); - const gitCredentials = ['GL_TOKEN', 'GITLAB_TOKEN'].includes(envVar) - ? `gitlab-ci-token:${process.env[envVar]}` - : process.env[envVar]; + const envVar = Object.keys(GIT_TOKENS).find(envVar => !isUndefined(process.env[envVar])); + const gitCredentials = `${GIT_TOKENS[envVar] || ''}${process.env[envVar] || ''}`; const {protocols, ...parsed} = gitUrlParse(repositoryUrl); const protocol = protocols.includes('https') ? 'https' : protocols.includes('http') ? 'http' : 'https'; diff --git a/test/get-git-auth-url.test.js b/test/get-git-auth-url.test.js index e05fdd9d..86e2596f 100644 --- a/test/get-git-auth-url.test.js +++ b/test/get-git-auth-url.test.js @@ -13,6 +13,8 @@ test.beforeEach(() => { delete process.env.GITHUB_TOKEN; delete process.env.GL_TOKEN; delete process.env.GITLAB_TOKEN; + delete process.env.BB_TOKEN; + delete process.env.BITBUCKET_TOKEN; process.env.GIT_ASKPASS = 'echo'; process.env.GIT_TERMINAL_PROMPT = 0; }); @@ -152,6 +154,22 @@ test.serial('Return the "https" formatted URL if "gitCredentials" is defined wit ); }); +test.serial('Return the "https" formatted URL if "gitCredentials" is defined with "BB_TOKEN"', async t => { + process.env.BB_TOKEN = 'token'; + t.is( + await getAuthUrl({repositoryUrl: 'git@host.null:owner/repo.git'}), + 'https://x-token-auth:token@host.null/owner/repo.git' + ); +}); + +test.serial('Return the "https" formatted URL if "gitCredentials" is defined with "BITBUCKET_TOKEN"', async t => { + process.env.BITBUCKET_TOKEN = 'token'; + t.is( + await getAuthUrl({repositoryUrl: 'git@host.null:owner/repo.git'}), + 'https://x-token-auth:token@host.null/owner/repo.git' + ); +}); + test.serial('Handle "https" URL with group and subgroup, with "GIT_CREDENTIALS"', async t => { process.env.GIT_CREDENTIALS = 'user:pass'; t.is(