diff --git a/.workflow/check_run-pipeline.yml b/.workflow/check_run-pipeline.yml index 2774eb33be2515da28284dfaf593ee065993b7ed..cd40c869a9ffb16bd9a61d7d87c2ec5c94aa6a31 100644 --- a/.workflow/check_run-pipeline.yml +++ b/.workflow/check_run-pipeline.yml @@ -8,12 +8,21 @@ stages: steps: - step: build@ruby name: build_ruby_31 - displayName: Ruby 构建 + displayName: Rubocop检测 rubyVersion: 2.6.8 - commands: | - echo `pwd` - echo `ls` - ruby "./check_run_script.rb" + commands: + - Int: + - echo `ruby -v` + - echo `pwd` + - Install RubyENV: + - gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/ + - gem install rest-client + - gem install json + - gem install rubocop + - gem install base64 + + - Run Script: + - ruby "./script/rubocop_check.rb" ${AGILE_MODULE_NAME} ${AGILE_SOURCE_REVISION} ${AGILE_REVISION} ${ACCESS_TOKEN} triggers: pr: branches: diff --git a/check_run_script.rb b/check_run_script.rb index 2c931ef6973e961741b49c1db16310264b746201..317618b8f16165a7108117ae075a659eaa2db33d 100644 --- a/check_run_script.rb +++ b/check_run_script.rb @@ -2,119 +2,118 @@ require 'rest-client' require 'json' class RubocopAnalysis - def initialize(head_sha) + def initialize(repo, head_sha, bare_sha, access_token_str) - @repo = "liwen/gitos" - @access_token = "2339922d27630f723f566e9a2ace6a6f" + @repo = repo + puts "access_token_str: #{access_token_str}" + @access_token = Base64.decode(access_token_str)[0...32] + puts "@access_token: #{@access_token}" @head_sha = head_sha + @bare_sha = bare_sha end def exec - puts "#{@repo}, #{@access_token}, #{@head_sha}" create_rubocop - rubocop_exec + initiate_check_run end def create_rubocop - puts "create_rubocop" data = { details_url: 'https://gitee.com/liwen', name: 'liwen_rubocop', - access_token: @access_token, head_sha: @head_sha } url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs" - @response_json = RestClient.post url, data + @response_json = RestClient.post url, data, {'Private-Token' => @access_token} @response = JSON.parse(@response_json) @check_run_id = @response['id'] puts "@check_run_id => #{@check_run_id}" end - def progress_check_run - puts "progress_check_run" + def initiate_check_run url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs/#{@check_run_id}" data = { access_token: @access_token, status: 'in_progress' } - RestClient.patch url, data - end + repos = RestClient.patch url, data + + + diff_paths = `git diff --name-only #{@head_sha} #{@bare_sha}` + puts "diff_paths: #{diff_paths}" + @report = `rubocop --format json #{diff_paths}` + @output = JSON.parse @report - def analysis_annotation_conclusion(output) - puts "analysis_annotation_conclusion" annotations = [] - conclusion = 'success' - annotation_level_list = {"warning" => "warning", "error" => "failure", "fatal" => "failure"} - output['files'].each do |file| - - file['offenses'].each do |offense| - conclusion = "failure" if conclusion == 'success' && ["error", "fatal"].include?(offense["severity"]) - start_line = offense['location']['start_line'] - end_line = offense['location']['last_line'] - start_column = offense['location']['start_column'] - end_column = offense['location']['last_column'] - message = offense['message'] - path = offense['path'] - annotation_level = annotation_level_list[offense["severity"]] - - annotation = { - path: path, - start_line: start_line, - end_line: end_line, - annotation_level: annotation_level, - message: message + + if @output['summary']['offense_count'] == 0 + conclusion = 'success' + else + conclusion = 'success' + @output['files'].each do |file| + annotation_level_list = { + "warning" => "warning", + "error" => "failure", + "fatal" => "failure" } - if start_line == end_line - annotation.merge(start_column: start_column, end_column: end_column) + file['offenses'].each do |offense| + next unless annotation_level_list.keys.include?(offense["severity"]) + + if ["error", "fatal"].include?(offense["severity"]) + conclusion = "failure" + elsif conclusion == 'success' && "warning" == offense["severity"].to_s + conclusion = "neutral" + end + + start_line = offense['location']['start_line'] + end_line = offense['location']['last_line'] + start_column = offense['location']['start_column'] + end_column = offense['location']['last_column'] + message = offense['message'] + annotation_level = annotation_level_list[offense["severity"]] + + annotation = { + path: file['path'], + start_line: start_line, + end_line: end_line, + annotation_level: annotation_level, + message: message + } + if start_line == end_line + annotation.merge(start_column: start_column, end_column: end_column) + end + + annotations.push(annotation) end - - annotations.push(annotation) end end - [annotations, conclusion] - end - - def rubocop_exec - progress_check_run - puts "rubocop_exec" - - @report = `rubocop --format json` - puts "rubocop format json" - @output = JSON.parse @report - - annotations, conclusion = analysis_annotation_conclusion(@output) - puts "summary" summary = "Octo RuboCop summary\n-Offense count: #{@output['summary']['offense_count']}\n-File count: #{@output['summary']['target_file_count']}\n-Target file count: #{@output['summary']['inspected_file_count']}" text = "Octo RuboCop version: #{@output['metadata']['rubocop_version']}" url = "https://gitee.com/api/v5/repos/#{@repo}/check-runs/#{@check_run_id}" - puts "data" data = { access_token: @access_token, - conclusion: conclusion, + conclusion: "failure", details_url: "https://gitee.com/liwen", output: { title: 'Octo RuboCop', summary: summary, text: text, - annotations: annotations[1..50] + annotations: annotations } } - puts "#{url}" - response = RestClient.patch url, data - puts response + reposponse = RestClient.patch url, data + puts "finished: #{reposponse}" end end inputted_strings = ARGV -puts "You inputted: " if ARGV.empty? puts "nothing :(" else - inputted_strings.each do |str| - puts str - RubocopAnalysis.new(str).exec - end + repo, head_sha, bare_sha, access_token_str = inputted_strings + puts "repo: #{repo}, head_sha: #{head_sha}, bare_sha: #{bare_sha}" + RubocopAnalysis.new(head_sha, bare_sha).exec end \ No newline at end of file