├── screenshots └── annotation.png ├── lib ├── entrypoint.sh ├── request.js └── run.js ├── README.md ├── Dockerfile ├── LICENSE └── .gitignore /screenshots/annotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gimenete/eslint-action/HEAD/screenshots/annotation.png -------------------------------------------------------------------------------- /lib/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | npm install 6 | 7 | NODE_PATH=node_modules node /action/lib/run.js 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESLint Action 2 | 3 | Offload your CI of running ESLint. With this action you can run it in parallel to your CI process, which means, faster builds! 4 | 5 | This action will also annotate the diff with the errors and warnings reported by ESLint. 6 | 7 | ![](screenshots/annotation.png) 8 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:10-slim 2 | 3 | LABEL com.github.actions.name="ESLint checks" 4 | LABEL com.github.actions.description="Lint your code with eslint in parallel to your builds" 5 | LABEL com.github.actions.icon="code" 6 | LABEL com.github.actions.color="yellow" 7 | 8 | LABEL maintainer="Alberto Gimeno " 9 | 10 | COPY lib /action/lib 11 | ENTRYPOINT ["/action/lib/entrypoint.sh"] 12 | -------------------------------------------------------------------------------- /lib/request.js: -------------------------------------------------------------------------------- 1 | const https = require('https') 2 | 3 | module.exports = (url, options) => { 4 | return new Promise((resolve, reject) => { 5 | const req = https 6 | .request(url, options, res => { 7 | let data = '' 8 | res.on('data', chunk => { 9 | data += chunk 10 | }) 11 | res.on('end', () => { 12 | if (res.statusCode >= 400) { 13 | const err = new Error(`Received status code ${res.statusCode}`) 14 | err.response = res 15 | err.data = data 16 | reject(err) 17 | } else { 18 | resolve({ res, data: JSON.parse(data) }) 19 | } 20 | }) 21 | }) 22 | .on('error', reject) 23 | if (options.body) { 24 | req.end(JSON.stringify(options.body)) 25 | } else { 26 | req.end() 27 | } 28 | }) 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alberto Gimeno 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /lib/run.js: -------------------------------------------------------------------------------- 1 | const request = require('./request') 2 | 3 | const { GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN, GITHUB_WORKSPACE } = process.env 4 | const event = require(GITHUB_EVENT_PATH) 5 | const { repository } = event 6 | const { 7 | owner: { login: owner } 8 | } = repository 9 | const { name: repo } = repository 10 | 11 | const checkName = 'ESLint check' 12 | 13 | const headers = { 14 | 'Content-Type': 'application/json', 15 | Accept: 'application/vnd.github.antiope-preview+json', 16 | Authorization: `Bearer ${GITHUB_TOKEN}`, 17 | 'User-Agent': 'eslint-action' 18 | } 19 | 20 | async function createCheck() { 21 | const body = { 22 | name: checkName, 23 | head_sha: GITHUB_SHA, 24 | status: 'in_progress', 25 | started_at: new Date() 26 | } 27 | 28 | const { data } = await request(`https://api.github.com/repos/${owner}/${repo}/check-runs`, { 29 | method: 'POST', 30 | headers, 31 | body 32 | }) 33 | const { id } = data 34 | return id 35 | } 36 | 37 | function eslint() { 38 | const eslint = require('eslint') 39 | 40 | const cli = new eslint.CLIEngine() 41 | const report = cli.executeOnFiles(['.']) 42 | // fixableErrorCount, fixableWarningCount are available too 43 | const { results, errorCount, warningCount } = report 44 | 45 | const levels = ['', 'warning', 'failure'] 46 | 47 | const annotations = [] 48 | for (const result of results) { 49 | const { filePath, messages } = result 50 | const path = filePath.substring(GITHUB_WORKSPACE.length + 1) 51 | for (const msg of messages) { 52 | const { line, severity, ruleId, message } = msg 53 | const annotationLevel = levels[severity] 54 | annotations.push({ 55 | path, 56 | start_line: line, 57 | end_line: line, 58 | annotation_level: annotationLevel, 59 | message: `[${ruleId}] ${message}` 60 | }) 61 | } 62 | } 63 | 64 | return { 65 | conclusion: errorCount > 0 ? 'failure' : 'success', 66 | output: { 67 | title: checkName, 68 | summary: `${errorCount} error(s), ${warningCount} warning(s) found`, 69 | annotations 70 | } 71 | } 72 | } 73 | 74 | async function updateCheck(id, conclusion, output) { 75 | const body = { 76 | name: checkName, 77 | head_sha: GITHUB_SHA, 78 | status: 'completed', 79 | completed_at: new Date(), 80 | conclusion, 81 | output 82 | } 83 | 84 | await request(`https://api.github.com/repos/${owner}/${repo}/check-runs/${id}`, { 85 | method: 'PATCH', 86 | headers, 87 | body 88 | }) 89 | } 90 | 91 | function exitWithError(err) { 92 | console.error('Error', err.stack) 93 | if (err.data) { 94 | console.error(err.data) 95 | } 96 | process.exit(1) 97 | } 98 | 99 | async function run() { 100 | const id = await createCheck() 101 | try { 102 | const { conclusion, output } = eslint() 103 | console.log(output.summary) 104 | await updateCheck(id, conclusion, output) 105 | if (conclusion === 'failure') { 106 | process.exit(78) 107 | } 108 | } catch (err) { 109 | await updateCheck(id, 'failure') 110 | exitWithError(err) 111 | } 112 | } 113 | 114 | run().catch(exitWithError) 115 | --------------------------------------------------------------------------------