Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for GitLab CI #855

Merged
merged 42 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9c8ba47
Recognise DANGER_GITLAB_HOST env
notjosh Apr 5, 2019
cc632c2
GitLab implementation & tests for pullRequestParser
notjosh Apr 5, 2019
e6410b8
Added base platform code for GitLab
notjosh Apr 5, 2019
91ef63a
Fetchin' live MR vales from GitLab API
notjosh Apr 5, 2019
c274eb8
Oof, there's a lot going on in there. The runner works in another pro…
notjosh Apr 6, 2019
8ef0581
Generally the pulling/reading works, to the point of commenting
notjosh Apr 6, 2019
abecf38
The nuts & bolts of API commenting are working
notjosh Apr 6, 2019
8ceb1ec
Inline comments seem to sort of work?
notjosh Apr 6, 2019
2367ed3
Minor bugfix to only delete comments that Danger created
notjosh Apr 6, 2019
a22c68b
Support GitLab CI as a CI source
bigkraig Apr 8, 2019
76b7bb1
Fixes inline commenting in GitLab
bigkraig Apr 9, 2019
957412c
Added CHANGELOG entry
bigkraig Apr 9, 2019
d344384
Added GitLab to README
bigkraig Apr 9, 2019
527c62c
Merge branch 'master' into feature/gitlab
bigkraig Apr 9, 2019
b2ec339
Removed unused test file
bigkraig Apr 9, 2019
5168c45
Throwing errors on not yet implemented gitlab functions
bigkraig Apr 9, 2019
7d9b5c9
Using the CI_MERGE_REQUEST_IID var according to https://docs.gitlab.c…
bigkraig Apr 9, 2019
f0d1e81
Removed some accidentally added vscode configuration
bigkraig Apr 9, 2019
6ac429c
Updated the gitlab dependency to include support for Node 8
bigkraig Apr 10, 2019
4acda3b
Bumped the oldest node to something not so old (8.9)
bigkraig Apr 11, 2019
06ea6d9
Merge branch 'master' into feature/gitlab
bigkraig Apr 14, 2019
7d629cc
Changed out the older node 8 tests to test against the current LTS ve…
bigkraig Apr 14, 2019
19a35b7
Merge remote-tracking branch 'upstream/master' into feature/gitlab
May 17, 2019
e3a78d7
Add tests for GitLab
May 17, 2019
2d3c6de
Merge pull request #1 from jamime/feature/gitlab
bigkraig May 20, 2019
ab83665
delete main comment when all issues are resolved
May 21, 2019
a26047e
Merge pull request #2 from bigkraig/feature/delete_comment
jamime May 28, 2019
7b34217
update gitlab dep
May 28, 2019
2ba17eb
use GITLAB_CI to detect CI
May 29, 2019
765d5d6
Revert "update gitlab dep"
May 29, 2019
3a337fb
improve GitLab environment detection
May 29, 2019
157383b
fix: use DANGER_GITLAB_API_TOKEN to detect DSL type
May 29, 2019
657e0a0
Merge branch 'master' into feature/gitlab
f-meloni May 29, 2019
9bf4690
fix: updateOrCreateComment note detection
May 30, 2019
9363aa1
update gitlab dep
Jun 3, 2019
1d75688
docs: update command in readme
Jun 4, 2019
0952bac
chore: tidy code
Jun 4, 2019
67662eb
fix: improve danger-pr
Jun 4, 2019
78af7b2
feat: GitLab fileContents
Jun 5, 2019
8776aa6
fix: change GitHub platform to be a named function instead of an arro…
Jun 5, 2019
083cff7
ci: trigger build
Jun 5, 2019
b386dae
feat: add logging to GitLabAPI
Jun 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading