Skip to content

Commit

Permalink
Merge pull request #14 from danger/pr
Browse files Browse the repository at this point in the history
Initial work on the CHANGELOG test, and adds a fail command
  • Loading branch information
orta committed Oct 16, 2016
2 parents 9577a35 + 819bf26 commit 6aff57e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
51 changes: 51 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### 0.0.2

OK, first usable for others version. Only supports GitHub and Travis CI.

You can run by doing:

```sh
danger
```

Make sure you set a `DANGER_GITHUB_API_TOKEN` on your CI - [see the Ruby guide](http://danger.systems/guides/getting_started.html#setting-up-danger-to-run-on-your-ci) for that.

Then you can make a `dangerfile.js` (has to be lowercase, deal with it.) It has access to a whopping 2 DSL attributes.

```sh
pr
git
fail(message: string)
```

`pr` _probably_ won't be sticking around for the long run, but if you're using a `0.0.2` release, you should be OK with that. It's the full metadata of the PR, so [this JSON file](https://raw.githubusercontent.com/danger/danger/master/spec/fixtures/github_api/pr_response.json).
`git` currently has:

```sh
git.modified_file
git.created_files
git.deleted_files
```

which are string arrays of files.

`fail(message: string)` will let you raise an error, and will make the process return 1 after the parsing has finished.

Overall: your Dangerfile should look something like:

```js
import { danger } from "danger"

const hasChangelog = danger.git.modified_files.includes("changelog.md")
if (!hasChangelog) {
fail("No Changelog changes!")
}
```

That should do ya. I think. This doens't support babel, and I haven't explored using other modules etc, so

./

### 0.0.1

Not usable for others, only stubs of classes etc.
6 changes: 5 additions & 1 deletion dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// @flow

// import danger from "danger"
import danger from "./source/danger"
import { danger, fail } from "./source/danger"

// warn on changes in Package.json and not in shrinkwrap
const hasChangelog = danger.git.modified_files.includes("changelog.md")
if (!hasChangelog) {
fail("No Changelog changes!")
}

// warn on changelog
// console.log(danger)
Expand Down
8 changes: 5 additions & 3 deletions source/danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ type DangerDSL = {
git: GitDSL
}

const danger: DangerDSL = {
/** Fails the build */
export function fail(message: string) {
}

export const danger: DangerDSL = {
git: {
modified_files: ["hello world"],
created_files: ["other file"],
deleted_files: ["last file"]
}
}

export default danger
1 change: 0 additions & 1 deletion source/dsl/DangerDSL.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
"use strict"

// import type { Platform } from "../platforms/platform"
import type { GitDSL } from "../dsl/Git"

export default class DangerDSL {
Expand Down
12 changes: 11 additions & 1 deletion source/runner/Dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default class Dangerfile {
// then user get typed data, and we fill it in
// via the VM context

const cleaned = data.replace(/import danger from/gi, "// import danger from")
const cleaned = data
.replace(/import danger /gi, "// import danger ")
.replace(/import { danger/gi, "// import { danger ")

const script = new vm.Script(cleaned, {
filename: file,
Expand All @@ -28,12 +30,20 @@ export default class Dangerfile {
timeout: 1000 // ms
})

let failed = false
const fail = (message: string) => {
console.error(message)
failed = true
}

const context: any = {
fail,
console,
danger: this.dsl
}

script.runInNewContext(context)
if (failed) { process.exitCode = 1 }
})
}
}
Expand Down

0 comments on commit 6aff57e

Please sign in to comment.