A working example of Danger JS

Integrate Danger JS in 5 Minutes

Automation tool to check a pull request

3 min readJul 7, 2018

--

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

Install Danger JS as devDependencies

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.

Create a GitHub access token

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.

Setup the environment variable

4. Create a dangerfile.js

Create a dangerfile.js under your project root directory. You can use the following as example.

https://github.com/ivan-ha/playground-dangerjs/blob/development/dangerfile.js

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.

Run Danger JS against any opened pull request

Then you should be able to see something like this.

Test Danger JS locally

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.

Set up environment variables

Then, run Danger JS with

Comment on GitHub using Danger JS

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!

That’s it, you should be ready to implement your own checking logic.

--

--