Skip to content

Instantly share code, notes, and snippets.

@turekj
Created June 17, 2018 08:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turekj/7215bcc968e8396c9aa0e80cb8996fea to your computer and use it in GitHub Desktop.
Save turekj/7215bcc968e8396c9aa0e80cb8996fea to your computer and use it in GitHub Desktop.
Danger script for JSCPD integration
def find_duplicates
`jscpd`
unless File.exists?('jscpd_report.json')
puts "jscpd_report.json was not generated..."
return
end
jscpd_report = JSON.parse(File.read('jscpd_report.json'))
jscpd_clones = jscpd_report["statistics"]["clones"]
if jscpd_clones > 0
warn("JSCPD found #{jscpd_clones} clone(s)")
report = jscpd_report["duplicates"].map { |d| format_duplicate d }.join("\n")
markdown("## JSCPD issues\nFirst | Second \n--- | --- \n#{report}")
end
File.delete('jscpd_report.json') if File.exists?('jscpd_report.json')
end
def format_duplicate (duplicate)
lines = Integer(duplicate["lines"])
first_file = duplicate["firstFile"]["name"].split('/').last
first_file_start = Integer(duplicate["firstFile"]["start"])
first_file_stop = first_file_start + lines
second_file = duplicate["secondFile"]["name"].split('/').last
second_file_start = Integer(duplicate["secondFile"]["start"])
second_file_stop = second_file_start + lines
"#{first_file}: #{first_file_start}-#{first_file_stop} | #{second_file}: #{second_file_start}-#{second_file_stop}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment