pmd/Dangerfile

99 lines
3.4 KiB
Plaintext
Raw Normal View History

2018-08-02 22:35:09 +08:00
require 'pmdtester'
2018-07-23 23:06:46 +08:00
require 'time'
require 'logger'
require 'fileutils'
require 'etc'
2018-07-23 23:06:46 +08:00
@logger = Logger.new(STDOUT)
2024-02-15 21:25:08 +01:00
def get_args(base_branch, autogen = true, patch_config = './pmd/.ci/files/all-regression-rules.xml')
['--local-git-repo', './pmd',
'--list-of-project', './pmd/.ci/files/project-list.xml',
'--base-branch', base_branch,
'--patch-branch', 'HEAD',
'--patch-config', patch_config,
'--mode', 'online',
autogen ? '--auto-gen-config' : '--filter-with-patch-config',
'--keep-reports',
'--error-recovery',
'--baseline-download-url', 'https://pmd-code.org/pmd-regression-tester/',
'--threads', Etc.nprocessors.to_s,
# '--debug',
]
end
2018-07-23 23:06:46 +08:00
def run_pmdtester
Dir.chdir('..') do
2018-09-10 21:16:42 +08:00
begin
@base_branch = ENV['PMD_CI_BRANCH']
@logger.info "\n\n--------------------------------------"
@logger.info "Run against PR base #{@base_branch}"
@summary = PmdTester::Runner.new(get_args(@base_branch)).run
unless Dir.exist?('target/reports/diff')
message("No regression tested rules have been changed.", sticky: true)
return
end
# move the generated report out of the way
FileUtils.mv 'target/reports/diff', 'target/diff1'
message1 = create_message
# run against main branch (if the PR is not already against main)
unless ENV['PMD_CI_BRANCH'] == 'main'
@base_branch = 'main'
@logger.info "\n\n--------------------------------------"
@logger.info "Run against #{@base_branch}"
@summary = PmdTester::Runner.new(get_args(@base_branch, false, 'target/diff1/patch_config.xml')).run
# move the generated report out of the way
FileUtils.mv 'target/reports/diff', 'target/diff2'
message2 = create_message
end
tar_report
message1 += "[Download full report as build artifact](#{ENV['PMD_CI_JOB_URL']}?pr=#{ENV['PMD_CI_PULL_REQUEST_NUMBER']})"
# set value of sticky to true and the message is kept after new commits are submitted to the PR
message(message1, sticky: true)
if message2
message2 += "[Download full report as build artifact](#{ENV['PMD_CI_JOB_URL']}?pr=#{ENV['PMD_CI_PULL_REQUEST_NUMBER']})"
# set value of sticky to true and the message is kept after new commits are submitted to the PR
message(message2, sticky: true)
end
2018-09-10 21:16:42 +08:00
rescue StandardError => e
warn("Running pmdtester failed, this message is mainly used to remind the maintainers of PMD.")
@logger.error "Running pmdtester failed: #{e.inspect}"
end
2018-07-23 23:06:46 +08:00
end
end
def create_message
"Compared to #{@base_branch}:\n"\
"This changeset " \
"changes #{@summary[:violations][:changed]} violations,\n" \
"introduces #{@summary[:violations][:new]} new violations, " \
"#{@summary[:errors][:new]} new errors and " \
"#{@summary[:configerrors][:new]} new configuration errors,\n" \
"removes #{@summary[:violations][:removed]} violations, "\
"#{@summary[:errors][:removed]} errors and " \
"#{@summary[:configerrors][:removed]} configuration errors.\n"
end
def tar_report
Dir.chdir('target') do
tar_filename = "pr-#{ENV['PMD_CI_PULL_REQUEST_NUMBER']}-diff-report-#{Time.now.strftime("%Y-%m-%dT%H-%M-%SZ")}.tar.gz"
2018-07-23 23:06:46 +08:00
`tar czf #{tar_filename} diff1/ diff2/`
tar_size = (10 * File.size(tar_filename) / 1024 / 1024)/10.0
@logger.info "Created file #{tar_filename} (#{tar_size}mb)"
2018-07-23 23:06:46 +08:00
end
end
# Perform regression testing
2020-11-12 13:11:56 +01:00
run_pmdtester
2018-07-23 23:06:46 +08:00
# vim: syntax=ruby