Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcodes committed Sep 3, 2019
0 parents commit f412d3c
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tmp/
/.vscode/
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ruby:2.6

LABEL com.github.actions.name="Rubocop Linter"
LABEL com.github.actions.description="Lint your Ruby code"
LABEL com.github.actions.icon="code"
LABEL com.github.actions.color="red"

LABEL maintainer="Andrew Mason <[email protected]>"

COPY lib /action/lib
ENTRYPOINT ["/action/lib/entrypoint.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Andrew Mason

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rubocop GitHub Action

TODO
8 changes: 8 additions & 0 deletions lib/action.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"repository": {
"owner": {
"login": "andrewmcodes"
}
,"name": "rubocop-linter"
}
}
7 changes: 7 additions & 0 deletions lib/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

set -e

gem install rubocop rubocop-performance rubocop-rails

ruby /action/lib/index.rb
134 changes: 134 additions & 0 deletions lib/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require 'net/http'
require 'json'
require 'time'

@GITHUB_SHA = ENV["GITHUB_SHA"]
@GITHUB_EVENT_PATH = ENV["GITHUB_EVENT_PATH"]
@GITHUB_TOKEN = ENV["GITHUB_TOKEN"]
@GITHUB_WORKSPACE = ENV["GITHUB_WORKSPACE"]

@event = JSON.parse(File.read(ENV["GITHUB_EVENT_PATH"]))
@repository = @event["repository"]
@owner = @repository["owner"]["login"]
@repo = @repository["name"]

@check_name = "Rubocop"

@headers = {
"Content-Type": 'application/json',
"Accept": 'application/vnd.github.antiope-preview+json',
"Authorization": "Bearer #{@GITHUB_TOKEN}",
"User-Agent": 'rubocop-action'
}

def create_check
body = {
"name" => @check_name,
"head_sha" => @GITHUB_SHA,
"status" => "in_progress",
"started_at" => Time.now.iso8601
}

http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true
path = "/repos/#{@owner}/#{@repo}/check-runs"

resp = http.post(path, body.to_json, @headers)

if resp.code.to_i >= 300
raise resp.message
end

data = JSON.parse(resp.body)
return data["id"]
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
}

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
raise resp.message
end
end

@annotation_levels = {
"refactor" => 'failure',
"convention" => 'failure',
"warning" => 'warning',
"error" => 'failure',
"fatal" => 'failure'
}

def run_rubocop
annotations = []
errors = nil
Dir.chdir(@GITHUB_WORKSPACE) {
errors = JSON.parse(`rubocop --format json`)
}
conclusion = "success"
count = 0

errors["files"].each do |file|
path = file["path"]
offenses = file["offenses"]

offenses.each do |offense|
severity = offense["severity"]
message = offense["message"]
location = offense["location"]
annotation_level = @annotation_levels[severity]
count = count + 1

if annotation_level == "failure"
conclusion = "failure"
end

annotations.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 }
end

def run
id = create_check()
begin
results = run_rubocop()
conclusion = results["conclusion"]
output = results["output"]

update_check(id, conclusion, output)

fail if conclusion == "failure"
rescue
update_check(id, "failure", nil)
fail
end
end

run()
Empty file added screenshots/.keep
Empty file.

0 comments on commit f412d3c

Please sign in to comment.