Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Mar 26, 2018
2 parents 484671d + 2cdbc6f commit 73e49bd
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 15 deletions.
31 changes: 28 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

## Master

## 3.3.0

* Adds support for inline comments when using GitHub.

This is one of those "massive under the hood" changes, that has a tiny user DSL surface. From this point onwards
Expand Down Expand Up @@ -46,6 +44,25 @@

-- [@sunshinejr][]

## 3.3.2

* Adds support for TeamCity as a CI provider. [@fwal][]

## 3.3.1

* Fixed Babel 7 breaking because of sourceFileName being defined wrong. [@happylinks][]

## 3.3.0

* Fix `committer` field issue - missing in Stash API by using commit author instead. [@zdenektopic][]
* Adds a new command: `reset-status`

This command is for setting the CI build status in advance of running Danger. If your Danger build relies
on running tests/linters, then you might want to set the PR status (the red/green/yellow dots) to pending
at the start of your build. You can do this by running `yarn danger reset-status`.

[@mxstbr][]

## 3.2.0

* Add BitBucket Server support.
Expand Down Expand Up @@ -992,5 +1009,13 @@ Not usable for others, only stubs of classes etc. - [@orta][]
[@peterjgrainger]: https://github.com/peterjgrainger
[@azz]: https://github.com/azz
[@mifi]: https://github.com/ionutmiftode

<<<<<<< HEAD
[@sunshinejr]: https://github.com/sunshinejr
[ref]: http://danger.systems/js/reference.html
=======
[@mxstbr]: https://github.com/mxstbr
[@happylinks]: https://github.com/happylinks
[@fwal]: https://github.com/fwal

> > > > > > > 2cdbc6f1ffed5253c40639fc7ed7bd299347f949
> > > > > > > [ref]: http://danger.systems/js/reference.html
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This provides another logical step in your process, through which Danger can hel
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 or Visual Studio Team Services.
Codeship, Drone, Buildkite, Nevercode, buddybuild, Visual Studio Team Services or TeamCity.

[![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)

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "3.2.0",
"version": "3.3.2",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand All @@ -11,7 +11,8 @@
"danger-process": "distribution/commands/danger-process.js",
"danger-ci": "distribution/commands/danger-ci.js",
"danger-init": "distribution/commands/danger-init.js",
"danger-local": "distribution/commands/danger-local.js"
"danger-local": "distribution/commands/danger-local.js",
"danger-reset-status": "distribution/commands/danger-reset-status.js"
},
"jest": {
"transform": {
Expand Down
59 changes: 59 additions & 0 deletions source/ci_source/providers/TeamCity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist } from "../ci_source_helpers"

/**
*
* ### CI Setup
*
* You need to add `DANGER_GITHUB_API_TOKEN` to the ENV for the build or machine manually.
* Then you also need to figure out how to provide the URL for the pull request in `PULL_REQUEST_URL` ENV.
*
* TeamCity provides the `%teamcity.build.branch%` variable that contains something like `pull/123` that you can use:
* ```sh
* PULL_REQUEST_URL='https://github.com/dager/danger-js/%teamcity.build.branch%'
* ```
*
*/

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

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

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

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

const mustHave = ["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}` : ""
}
}
68 changes: 68 additions & 0 deletions source/ci_source/providers/_tests/_teamcity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { TeamCity } from "../TeamCity"
import { getCISourceForEnv } from "../../get_ci_source"

const correctEnv = {
TEAMCITY_VERSION: "1.2.3",
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541",
}

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

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

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

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

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

const envs = ["TEAMCITY_VERSION", "PULL_REQUEST_URL"]
envs.forEach((key: string) => {
let env = {
TEAMCITY_VERSION: "1.2.3",
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541",
}
env[key] = null

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

describe(".pullRequestID", () => {
it("pulls it out of the env", () => {
const teamcity = new TeamCity({
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541",
})
expect(teamcity.pullRequestID).toEqual("541")
})
})

describe(".repoSlug", () => {
it("derives it from the PR Url", () => {
const teamcity = new TeamCity(correctEnv)
expect(teamcity.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 @@ -10,6 +10,7 @@ import { Jenkins } from "./Jenkins"
import { Nevercode } from "./Nevercode"
import { Semaphore } from "./Semaphore"
import { Surf } from "./Surf"
import { TeamCity } from "./TeamCity"
import { Travis } from "./Travis"
import { VSTS } from "./VSTS"

Expand All @@ -28,6 +29,7 @@ const providers = [
BuddyBuild,
VSTS,
Bitrise,
TeamCity,
]

// Mainly used for Dangerfile linting
Expand All @@ -44,6 +46,7 @@ const realProviders = [
Buildkite,
BuddyBuild,
VSTS,
TeamCity,
]

export { providers, realProviders }
36 changes: 36 additions & 0 deletions source/commands/ci/reset-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import chalk from "chalk"
import * as debug from "debug"

import { getPlatformForEnv } from "../../platforms/platform"
import { SharedCLI } from "../utils/sharedDangerfileArgs"
import getRuntimeCISource from "../utils/getRuntimeCISource"

import { RunnerConfig } from "./runner"

const d = debug("danger:reset-status")

export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
d(`Starting sub-process run with ${app.args}`)
const source = (config && config.source) || (await getRuntimeCISource(app))

// This does not set a failing exit code
if (source && !source.isPR) {
console.log("Skipping Danger due to this run not executing on a PR.")
}

// The optimal path
if (source && source.isPR) {
const platform = (config && config.platform) || getPlatformForEnv(process.env, source)
if (!platform) {
console.log(chalk.red(`Could not find a source code hosting platform for ${source.name}.`))
console.log(
`Currently Danger JS only supports GitHub, if you want other platforms, consider the Ruby version or help out.`
)
process.exitCode = 1
}

if (platform) {
await platform.updateStatus("pending", "Danger is waiting for your CI run to complete...")
}
}
}
12 changes: 12 additions & 0 deletions source/commands/danger-reset-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env node

import * as program from "commander"

import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import { runRunner } from "./ci/runner"

program.usage("[options]").description("Reset the status of a GitHub PR to pending.")
setSharedArgs(program).parse(process.argv)

const app = (program as any) as SharedCLI
runRunner(app)
1 change: 1 addition & 0 deletions source/commands/danger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ program
.command("pr", "Runs your local Dangerfile against an existing GitHub PR. Will not post on the PR")
.command("runner", "Runs a dangerfile against a DSL passed in via STDIN [You probably don't need this]")
.command("local", "Runs danger standalone on a repo, useful for git hooks")
.command("reset-status", "Set the status of a PR to pending when a new CI run starts")
.on("--help", () => {
console.log("\n")
console.log(" Docs:")
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class GitHub implements Platform {
* then return true.
*/

updateStatus = async (passed: boolean, message: string, url?: string): Promise<boolean> => {
updateStatus = async (passed: boolean | "pending", message: string, url?: string): Promise<boolean> => {
const ghAPI = this.api.getExternalAPI()

const prJSON = await this.api.getPullRequestInfo()
Expand Down
16 changes: 11 additions & 5 deletions source/platforms/bitbucket_server/BitBucketServerGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ function bitBucketServerCommitToGitCommit(
name: bbsCommit.author.name,
date: new Date(bbsCommit.authorTimestamp).toISOString(),
},
committer: {
email: bbsCommit.committer.emailAddress,
name: bbsCommit.committer.name,
date: new Date(bbsCommit.committerTimestamp).toISOString(),
},
committer: bbsCommit.committer
? {
email: bbsCommit.committer.emailAddress,
name: bbsCommit.committer.name,
date: new Date(bbsCommit.committerTimestamp).toISOString(),
}
: {
email: bbsCommit.author.emailAddress,
name: bbsCommit.author.name,
date: new Date(bbsCommit.authorTimestamp).toISOString(),
},
message: bbsCommit.message,
tree: null,
url,
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface Platform {
/** Replace the main Danger comment */
updateOrCreateComment: (dangerID: string, newComment: string) => Promise<any>
/** Sets the current PR's status */
updateStatus: (passed: boolean, message: string, url?: string) => Promise<boolean>
updateStatus: (passed: boolean | "pending", message: string, url?: string) => Promise<boolean>
/** Get the contents of a file at a path */
getFileContents: (path: string, slug?: string, ref?: string) => Promise<string>
}
Expand Down
4 changes: 2 additions & 2 deletions source/runner/runners/utils/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export const babelify = (content: string, filename: string, extraPlugins: string
filename,
filenameRelative: filename,
sourceMap: false,
sourceFileName: null,
sourceMapTarget: null,
sourceFileName: undefined,
sourceMapTarget: undefined,
sourceType: "module",
plugins: [...extraPlugins, ...options.plugins],
}
Expand Down

0 comments on commit 73e49bd

Please sign in to comment.