Skip to content

Commit

Permalink
add support for Docker Cloud CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Camacho committed Jan 30, 2017
1 parent 80ab229 commit 391cb70
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 8 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Danger on Node, wonder what's going on? see [VISION.md](VISION.md)

*Welcome!*

So, what's the deal? Well, right now Danger JS does a lot of the simpler parts of [the Ruby version](http://danger.systems).
You can look at [Git](https://github.com/danger/danger-js/blob/master/source/dsl/GitDSL.ts) metadata, or [GitHub](https://github.com/danger/danger-js/blob/master/source/dsl/GitHubDSL.ts) metadata on Travis, Circle, Semaphore or Jenkins.
So, what's the deal? Well, right now Danger JS does a lot of the simpler parts of [the Ruby version](http://danger.systems).
You can look at [Git](https://github.com/danger/danger-js/blob/master/source/dsl/GitDSL.ts) metadata, or [GitHub](https://github.com/danger/danger-js/blob/master/source/dsl/GitHubDSL.ts) metadata on Travis, Circle, Semaphore, Jenkins, or Docker Cloud.

Danger can fail your build, write a comment on GitHub, edit it as your PR changes and then delete it once you've passed review. Perfect.

Expand Down Expand Up @@ -63,13 +63,25 @@ For now, to get set up I'd recommend looking at [the setup guide for the Ruby ve

You will need to create a bot account, and set up CI to run danger.

If you are using Docker Cloud, make sure to set the following blank ENV vars in your `docker-compose.test.yml` file so they are carried forward from the build environment:

```yml
sut:
build: .
environment:
- DANGER_GITHUB_API_TOKEN
- DOCKER_REPO
- PULL_REQUEST_URL
- SOURCE_REPOSITORY_URL
```

## Running/Testing manually against a repo

There are two ways to do this:

#### Using `danger pr`

The command `danger pr` expects an argument of a PR url, e.g. `danger pr https://github.com/danger/danger-js/pull/100`.
The command `danger pr` expects an argument of a PR url, e.g. `danger pr https://github.com/danger/danger-js/pull/100`.

This will use your local `dangerfile.js` against the metadata of that PR. Danger will then output the results as JSON, instead of on the PR itself.

Expand All @@ -92,7 +104,7 @@ git checkout branch-for-pr-1234
DANGER_TEST_PR='1234' npm run danger
```

assuming that your local file-system matches up to that branch on github, this will be a good approximation of how danger will work when you integrate it into your CI system.
assuming that your local file-system matches up to that branch on github, this will be a good approximation of how danger will work when you integrate it into your CI system.

Note: this will leave a comment on the PR.

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// Add your own contribution below

### 0.11.0

* Add support for [Docker Cloud](https://cloud.docker.com)

### 0.10.1

* Builds which only use markdown now only show the markdown, and no violations table is shown - mxstbr
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "0.10.1",
"version": "0.11.0",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand Down
47 changes: 47 additions & 0 deletions source/ci_source/providers/DockerCloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist } from "../ci_source_helpers"

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

get name(): string { return "Docker Cloud" }

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

get isPR(): boolean {
if (ensureEnvKeysExist(this.env, ["PULL_REQUEST_URL"])) {
return true
}

const mustHave = ["SOURCE_REPOSITORY_URL", "PULL_REQUEST_URL"]
return ensureEnvKeysExist(this.env, mustHave)
}

private _prParseURL(): {owner?: string, reponame?: string, id?: string} {
const prUrl = this.env.PULL_REQUEST_URL || ""
const splitSlug = prUrl.split("/")
if (splitSlug.length === 7) {
const owner = splitSlug[3]
const reponame = splitSlug[4]
const id = splitSlug[6]
return {owner, reponame, id}
};
return {}
}

get pullRequestID(): string {
const {id} = this._prParseURL()
return id || ""
}

get repoSlug(): string {
const {owner, reponame} = this._prParseURL()
return (owner && reponame) ? `${owner}/${reponame}` : ""
}

get repoURL(): string { return this.env.SOURCE_REPOSITORY_URL }
get supportedPlatforms(): string[] { return ["github"] }
}
62 changes: 62 additions & 0 deletions source/ci_source/providers/_tests/_dockerCloud.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {DockerCloud} from "../DockerCloud"

const correctEnv = {
"DOCKER_REPO": "someproject",
"PULL_REQUEST_URL": "https://github.com/artsy/eigen/pull/800",
"SOURCE_REPOSITORY_URL": "https://github.com/artsy/eigen"
}

describe(".isCI", () => {
it("validates when all DockerCloud environment vars are set", () => {
const dockerCloud = new DockerCloud(correctEnv)
expect(dockerCloud.isCI).toBeTruthy()
})

it("does not validate without env", () => {
const dockerCloud = new DockerCloud({})
expect(dockerCloud.isCI).toBeFalsy()
})
})

describe(".isPR", () => {
it("validates when all dockerCloud environment vars are set", () => {
const dockerCloud = new DockerCloud(correctEnv)
expect(dockerCloud.isPR).toBeTruthy()
})

it("does not validate outside of dockerCloud", () => {
const dockerCloud = new DockerCloud({})
expect(dockerCloud.isPR).toBeFalsy()
})

const envs = ["PULL_REQUEST_URL", "SOURCE_REPOSITORY_URL", "DOCKER_REPO"]
envs.forEach((key: string) => {
let env = {
"DOCKER_REPO": "someproject",
"PULL_REQUEST_URL": "https://github.com/artsy/eigen/pull/800",
"SOURCE_REPOSITORY_URL": "https://github.com/artsy/eigen"
}
env[key] = null

it(`does not validate when ${key} is missing`, () => {
const dockerCloud = new DockerCloud({})
expect(dockerCloud.isPR).toBeFalsy()
})
})
})

describe(".pullRequestID", () => {
it("pulls it out of the env", () => {
const dockerCloud = new DockerCloud({
"PULL_REQUEST_URL": "https://github.com/artsy/eigen/pull/800"
})
expect(dockerCloud.pullRequestID).toEqual("800")
})
})

describe(".repoSlug", () => {
it("derives it from the PR Url", () => {
const dockerCloud = new DockerCloud(correctEnv)
expect(dockerCloud.repoSlug).toEqual("artsy/eigen")
})
})
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
version "0.0.32"
resolved "https://registry.yarnpkg.com/@types/es6-promise/-/es6-promise-0.0.32.tgz#3bcf44fb1e429f3df76188c8c6d874463ba371fd"

"@types/jest@^16.0.3":
version "16.0.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-16.0.3.tgz#28a465586cd37d2638043ebcca91010ed89fc284"
"@types/jest@^18.0.0":
version "18.0.0"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-18.0.0.tgz#c0cfc76c426b4ef5cdad464870cd9b4b081eea29"

"@types/node-fetch@^1.6.6":
version "1.6.7"
Expand Down

0 comments on commit 391cb70

Please sign in to comment.