Automated ESLint Checking with Gitlab Web Hooks
ESLint, a pluggable JavaScript linting tool — how can we integrate it with Gitlab and apply it to development?
Gitlab Web Hook
Gitlab Web Hook provides hooks for the following events:
- Push events
- Tag push events
- Comments
- Issues events
- Merge Request events
…
When a corresponding event occurs, it triggers a preset URL (the Web Hook) and sends a POST request containing detailed information about the event. It is through this POST request that we process each event.
For example, Merge Requests events:
1 | //Request body: |
From this request body, we can learn the Merge Request’s creator, branch, source branch information, target branch information, the most recent commit, the assigned user, the corresponding status, and other details.
ESLint
As a pluggable linting solution, ESLint gradually overtook the original JSLint and JSHint, and “absorbed” JSCS. Its greatest advantage is its pluggable architecture, with various plugins available for extension, enabling developers to flexibly configure rules. If that still doesn’t meet your needs, you can easily develop plugins tailored to your own requirements. Additionally, it supports ES6 and JSX syntax. Frontend developers are truly a fashionable bunch — you can’t stop a frontend developer from using new tools…
Writing Your Own Config/Plugin npm Package
Two approaches:
- The first is similar to eslint-google-config, by writing a configuration file npm package. See the official tutorial for details.
- The second is in plugin form, similar to eslint-plugin-react-native, by writing a standalone ESLint plugin.
This article uses the second approach, which not only makes it easy to configure existing rules but also to add custom rules in the future:
1 | import ReactEslint from 'eslint-plugin-react'; |
Assuming your plugin is called eslint-react-demo, install the plugin (provided you’ve published it via npm):
1 | npm install eslint-react-demo babel-eslint eslint --save-dev |
Then configure the following .eslintrc file in the project root:
1 | { |
Implementing the Gitlab Web Hook with Node
Now that we have the plugin, let’s talk about how to implement automated ESLint checking. Since we currently use an Issue-based development workflow, when developers finish a feature and need to merge into the master branch, they must submit a Merge Request. By listening to and intercepting the Merge Request event, the corresponding Web Hook needs to accomplish the following tasks:
- Determine the Merge Request event’s trigger action — if it’s open or reopen, execute the ESLint check
- Extract the git URL, branch, and other information from the POST request body, then clone the corresponding repository locally
- Execute the ESLint check
- If the ESLint check passes, do nothing; otherwise, post the ESLint results as a comment on the Merge Request page and close the Merge Request (alternatively, you could email the developer)
Key code:
1 | check(mr) { |
The shell script used:
1 | !/bin/bash |
The End
Automated ESLint Checking with Gitlab Web Hooks
http://quanru.github.io/2016/10/02/Automated-ESLint-Checking-with-Gitlab-Web-Hooks

