From 9f15426bbf28f623705f194afe9f27d3d093e15b Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Thu, 24 Oct 2019 22:15:35 -0400 Subject: [PATCH] Fix Unprocessable Entity (RuntimeError) RuntimeError (#18) fixes #16 fixes #17 Also updates the docs and moves to a smaller ruby image. --- Dockerfile | 6 +++++- README.md | 30 +++++++++++++-------------- lib/entrypoint.sh | 2 +- lib/index.rb | 52 ++++++++++++++++++++++++----------------------- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/Dockerfile b/Dockerfile index 865de90..98bd15c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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.description="Lint your Ruby code" @@ -8,4 +10,6 @@ LABEL com.github.actions.color="red" LABEL maintainer="Andrew Mason " COPY lib /action/lib +RUN gem install bundler + ENTRYPOINT ["/action/lib/entrypoint.sh"] diff --git a/README.md b/README.md index 6c52612..1d0bebe 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,11 @@ # 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 @@ -12,7 +14,7 @@ Add the following to your GitHub action workflow: ```yaml - name: Rubocop Linter - uses: andrewmcodes/rubocop-linter-action@v0.1.2 + uses: andrewmcodes/rubocop-linter-action@v0.2.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` @@ -20,27 +22,23 @@ Add the following to your GitHub action workflow: ## Example Workflow ```yaml -name: Ruby +name: Rubocop -on: [push] +on: + pull_request: + branches: + - '*' + push: + branches: + - master jobs: build: - runs-on: ubuntu-latest - steps: - 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 - uses: andrewmcodes/rubocop-linter-action@v0.1.2 + uses: andrewmcodes/rubocop-linter-action@v0.2.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` diff --git a/lib/entrypoint.sh b/lib/entrypoint.sh index e272643..8a107ff 100755 --- a/lib/entrypoint.sh +++ b/lib/entrypoint.sh @@ -2,6 +2,6 @@ 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 diff --git a/lib/index.rb b/lib/index.rb index 395b4c0..3315e5e 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -11,9 +11,8 @@ require 'time' @repository = @event["repository"] @owner = @repository["owner"]["login"] @repo = @repository["name"] - +@conclusion = "success" @check_name = "Rubocop" - @headers = { "Content-Type": 'application/json', "Accept": 'application/vnd.github.antiope-preview+json', @@ -46,8 +45,6 @@ end def update_check(id, conclusion, output) body = { "name" => @check_name, - "head_sha" => @GITHUB_SHA, - "status" => 'completed', "completed_at" => Time.now.iso8601, "conclusion" => conclusion, "output" => output @@ -56,7 +53,6 @@ def update_check(id, conclusion, output) http = Net::HTTP.new('api.github.com', 443) http.use_ssl = true path = "/repos/#{@owner}/#{@repo}/check-runs/#{id}" - resp = http.patch(path, body.to_json, @headers) if resp.code.to_i >= 300 @@ -73,13 +69,25 @@ end } 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 + count = 0 + annotation_list = [] + Dir.chdir(@GITHUB_WORKSPACE) { errors = JSON.parse(`rubocop --format json`) } - conclusion = "success" - count = 0 errors["files"].each do |file| path = file["path"] @@ -92,27 +100,22 @@ def run_rubocop annotation_level = @annotation_levels[severity] count = count + 1 + return annotation_list if count == 48 + if annotation_level == "failure" - conclusion = "failure" + @conclusion = "failure" end - annotations.push({ - "path" => path, - "start_line" => location["start_line"], - "end_line" => location["start_line"], - "annotation_level": annotation_level, - "message" => message - }) + annotation_list.push({ + "path" => path, + "start_line" => location["start_line"], + "end_line" => location["start_line"], + "annotation_level": annotation_level, + "message" => message + }) end end - - output = { - "title": @check_name, - "summary": "#{count} offense(s) found", - "annotations" => annotations - } - - return { "output" => output, "conclusion" => conclusion } + return annotation_list end def run @@ -121,7 +124,6 @@ def run results = run_rubocop() conclusion = results["conclusion"] output = results["output"] - update_check(id, conclusion, output) rescue update_check(id, "failure", nil)