Created
November 29, 2011 02:48
-
-
Save NigelThorne/1403141 to your computer and use it in GitHub Desktop.
Report On the Worst code in your codebase (for some definition of worst) as reported by Team City and NCover
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
# Report On the Worst code in your codebase (for some definition of worst) as reported by Team City and NCover | |
# *******This works for us.. may need editing to work for you!******** | |
# By Nigel Thorne (nwt) www.nigelthorne.com | |
# https://gist.github.com/gists/1403141 | |
#install : | |
# once ruby is installed... | |
# and in your path | |
# gem install nokogiri | |
# gem install mechanize | |
# change the username password and server | |
# then run from the folder you want to recurse down from | |
# by calling "ruby worst_code.rb" | |
# from a command prompt. | |
@@USERNAME = 'username' | |
@@PASSWORD = 'password' | |
@@SERVER = 'http://server:8000' | |
@@TEAMCITYBUILDID = "bt01" | |
require 'rubygems' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'mechanize' | |
def worst(node_set) | |
node_set.map{|m| [m,score(m)] }.sort { |a,b| b[1]<=>a[1] }[0] | |
end | |
def score(node) | |
count(node) > 0 ? percent(node) * coverage(node) : 0.0 | |
end | |
def uncovered_count(node) | |
node.xpath(".//seqpnt[@vc = '0']").length | |
end | |
def count(node) | |
node.xpath(".//seqpnt").length | |
end | |
def percent(node) | |
uncovered_count(node).to_f/count(node).to_f | |
end | |
def coverage(node) | |
node['cc'].to_f | |
end | |
def old_score(node) | |
uncovered_count(node) * coverage(node) | |
end | |
def login_to_TeamCity | |
a = Mechanize.new { |agent| | |
agent.user_agent_alias = 'Mac Safari' | |
agent.auth(@@USERNAME, @@PASSWORD) | |
} | |
a.get(@@SERVER + "/httpAuth/") # log us in | |
a | |
end | |
def get_latest_build_number(a) | |
p = a.get(@@SERVER + "/viewType.html?tab=buildTypeStatusDiv&buildTypeId=#{@@TEAMCITYBUILDID}") | |
p.search(".testList").search("a").select{|x| /Tests passed:/ =~ x.text }.first[:href].match(/buildId=([0-9]+)/)[1] | |
end | |
def csv(*args) | |
args.map{|a| "\"#{a.to_s.gsub(/"/,'""')}\""}.join(",") | |
end | |
if (File.exists?("merged.xml")) | |
@content = File.read("merged.xml") | |
else | |
a = login_to_TeamCity | |
build = get_latest_build_number(a) | |
@content = a.get(@@SERVER + "/repository/download/#{@@TEAMCITYBUILDID}/#{build}:id/ncover/All/Full/merged.xml").content | |
# @content = a.get(@@SERVER + "/repository/download/#{@@TEAMCITYBUILDID}/.lastFinished/ncover/All/Full/merged.xml").content | |
x = File.open("merged.xml","w+"){|f| f << @content} | |
end | |
methods = Nokogiri.XML(@content).xpath('.//method') | |
scored_methods = methods.map{|m| [m,score(m)] }.sort { |a,b| b[1]<=>a[1] } | |
File.open("worst_code_results.csv", "w+") do |output| | |
output.write("Score,Class,Method,CC,Old Score\n") | |
output.write(scored_methods.map{|m| csv(m[1],m[0].parent['name'],m[0]['signature'],m[0]['cc'],old_score(m[0]))}.join("\n")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment