Fix Unprocessable Entity (RuntimeError) RuntimeError (#18)

fixes #16
fixes #17

Also updates the docs and moves to a smaller ruby image.
This commit is contained in:
Andrew Mason 2019-10-24 22:15:35 -04:00 committed by GitHub
parent c3f0a167d0
commit 9f15426bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 43 deletions

View File

@ -1,4 +1,6 @@
FROM ruby:2.6 FROM ruby:2.6.5-alpine
RUN apk add --update build-base git
LABEL com.github.actions.name="Rubocop Linter" LABEL com.github.actions.name="Rubocop Linter"
LABEL com.github.actions.description="Lint your Ruby code" LABEL com.github.actions.description="Lint your Ruby code"
@ -8,4 +10,6 @@ LABEL com.github.actions.color="red"
LABEL maintainer="Andrew Mason <andrewmcodes@protonmail.com>" LABEL maintainer="Andrew Mason <andrewmcodes@protonmail.com>"
COPY lib /action/lib COPY lib /action/lib
RUN gem install bundler
ENTRYPOINT ["/action/lib/entrypoint.sh"] ENTRYPOINT ["/action/lib/entrypoint.sh"]

View File

@ -2,9 +2,11 @@
# Rubocop Linter Action # Rubocop Linter Action
![version number](https://img.shields.io/static/v1?label=Version&message=v0.1.2&color=blue) ![version number](https://img.shields.io/static/v1?label=Version&message=v0.2.0&color=blue)
GitHub Action to run Rubocop against your code. GitHub Action to run Rubocop against your code and create annotations in the UI.
**NOTE: due to the GitHub Check Runs API, we can only return 50 annotations per run. See [here](https://developer.github.com/v3/checks/runs/#output-object) for more info.**
## Usage ## Usage
@ -12,7 +14,7 @@ Add the following to your GitHub action workflow:
```yaml ```yaml
- name: Rubocop Linter - name: Rubocop Linter
uses: andrewmcodes/rubocop-linter-action@v0.1.2 uses: andrewmcodes/rubocop-linter-action@v0.2.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
@ -20,27 +22,23 @@ Add the following to your GitHub action workflow:
## Example Workflow ## Example Workflow
```yaml ```yaml
name: Ruby name: Rubocop
on: [push] on:
pull_request:
branches:
- '*'
push:
branches:
- master
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Set up Ruby 2.6
uses: actions/setup-ruby@v1
with:
ruby-version: 2.6.x
- name: add PostgreSQL dependencies
run: |
sudo apt-get update
sudo apt-get install -y postgresql-client libpq-dev
- name: Rubocop Linter - name: Rubocop Linter
uses: andrewmcodes/rubocop-linter-action@v0.1.2 uses: andrewmcodes/rubocop-linter-action@v0.2.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```

View File

@ -2,6 +2,6 @@
set -e set -e
gem install rubocop rubocop-performance rubocop-rails gem install rubocop rubocop-performance rubocop-rails rubocop-minitest rubocop-rspec
ruby /action/lib/index.rb ruby /action/lib/index.rb

View File

@ -11,9 +11,8 @@ require 'time'
@repository = @event["repository"] @repository = @event["repository"]
@owner = @repository["owner"]["login"] @owner = @repository["owner"]["login"]
@repo = @repository["name"] @repo = @repository["name"]
@conclusion = "success"
@check_name = "Rubocop" @check_name = "Rubocop"
@headers = { @headers = {
"Content-Type": 'application/json', "Content-Type": 'application/json',
"Accept": 'application/vnd.github.antiope-preview+json', "Accept": 'application/vnd.github.antiope-preview+json',
@ -46,8 +45,6 @@ end
def update_check(id, conclusion, output) def update_check(id, conclusion, output)
body = { body = {
"name" => @check_name, "name" => @check_name,
"head_sha" => @GITHUB_SHA,
"status" => 'completed',
"completed_at" => Time.now.iso8601, "completed_at" => Time.now.iso8601,
"conclusion" => conclusion, "conclusion" => conclusion,
"output" => output "output" => output
@ -56,7 +53,6 @@ def update_check(id, conclusion, output)
http = Net::HTTP.new('api.github.com', 443) http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true http.use_ssl = true
path = "/repos/#{@owner}/#{@repo}/check-runs/#{id}" path = "/repos/#{@owner}/#{@repo}/check-runs/#{id}"
resp = http.patch(path, body.to_json, @headers) resp = http.patch(path, body.to_json, @headers)
if resp.code.to_i >= 300 if resp.code.to_i >= 300
@ -73,13 +69,25 @@ end
} }
def run_rubocop def run_rubocop
annotations = [] annotations = annotation_messages
output = {
"title": @check_name,
"summary": "#{annotations.size} offense(s) found",
"annotations" => annotations
}
return { "output" => output, "conclusion" => @conclusion }
end
def annotation_messages
errors = nil errors = nil
count = 0
annotation_list = []
Dir.chdir(@GITHUB_WORKSPACE) { Dir.chdir(@GITHUB_WORKSPACE) {
errors = JSON.parse(`rubocop --format json`) errors = JSON.parse(`rubocop --format json`)
} }
conclusion = "success"
count = 0
errors["files"].each do |file| errors["files"].each do |file|
path = file["path"] path = file["path"]
@ -92,11 +100,13 @@ def run_rubocop
annotation_level = @annotation_levels[severity] annotation_level = @annotation_levels[severity]
count = count + 1 count = count + 1
return annotation_list if count == 48
if annotation_level == "failure" if annotation_level == "failure"
conclusion = "failure" @conclusion = "failure"
end end
annotations.push({ annotation_list.push({
"path" => path, "path" => path,
"start_line" => location["start_line"], "start_line" => location["start_line"],
"end_line" => location["start_line"], "end_line" => location["start_line"],
@ -105,14 +115,7 @@ def run_rubocop
}) })
end end
end end
return annotation_list
output = {
"title": @check_name,
"summary": "#{count} offense(s) found",
"annotations" => annotations
}
return { "output" => output, "conclusion" => conclusion }
end end
def run def run
@ -121,7 +124,6 @@ def run
results = run_rubocop() results = run_rubocop()
conclusion = results["conclusion"] conclusion = results["conclusion"]
output = results["output"] output = results["output"]
update_check(id, conclusion, output) update_check(id, conclusion, output)
rescue rescue
update_check(id, "failure", nil) update_check(id, "failure", nil)