initial commit
This commit is contained in:
commit
f412d3cc83
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/tmp/
|
||||||
|
/.vscode/
|
||||||
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -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 <andrewmcodes@protonmail.com>"
|
||||||
|
|
||||||
|
COPY lib /action/lib
|
||||||
|
ENTRYPOINT ["/action/lib/entrypoint.sh"]
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -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.
|
||||||
8
lib/action.json
Normal file
8
lib/action.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"repository": {
|
||||||
|
"owner": {
|
||||||
|
"login": "andrewmcodes"
|
||||||
|
}
|
||||||
|
,"name": "rubocop-linter"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
lib/entrypoint.sh
Executable file
7
lib/entrypoint.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
gem install rubocop rubocop-performance rubocop-rails
|
||||||
|
|
||||||
|
ruby /action/lib/index.rb
|
||||||
134
lib/index.rb
Normal file
134
lib/index.rb
Normal file
@ -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()
|
||||||
0
screenshots/.keep
Normal file
0
screenshots/.keep
Normal file
Reference in New Issue
Block a user