forked from devmasx/brakeman-linter-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f412d3c
Showing
8 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/tmp/ | ||
/.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Rubocop GitHub Action | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"repository": { | ||
"owner": { | ||
"login": "andrewmcodes" | ||
} | ||
,"name": "rubocop-linter" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.