Danger on Node, wonder what's going on? see VISION.md
Welcome!
So, what's the deal? Well, right now Danger JS does a lot of the simpler parts of the Ruby version. You can look at Git metadata, or GitHub metadata on Travis CI, Circle CI, Semaphore, Jenkins, Docker Cloud, surf-build, Codeship, Drone, or Buildkite.
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.
Getting set up in your Project
# with yarn
yarn add danger --dev
# or with npm
npm install --save-dev dangerIf using NPM, add a run command to your package.json
{
"scripts": {
"danger": "danger"
}
}Then create a dangerfile.js in the project root with some rules:
import { danger, fail, warn } from "danger"
import fs from "fs"
// Make sure there are changelog entries
const hasChangelog = danger.git.modified_files.includes("changelog.md")
if (!hasChangelog) { fail("No Changelog changes!") }
const jsFiles = danger.git.created_files.filter(path => path.endsWith("js"))
// new js files should have `@flow` at the top
const unFlowedFiles = jsFiles.filter(filepath => {
const content = fs.readFileSync(filepath)
return !content.includes("@flow")
})
if (unFlowedFiles.length > 0) {
warn(`These new JS files do not have Flow enabled: ${unFlowedFiles.join(", ")}`)
}You can also write your Dangerfile in TypeScript. Create dangerfile.ts in the project root with some rules:
import { danger, warn } from "danger"
import * as _ from "lodash"
// Request a CHANGELOG entry if not declared #trivial
const hasChangelog = _.includes(danger.git.modified_files, "changelog.md")
const isTrivial = _.includes((danger.github.pr.body + danger.github.pr.title), "#trivial")
if (!hasChangelog && !isTrivial) {
warn("Please add a changelog entry for your changes.")
// Politely ask for their name on the entry too
const changelogDiff = danger.git.diffForFile("changelog.md")
const contributorName = danger.github.pr.user.login
if (changelogDiff && _.includes(changelogDiff, contributorName)) {
warn("Please add your GitHub name to the changelog entry, so we can attribute you correctly.")
}
}Using Jest and TypeScript for testing? You're all set - Danger should be able to use your jest config in package.json to process and evaulate your dangerfile.ts.
Not using Jest on your TypeScript project? You'll need to take the following steps for danger to evaluate your dangerfile.ts:
- Install the
ts-jestpackage -yarn add ts-jest --dev - Add the following
jestsection to yourpackage.json
{
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
}
}
}Then you add yarn run danger to the end of your CI run, and Danger will run. Here's an example.
Want to see some existing JavaScript examples? Check out:
- Apps - Artsy/metaphysics.
- Libraries - Facebook/Jest, styled-components/styled-components and ReactiveX/rxjs.
Some TypeScript examples:
- Apps - Artsy/Emission
- Libraries danger/danger-js
I'd love PRs adding more.
Getting set up on CI
For now, to get set up I'd recommend looking at the setup guide for the Ruby version. All the environment vars are the exact same between versions.
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:
sut:
build: .
environment:
- DANGER_GITHUB_API_TOKEN
- DOCKER_REPO
- PULL_REQUEST_URL
- SOURCE_REPOSITORY_URLRunning/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.
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.
Using danger
If you create an appropriately scoped temporary api token for your github account, this can be a good way to see if danger is suitable for you before integrating it into your CI system.
You can manually trigger danger against a pull request on the command line by setting the following environmental variables:
export DANGER_FAKE_CI="YEP"
export DANGER_GITHUB_API_TOKEN='xxxxxxxxxx' # a github api token
export DANGER_TEST_REPO='username/reponame'Then you can run against a local branch that is attached to a pull-request, by running the following
git checkout branch-for-pr-1234
DANGER_TEST_PR='1234' npm run dangerassuming 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.
Advice
- You can have Danger read build logs if you use tee in your CI process:
yarn run lint | tee linter_output.txt. This can then be picked up withreadFileSyncatlinter_output.txtin your Dangerfile later.
Known issues
- Codeship support does not support fork to fork GitHub PRs.
This thing is broken, I should help improve it!
Awesommmmee.
git clone https://github.com/danger/danger-js.git
cd danger-js
# if you don't have yarn installed
npm install -g yarn
yarn installYou can then verify your install by running the tests, and the linters:
yarn test
yarn lintDev Life
Tips:
- You can run the
dangercommand globally from your dev build by runningyarn run link. - If you're using VS Code, press Run to start an example run, should go through most of the process with a debugger attached. Either use breakpoints, or add
debuggerto get a repl and context.
What is the TODO?
Check the issues, I try and keep my short term perspective there. Long term is in the VISION.md.
License, Contributor's Guidelines and Code of Conduct
We try to keep as much discussion as possible in GitHub issues, but also have a pretty inactive Slack --- if you'd like an invite, ping @Orta a DM on Twitter with your email. It's mostly interesting if you want to stay on top of Danger without all the emails from GitHub.
This project is open source under the MIT license, which means you have full access to the source code and can modify it to fit your own needs.
This project subscribes to the Moya Contributors Guidelines which TLDR: means we give out push access easily and often.
Contributors subscribe to the Contributor Code of Conduct based on the Contributor Covenant version 1.3.0.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
