Skip to content

Commit

Permalink
Use CI_API_V4_URL in GitLab if it is available
Browse files Browse the repository at this point in the history
  • Loading branch information
beezly committed Dec 16, 2019
1 parent 86d4573 commit 3f27263
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions docs/usage/gitlab.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions source/platforms/gitlab/GitLabAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions source/platforms/gitlab/_tests/_gitlab_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down

0 comments on commit 3f27263

Please sign in to comment.