Skip to content

Commit

Permalink
RuboCop action
Browse files Browse the repository at this point in the history
  • Loading branch information
gimenete committed Jan 9, 2019
1 parent 831ef84 commit 4cc8d8f
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 1 deletion.
5 changes: 4 additions & 1 deletion eslint/lib/entrypoint.sh
@@ -1,4 +1,7 @@
#!/bin/sh

set -e

npm install
NODE_PATH=node_modules node /action/lib/create-check.js

NODE_PATH=node_modules node /action/lib/run.js
File renamed without changes.
11 changes: 11 additions & 0 deletions rubocop/Dockerfile
@@ -0,0 +1,11 @@
FROM node:10-slim

LABEL com.github.actions.name="RuboCop"
LABEL com.github.actions.description=""
LABEL com.github.actions.icon="code"
LABEL com.github.actions.color="red"
LABEL repository=""
LABEL maintainer="Alberto Gimeno <gimenete@gmail.com>"

COPY lib /action/lib
ENTRYPOINT ["/action/lib/entrypoint.sh"]
7 changes: 7 additions & 0 deletions rubocop/lib/entrypoint.sh
@@ -0,0 +1,7 @@
#!/bin/sh

set -e

gem install rubocop

node /action/lib/run.js
29 changes: 29 additions & 0 deletions rubocop/lib/request.js
@@ -0,0 +1,29 @@
const https = require('https')

module.exports = (url, options) => {
return new Promise((resolve, reject) => {
const req = https
.request(url, options, res => {
let data = ''
res.on('data', chunk => {
data += chunk
})
res.on('end', () => {
if (res.statusCode >= 400) {
const err = new Error(`Received status code ${res.statusCode}`)
err.response = res
err.data = data
reject(err)
} else {
resolve({ res, data: JSON.parse(data) })
}
})
})
.on('error', reject)
if (options.body) {
req.end(JSON.stringify(options.body))
} else {
req.end()
}
})
}
122 changes: 122 additions & 0 deletions rubocop/lib/run.js
@@ -0,0 +1,122 @@
const { execSync } = require('child_process')
const request = require('./request')

const { GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN } = process.env
const event = require(GITHUB_EVENT_PATH)
const { repository } = event
const {
owner: { login: owner }
} = repository
const { name: repo } = repository

const checkName = 'ESLint check'

const headers = {
'Content-Type': 'application/json',
Accept: 'application/vnd.github.antiope-preview+json',
Authorization: `Bearer ${GITHUB_TOKEN}`,
'User-Agent': 'eslint-action'
}

async function createCheck() {
const body = {
name: checkName,
head_sha: GITHUB_SHA,
status: 'in_progress',
started_at: new Date()
}

const { data } = await request(`https://api.github.com/repos/${owner}/${repo}/check-runs`, {
method: 'POST',
headers,
body
})
const { id } = data
return id
}

function rubocop() {
const result = spawnSync('rubocop', ['--format', 'json'])

const { status, output } = result
const [, stderr] = output
const report = JSON.parse(stderr)

const annotationLevels = {
refactor: 'failure',
convention: 'failure',
warning: 'warning',
error: 'failure',
fatal: 'failure'
}

const { files, summary } = report

const annotations = []
for (const file of files) {
const { path, offenses } = file
for (const offense of offenses) {
const { severity, message, location } = offense
const { start_line, last_line: end_line } = location

annotations.push({
path,
start_line,
end_line,
annotation_level: annotationLevels[severity],
message
})
}
}

return {
conclusion: status > 0 ? 'failure' : 'success',
output: {
title: checkName,
summary: `${summary.offense_count} offense(s) found`,
annotations
}
}
}

async function updateCheck(id, conclusion, output) {
const body = {
name: checkName,
head_sha: GITHUB_SHA,
status: 'completed',
completed_at: new Date(),
conclusion,
output
}

await request(`https://api.github.com/repos/${owner}/${repo}/check-runs/${id}`, {
method: 'PATCH',
headers,
body
})
}

function exitWithError(err) {
console.error('Error', err.stack)
if (err.data) {
console.error(err.data)
}
process.exit(1)
}

async function run() {
const id = await createCheck()
try {
const { conclusion, output } = rubocop()
console.log(output.summary)
await updateCheck(id, conclusion, output)
if (conclusion === 'failure') {
process.exit(78)
}
} catch (err) {
await updateCheck(id, 'failure')
exitWithError(err)
}
}

run().catch(exitWithError)

0 comments on commit 4cc8d8f

Please sign in to comment.