From 3f272638c24fe8d61f0bce23e69677068902f73f Mon Sep 17 00:00:00 2001 From: Andrew Beresford Date: Mon, 16 Dec 2019 10:32:58 +0000 Subject: [PATCH] Use CI_API_V4_URL in GitLab if it is available --- docs/usage/gitlab.html.md | 7 +++++-- source/platforms/gitlab/GitLabAPI.ts | 12 ++++++++++++ source/platforms/gitlab/_tests/_gitlab_api.test.ts | 12 ++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/usage/gitlab.html.md b/docs/usage/gitlab.html.md index b5e21e063..822d295c8 100644 --- a/docs/usage/gitlab.html.md +++ b/docs/usage/gitlab.html.md @@ -7,11 +7,14 @@ blurb: An overview of using Danger with GitLab, and some examples --- To use Danger JS with GitLab: you'll need to create a new account for Danger to use, then set the following environment -variables on your CI system: +variable on your CI system: -- `DANGER_GITLAB_HOST` = Defaults to `https://gitlab.com` but you can use it for your own url - `DANGER_GITLAB_API_TOKEN` = An access token for the account which will post comments +If you are using a GitLab version prior to 11.7 you will also need to define the following environment variable: + +- `DANGER_GITLAB_HOST` = Defaults to `https://gitlab.com` but you can use it for your own url + Then in your Dangerfiles you will have a fully fleshed out `danger.gitlab` object to work with. For example: ```ts diff --git a/source/platforms/gitlab/GitLabAPI.ts b/source/platforms/gitlab/GitLabAPI.ts index dd1e8a772..272d66862 100644 --- a/source/platforms/gitlab/GitLabAPI.ts +++ b/source/platforms/gitlab/GitLabAPI.ts @@ -25,11 +25,23 @@ export interface GitLabAPICredentials { export function getGitLabAPICredentialsFromEnv(env: Env): GitLabAPICredentials { let host = "https://gitlab.com" const envHost = env["DANGER_GITLAB_HOST"] + const envCIAPI = env["CI_API_V4_URL"] + if (envHost) { // We used to support DANGER_GITLAB_HOST being just the host e.g. "gitlab.com" // however it is possible to have a custom host without SSL, ensure we only add the protocol if one is not provided const protocolRegex = /^https?:\/\//i host = protocolRegex.test(envHost) ? envHost : `https://${envHost}` + } else if (envCIAPI) { + // GitLab >= v11.7 supplies the API Endpoint in an environment variable and we can work out our host value from that. + // See https://docs.gitlab.com/ce/ci/variables/predefined_variables.html + const hostRegex = /^(https?):\/\/([^\/]+)\//i + if (hostRegex.test(envCIAPI)) { + const matches = hostRegex.exec(envCIAPI)! + const matchProto = matches[1] + const matchHost = matches[2] + host = `${matchProto}://${matchHost}` + } } return { diff --git a/source/platforms/gitlab/_tests/_gitlab_api.test.ts b/source/platforms/gitlab/_tests/_gitlab_api.test.ts index 05f3bb0e1..cc2fb45ac 100644 --- a/source/platforms/gitlab/_tests/_gitlab_api.test.ts +++ b/source/platforms/gitlab/_tests/_gitlab_api.test.ts @@ -35,6 +35,18 @@ describe("GitLab API", () => { ) }) + it("configures host from CI_API_V4_URL", () => { + api = new GitLabAPI( + { pullRequestID: "27117", repoSlug: "gitlab-org/gitlab-ce" }, + getGitLabAPICredentialsFromEnv({ + CI_API_V4_URL: "https://testciapiv4url.com/api/v4", + DANGER_GITLAB_API_TOKEN: "FAKE_DANGER_GITLAB_API_TOKEN", + }) + ) + + expect(api.projectURL).toBe("https://testciapiv4url.com/gitlab-org/gitlab-ce") + }) + it("projectURL is defined", () => { expect(api.projectURL).toBe("https://gitlab.com/gitlab-org/gitlab-ce") })