Skip to content

Commit

Permalink
Add support for Screwdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
dbgrandi committed Apr 29, 2018
1 parent c9939fc commit 7e15520
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

* Catch the github api error thrown from @octokit/rest [@Teamop][]
* Replace preview media type of github pull request reviews api [@Teamop][]
* Add support for [Screwdriver CI](http://screwdriver.cd) [@dbgrandi][]

# 3.6.0

Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
<a href="#this-thing-is-broken-i-should-help-improve-it">Helping Out</a> &bull;
<a href="http://danger.systems/js/usage/extending-danger.html">Plugin Development</a>
</p>

## What is Danger JS?

Danger runs after your CI, automating your team's conventions surrounding code review.

This provides another logical step in your process, through which Danger can help lint your rote tasks in daily code review.
This provides another logical step in your process, through which Danger can help lint your rote tasks in daily code
review.

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

Danger JS currently works with GitHub or BitBucket Server and Travis CI, Circle CI, Semaphore, Jenkins, Docker Cloud, Bitrise, surf-build,
Codeship, Drone, Buildkite, Nevercode, buddybuild, Visual Studio Team Services or TeamCity.
Danger JS currently works with GitHub or BitBucket Server and Travis CI, Circle CI, Semaphore, Jenkins, Docker Cloud,
Bitrise, surf-build, Codeship, Drone, Buildkite, Nevercode, buddybuild, Visual Studio Team Services, TeamCity or
Screwdriver.

[![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) [![Build Status](https://ci.appveyor.com/api/projects/status/ep5hgeox3lbc5c7f?svg=true)](https://ci.appveyor.com/project/orta/danger-js/branch/master) [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/danger)
[![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)
[![Build Status](https://ci.appveyor.com/api/projects/status/ep5hgeox3lbc5c7f?svg=true)](https://ci.appveyor.com/project/orta/danger-js/branch/master)
[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/danger)

## For example?

Expand Down
57 changes: 57 additions & 0 deletions source/ci_source/providers/Screwdriver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers"

/**
* ### CI Setup
*
* With BuildKite you run the server yourself, so you will want to run it as a part of your build process.
* It is common to have build steps, so we would recommend adding this to your scrip:
*
* ``` shell
* echo "--- Running Danger"
* bundle exec danger
* ```
*
* ### Token Setup
*
* #### GitHub
*
* As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
* is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.
*/
export class Screwdriver implements CISource {
constructor(private readonly env: Env) {}

get name(): string {
return "Screwdriver"
}

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

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

private _parseRepoURL(): string {
const repoURL = this.env.SCM_URL
const regexp = new RegExp("([/:])([^/]+/[^/.]+)(?:.git)?$")
const matches = repoURL.match(regexp)
return matches ? matches[2] : ""
}

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

get repoSlug(): string {
return this._parseRepoURL()
}

get ciRunURL() {
return process.env.BUILDKITE_BUILD_URL
}
}
75 changes: 75 additions & 0 deletions source/ci_source/providers/_tests/_screwdriver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Screwdriver } from "../Screwdriver"
import { getCISourceForEnv } from "../../get_ci_source"

const correctEnv = {
SCREWDRIVER: "true",
SD_PULL_REQUEST: "42",
SCM_URL: "git@github.com:danger/danger-js",
}

describe("being found when looking for CI", () => {
it("finds Screwdriver with the right ENV", () => {
const ci = getCISourceForEnv(correctEnv)
expect(ci).toBeInstanceOf(Screwdriver)
})
})

describe(".isCI", () => {
it("validates when SCREWDRIVER is present in environment", () => {
const screwdriver = new Screwdriver(correctEnv)
expect(screwdriver.isCI).toBeTruthy()
})

it("does not validate without SCREWDRIVER present in environment", () => {
const screwdriver = new Screwdriver({})
expect(screwdriver.isCI).toBeFalsy()
})
})

describe(".isPR", () => {
it("validates when all Screwdriver environment variables are set", () => {
const screwdriver = new Screwdriver(correctEnv)
expect(screwdriver.isPR).toBeTruthy()
})

it("does not validate with required environment variables", () => {
const screwdriver = new Screwdriver({})
expect(screwdriver.isPR).toBeFalsy()
})

const envs = ["SD_PULL_REQUEST", "SCM_URL"]
envs.forEach((key: string) => {
const env = {
...correctEnv,
[key]: null,
}

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

it("needs to have a PR number", () => {
const env = {
...correctEnv,
SD_PULL_REQUEST: "not a number",
}
const screwdriver = new Screwdriver(env)
expect(screwdriver.isPR).toBeFalsy()
})
})

describe(".pullRequestID", () => {
it("pulls it out of environment", () => {
const screwdriver = new Screwdriver(correctEnv)
expect(screwdriver.pullRequestID).toEqual("42")
})
})

describe(".repoSlug", () => {
it("pulls it out of environment", () => {
const screwdriver = new Screwdriver(correctEnv)
expect(screwdriver.repoSlug).toEqual("danger/danger-js")
})
})
3 changes: 3 additions & 0 deletions source/ci_source/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Surf } from "./Surf"
import { TeamCity } from "./TeamCity"
import { Travis } from "./Travis"
import { VSTS } from "./VSTS"
import { Screwdriver } from "./Screwdriver"

const providers = [
Travis,
Expand All @@ -30,6 +31,7 @@ const providers = [
VSTS,
Bitrise,
TeamCity,
Screwdriver,
]

// Mainly used for Dangerfile linting
Expand All @@ -47,6 +49,7 @@ const realProviders = [
BuddyBuild,
VSTS,
TeamCity,
Screwdriver,
]

export { providers, realProviders }

0 comments on commit 7e15520

Please sign in to comment.