Skip to content

Commit

Permalink
Merge pull request #855 from bigkraig/feature/gitlab
Browse files Browse the repository at this point in the history
Adds support for GitLab CI
  • Loading branch information
orta committed Jun 6, 2019
2 parents a1db4c6 + b386dae commit 583eae5
Show file tree
Hide file tree
Showing 31 changed files with 2,956 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -53,3 +53,8 @@ test-results.json
source/_danger.d.tse
source/_danger.d.ts
tests.json

# IDEs
.idea

/*.sh
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -23,7 +23,7 @@ matrix:
- yarn flow check

# Checks every example dangerfile can run in `danger runner`.
- node_js: "8.4"
- node_js: "8"
script:
- yarn build
- node scripts/run-fixtures.js
Expand Down Expand Up @@ -67,7 +67,7 @@ matrix:
source/platforms/git/_tests/local_dangerfile_example.ts || echo "Skipping Danger Local for non PR run"'

# Create some fake projects at runtime
- node_js: "8.12"
- node_js: "8"
script:
- echo "This is only for Integration tests on two blank projects"
- yarn build
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,8 @@
<!-- Your comment below this -->
- Add support for AppCenter - [@mrndjo]

- Adds GitLab & GitLab CI support. - [@notjosh], [@bigkraig], [@jamime]

# 7.1.4

- Un-hardcodes the repo in `danger.github.utils.createOrUpdatePR`- [@ds300]
Expand Down Expand Up @@ -1634,3 +1636,4 @@ Not usable for others, only stubs of classes etc. - [@orta]
[@dblandin]: https://github.com/dblandin
[@paulmelnikow]: https://github.com/paulmelnikow
[@ds300]: https://github.com/ds300
[@jamime]: https://github.com/jamime
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -19,9 +19,9 @@ review.

You can use Danger to codify your teams norms, leaving humans to think about harder problems.

Danger JS works with GitHub or BitBucket Server for code review, then with: Travis CI, Circle CI, GitHub Actions,
Semaphore, Jenkins, Docker Cloud, Bitrise, surf-build, Codeship, Drone, Buildkite, Nevercode, buddybuild, TeamCity,
Visual Studio Team Services, Screwdriver, Concourse, Netlify, CodeBuild, Codefresh or AppCenter.
Danger JS works with GitHub or BitBucket Server for code review, then with: Travis CI, GitLab CI, Circle CI, GitHub
Actions, Semaphore, Jenkins, Docker Cloud, Bitrise, surf-build, Codeship, Drone, Buildkite, Nevercode, buddybuild,
TeamCity, Visual Studio Team Services, Screwdriver, Concourse, Netlify, CodeBuild, Codefresh or AppCenter.

[![npm](https://img.shields.io/npm/v/danger.svg)](https://www.npmjs.com/package/danger)
[![Build Status](https://travis-ci.org/danger/danger-js.svg?branch=master)](https://travis-ci.org/danger/danger-js)
Expand Down Expand Up @@ -83,7 +83,7 @@ it compiles.
You can run your dev copy of danger against a PR by running:

```sh
yarn build; node --inspect distribution/source/commands/danger-pr.js https://github.com/danger/danger-js/pull/817
yarn build; node --inspect distribution/commands/danger-pr.js https://github.com/danger/danger-js/pull/817
```

### How does Danger JS work?
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -100,6 +100,7 @@
"@types/lodash.mapvalues": "^4.6.6",
"@types/lodash.memoize": "^4.1.3",
"@types/micromatch": "^3.1.0",
"@types/nock": "^10.0.3",
"@types/node": "^10.11.3",
"@types/node-fetch": "^2.1.2",
"@types/p-limit": "^2.0.0",
Expand All @@ -115,6 +116,7 @@
"jest-json-reporter": "^1.2.2",
"lint-staged": "^7.3.0",
"madge": "^3.2.0",
"nock": "^10.0.6",
"pkg": "^4.3.4",
"prettier": "^1.14.2",
"release-it": "^7.6.1",
Expand All @@ -134,6 +136,7 @@
"commander": "^2.18.0",
"debug": "^4.1.1",
"get-stdin": "^6.0.0",
"gitlab": "^6.0.0",
"http-proxy-agent": "^2.1.0",
"https-proxy-agent": "^2.2.1",
"hyperlinker": "^1.0.0",
Expand Down
30 changes: 30 additions & 0 deletions source/ci_source/providers/GitLabCI.ts
@@ -0,0 +1,30 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers"

export class GitLabCI implements CISource {
constructor(private readonly env: Env) {}

get name(): string {
return "GitLab CI"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["GITLAB_CI"])
}

get isPR(): boolean {
const mustHave = ["CI_MERGE_REQUEST_IID", "CI_PROJECT_PATH"]
const mustBeInts = ["CI_MERGE_REQUEST_IID"]
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts)
}

get pullRequestID(): string {
return this.env.CI_MERGE_REQUEST_IID
}

get repoSlug(): string {
return this.env.CI_PROJECT_PATH
}
}

// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
3 changes: 3 additions & 0 deletions source/ci_source/providers/index.ts
Expand Up @@ -11,6 +11,7 @@ import { DockerCloud } from "./DockerCloud"
import { Drone } from "./Drone"
import { FakeCI } from "./Fake"
import { GitHubActions } from "./GitHubActions"
import { GitLabCI } from "./GitLabCI"
import { Jenkins } from "./Jenkins"
import { Netlify } from "./Netlify"
import { Nevercode } from "./Nevercode"
Expand All @@ -24,6 +25,7 @@ import { VSTS } from "./VSTS"
const providers = [
FakeCI,
GitHubActions,
GitLabCI,
Travis,
Circle,
Semaphore,
Expand All @@ -49,6 +51,7 @@ const providers = [
// Mainly used for Dangerfile linting
const realProviders = [
GitHubActions,
GitLabCI,
Travis,
Circle,
Semaphore,
Expand Down
1 change: 1 addition & 0 deletions source/commands/ci/runner.ts
Expand Up @@ -58,6 +58,7 @@ export const runRunner = async (app: SharedCLI, config?: Partial<RunnerConfig>)

if (platform) {
const dangerJSONDSL = await jsonDSLGenerator(platform, source, app)
d({ dangerJSONDSL })
const execConfig: ExecutorOptions = {
stdoutOnly: !platform.supportsCommenting() || app.textOnly,
verbose: app.verbose,
Expand Down
33 changes: 27 additions & 6 deletions source/commands/danger-pr.ts
Expand Up @@ -14,6 +14,7 @@ import { prepareDangerDSL } from "./utils/runDangerSubprocess"
import { runRunner } from "./ci/runner"
import { Platform, getPlatformForEnv } from "../platforms/platform"
import { CISource } from "../ci_source/ci_source"
import { getGitLabAPICredentialsFromEnv } from "../platforms/gitlab/GitLabAPI"

const d = debug("pr")
const log = console.log
Expand All @@ -25,6 +26,8 @@ interface App extends SharedCLI {
js?: boolean
}

const gitLabApiCredentials = getGitLabAPICredentialsFromEnv(process.env)

program
.usage("[options] <pr_url>")
.description("Emulate running Danger against an existing GitHub Pull Request.")
Expand All @@ -34,9 +37,15 @@ program
.on("--help", () => {
log("\n")
log(" Docs:")
if (!process.env["DANGER_GITHUB_API_TOKEN"] && !process.env["DANGER_BITBUCKETSERVER_HOST"]) {
if (
!process.env["DANGER_GITHUB_API_TOKEN"] &&
!process.env["DANGER_BITBUCKETSERVER_HOST"] &&
!gitLabApiCredentials.token
) {
log("")
log(" You don't have a DANGER_GITHUB_API_TOKEN set up, this is optional, but TBH, you want to do this.")
log(
" You don't have a DANGER_GITHUB_API_TOKEN/DANGER_GITLAB_API_TOKEN set up, this is optional, but TBH, you want to do this."
)
log(" Check out: http://danger.systems/js/guides/the_dangerfile.html#working-on-your-dangerfile")
log("")
}
Expand All @@ -60,13 +69,14 @@ if (program.args.length === 0) {
console.error("Please include a PR URL to run against")
process.exitCode = 1
} else {
const customHost = process.env["DANGER_GITHUB_HOST"] || process.env["DANGER_BITBUCKETSERVER_HOST"] || "github"
const customHost =
process.env["DANGER_GITHUB_HOST"] || process.env["DANGER_BITBUCKETSERVER_HOST"] || gitLabApiCredentials.host // this defaults to https://gitlab.com

// Allow an ambiguous amount of args to find the PR reference
const findPR = program.args.find(a => a.includes(customHost))
const findPR = program.args.find(a => a.includes(customHost) || a.includes("github"))

if (!findPR) {
console.error(`Could not find an arg which mentioned GitHub or BitBucket Server.`)
console.error(`Could not find an arg which mentioned GitHub, BitBucket Server, or GitLab.`)
process.exitCode = 1
} else {
const pr = pullRequestParser(findPR)
Expand All @@ -86,7 +96,18 @@ if (program.args.length === 0) {
d(`executing dangerfile at ${dangerfilePath(program)}`)
}
const source = new FakeCI({ DANGER_TEST_REPO: pr.repo, DANGER_TEST_PR: pr.pullRequestNumber })
const platform = getPlatformForEnv(process.env, source, /* requireAuth */ false)
const platform = getPlatformForEnv(
{
...process.env,
// Inject a platform hint, its up to getPlatformForEnv to decide if the environment is suitable for the
// requested platform. Because we have a URL we can determine with greater accuracy what platform that the
// user is attempting to test. This complexity is required because danger-pr defaults to using
// un-authenticated GitHub where typically when using FakeCI we want to use Fake(Platform) e.g. when running
// danger-local
DANGER_PR_PLATFORM: pr.platform,
},
source
)

if (isJSON) {
d("getting just the JSON/JS DSL")
Expand Down

0 comments on commit 583eae5

Please sign in to comment.