Skip to content

Instantly share code, notes, and snippets.

@JacobNinja
Last active December 20, 2015 07:09
Show Gist options
  • Save JacobNinja/6091692 to your computer and use it in GitHub Desktop.
Save JacobNinja/6091692 to your computer and use it in GitHub Desktop.
Bugwatch usage example
repo = Bugwatch::Repo.discover('bugwatch', Dir.pwd)
# => #<Bugwatch::Repo:0x00000101c98ae0 @name="bugwatch", @url="/Users/jacobr/code/bugwatch">
commit = repo.commits.first
# => #<Bugwatch::Commit:0x00000101ca0100 @attributes={:sha=>"d7ecd8b082906df27be6d973a196f32621d5ad1f", :message=>"add the rest of the headers", :diffs=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0218>:each>, :stats=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0290>:each>, :tree=>#<Bugwatch::Tree:0x00000101ca01c8 @grit=#<Grit::Tree "658dc44b7abec270b90121798e111202ac040dd9">>, :author_name=>"Jacob Richardson", :author_email=>"[email protected]", :authored_date=>2013-07-23 12:22:49 -0600, :committer_name=>"Jacob Richardson", :committer_email=>"[email protected]", :committed_date=>2013-07-23 12:22:49 -0600}>
commit.author_name
# => "Jacob Richardson"
commit.authored_date
# => 2013-07-23 12:22:49 -0600
commit.files
# => ["Gemfile", "Rakefile", "bugwatch.gemspec", "features/exceptions/commit_finder.feature", "features/step_definitions/exception_steps.rb", "features/support.rb", "test/analysis_test.rb", "test/churn_test.rb", "test/commit_test.rb", "test/complexity_score_test.rb", "test/diff_parser_test.rb", "test/diff_test.rb", "test/exception_data_test.rb", "test/exception_tracker_test.rb", "test/flog_bug_test.rb", "test/flog_score_test.rb", "test/importer_test.rb", "test/method_parser_test.rb", "test/repo_test.rb", "test/ruby_complexity_test.rb", "test/support/code.rb", "test/support/fixtures.rb", "test/tag_test.rb", "test/test_helper.rb", "test/tree_test.rb", "vendor/flog/History.txt", "vendor/flog/README.txt", "vendor/flog/lib/flog.rb", "vendor/flog/lib/flog_task.rb", "vendor/flog/lib/gauntlet_flog.rb"]
commit.complexity.score
# => -338.22751221424045
commit.complexity.scores
# => [#<Bugwatch::ComplexityScore:0x0000010209adf0 @file="features/support.rb", @before_score=7.900000000000001, @after_score=7.900000000000001>, #<Bugwatch::ComplexityScore:0x0000010209adc8 @file="test/support/code.rb", @before_score=0, @after_score=0>, #<Bugwatch::ComplexityScore:0x0000010209ada0 @file="test/support/fixtures.rb", @before_score=36.092272103596265, @after_score=36.092272103596265>, #<Bugwatch::ComplexityScore:0x0000010209ad78 @file="test/test_helper.rb", @before_score=10.3, @after_score=10.3>, #<Bugwatch::ComplexityScore:0x0000010209ad50 @file="vendor/flog/lib/flog.rb", @before_score=625.9692041855611, @after_score=625.9692041855611>, #<Bugwatch::ComplexityScore:0x0000010209ad28 @file="vendor/flog/lib/flog_task.rb", @before_score=44.94917878524719, @after_score=0.0>, #<Bugwatch::ComplexityScore:0x0000010209ad00 @file="vendor/flog/lib/gauntlet_flog.rb", @before_score=293.2783334289933, @after_score=0.0>]
_.map(&:file).zip(_.map(&:accumulated))
# => [["features/support.rb", 0.0], ["test/support/code.rb", 0], ["test/support/fixtures.rb", 0.0], ["test/test_helper.rb", 0.0], ["vendor/flog/lib/flog.rb", 0.0], ["vendor/flog/lib/flog_task.rb", -44.94917878524719], ["vendor/flog/lib/gauntlet_flog.rb", -293.2783334289933]]
commit.stats.map {|stat| [stat.file, stat.total]}
#=> [["Gemfile", 30], ["Rakefile", 31], ["bugwatch.gemspec", 43], ["features/exceptions/commit_finder.feature", 30], ["features/step_definitions/exception_steps.rb", 30], ["features/support.rb", 30], ["test/analysis_test.rb", 30], ["test/churn_test.rb", 30], ["test/commit_test.rb", 30], ["test/complexity_score_test.rb", 30], ["test/diff_parser_test.rb", 30], ["test/diff_test.rb", 30], ["test/exception_data_test.rb", 30], ["test/exception_tracker_test.rb", 30], ["test/flog_bug_test.rb", 30], ["test/flog_score_test.rb", 30], ["test/importer_test.rb", 30], ["test/method_parser_test.rb", 30], ["test/repo_test.rb", 30], ["test/ruby_complexity_test.rb", 30], ["test/support/code.rb", 30], ["test/support/fixtures.rb", 30], ["test/tag_test.rb", 30], ["test/test_helper.rb", 30], ["test/tree_test.rb", 30], ["vendor/flog/History.txt", 239], ["vendor/flog/README.txt", 59], ["vendor/flog/lib/flog.rb", 30], ["vendor/flog/lib/flog_task.rb", 40], ["vendor/flog/lib/gauntlet_flog.rb", 193]]
class CustomAnalyzer
def self.key
'CustomAnalyzer-1'
end
def self.call(bugwatch_commit)
# Store stuff in a database or email people here
puts "Analyzing #{bugwatch_commit.sha}: authored date: #{bugwatch_commit.authored_date}, committer: #{bugwatch_commit.author_name}, complexity: #{bugwatch_commit.complexity.score}"
end
end
class NoOpCacheStrategy
def initialize
@imported = []
end
def imported
@imported
end
def analyzed(key)
[]
end
def store(bugwatch_commit)
puts "Storing #{bugwatch_commit.sha}!"
@imported << bugwatch_commit.sha
end
def store_analysis(bugwatch_commit, key)
puts "Storing analysis for #{bugwatch_commit.sha} with #{key}"
end
end
repo.analyze!(NoOpCacheStrategy.new, [CustomAnalyzer], 'fc3b9d3b2b4764583253e69b0850eca50a2f9711') # begin walking history at this sha
# Storing fc3b9d3b2b4764583253e69b0850eca50a2f9711!
# Storing 5aed1750f97777281983469a1cc42b58f7250b66!
# Storing dc7921e057a01563b79a9cfb1ed4ede2b4fde86f!
# Storing 840f8fe78dd8258582f7b2783e9cf49e1bc7bf83!
# Storing 0a23638825aae3100a94b754e8be829daa6a738d!
# Analyzing fc3b9d3b2b4764583253e69b0850eca50a2f9711: authored date: 2012-01-26 09:15:29 -0700, committer: Jacob Richardson, complexity: 339.7354949499958
# Storing analysis for fc3b9d3b2b4764583253e69b0850eca50a2f9711 with CustomAnalyzer-1
# Analyzing 5aed1750f97777281983469a1cc42b58f7250b66: authored date: 2012-01-23 19:54:36 -0700, committer: Jacob Richardson, complexity: 228.70831852893863
# Storing analysis for 5aed1750f97777281983469a1cc42b58f7250b66 with CustomAnalyzer-1
# Analyzing dc7921e057a01563b79a9cfb1ed4ede2b4fde86f: authored date: 2012-01-17 19:29:22 -0700, committer: Jacob Richardson, complexity: 139.8634885701988
# Storing analysis for dc7921e057a01563b79a9cfb1ed4ede2b4fde86f with CustomAnalyzer-1
# Analyzing 840f8fe78dd8258582f7b2783e9cf49e1bc7bf83: authored date: 2012-01-07 15:12:38 -0700, committer: Jacob Richardson, complexity: -2.1999999999999993
# Storing analysis for 840f8fe78dd8258582f7b2783e9cf49e1bc7bf83 with CustomAnalyzer-1
# Analyzing 0a23638825aae3100a94b754e8be829daa6a738d: authored date: 2012-01-07 15:11:13 -0700, committer: Jacob Richardson, complexity: 123.43766156889419
# Storing analysis for 0a23638825aae3100a94b754e8be829daa6a738d with CustomAnalyzer-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment