Integrate Danger JS in 5 Minutes
I recently integrated Danger JS into one of my working repository. It’s a automation checker against any opened pull requests.
This article walkthrough how to set up such integration in GitHub within 5 minutes.
1. Install Danger JS
Add Danger JS as your devDependencies, you can do it via
2. Setup a GitHub token
Actually this step is optional if you are working on open source project. But you definitely want to set that up in GitHub otherwise you will hit the API limit very quickly.
Name it whatever you like and it’s enough to just grant it the repo scope. Never grant more than enough for better security control. Copy the token as shown, you won’t be able to get it back.
3. Store the token in environment variable
You need to store the token as DANGER_GITHUB_API_TOKEN
.
4. Create a dangerfile.js
Create a dangerfile.js
under your project root directory. You can use the following as example.
5. Test it locally
One good thing of Danger JS is it’s very easy to set up a local development environment. You can run it locally against an opened pull request. This will not leave real comment in GitHub.
Then you should be able to see something like this.
6. Comment on GitHub
Or, if you want, it can really leave a comment on the real pull request.
First, you need to setup more environment variables as follow.
Then, run Danger JS with
You should see a new comment created on the real GitHub pull request. You can consider this step as developing/testing the dangerfile.js
to make sure the logic inside is alright before proceeding running it in CI.
7. Setup your CI
I won’t cover this part, but it should be easy if you go through the official tutorial.
For example, to run it on Travis is as simple as
In a nut shell, Danger JS expose the pull request and repository context via the danger
object. You can then make use of the information to do whatever pattern matching or checking yourself.
But of course, you would like to checkout some dangerfile examples from other open source project!