From ca7f4fbfff319b7d7de2af13ad8a67ba1fa01d2b Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Thu, 27 Mar 2014 14:24:31 -0400 Subject: [PATCH 1/3] Receive test event - Factor out create_ methods for reuse --- lib/cc/services/github_issues.rb | 17 +++++++++++++---- lib/cc/services/lighthouse.rb | 19 +++++++++++++------ lib/cc/services/pivotal_tracker.rb | 18 ++++++++++++++---- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/cc/services/github_issues.rb b/lib/cc/services/github_issues.rb index 7fe3495..c5f98e7 100644 --- a/lib/cc/services/github_issues.rb +++ b/lib/cc/services/github_issues.rb @@ -18,11 +18,20 @@ class Config < CC::Service::Config BASE_URL = "https://api.github.com" + def receive_test + create_issue("Test ticket from Code Climate", "") + end + def receive_quality - params = { - title: "Refactor #{constant_name} from #{rating} on Code Climate", - body: details_url, - } + title = "Refactor #{constant_name} from #{rating} on Code Climate" + + create_issue(title, details_url) + end + +private + + def create_issue(title, issue_body) + params = { title: title, body: issue_body } if config.labels.present? params[:labels] = config.labels.split(",").map(&:strip).reject(&:blank?).compact diff --git a/lib/cc/services/lighthouse.rb b/lib/cc/services/lighthouse.rb index 597cd11..992ec83 100644 --- a/lib/cc/services/lighthouse.rb +++ b/lib/cc/services/lighthouse.rb @@ -21,13 +21,20 @@ class Config < CC::Service::Config self.title = "Lighthouse" self.issue_tracker = true + def receive_test + create_ticket("Test ticket from Code Climate", "") + end + def receive_quality - params = { - ticket: { - title: "Refactor #{constant_name} from #{rating} on Code Climate", - body: details_url - } - } + title = "Refactor #{constant_name} from #{rating} on Code Climate" + + create_ticket(title, details_url) + end + +private + + def create_ticket(title, ticket_body) + params = { ticket: { title: title, body: ticket_body } } if config.tags.present? params[:ticket][:tags] = config.tags.strip diff --git a/lib/cc/services/pivotal_tracker.rb b/lib/cc/services/pivotal_tracker.rb index ba301da..0c18544 100644 --- a/lib/cc/services/pivotal_tracker.rb +++ b/lib/cc/services/pivotal_tracker.rb @@ -19,11 +19,23 @@ class Config < CC::Service::Config BASE_URL = "https://www.pivotaltracker.com/services/v3" + def receive_test + create_story("Test ticket from Code Climate", "") + end + def receive_quality + name = "Refactor #{constant_name} from #{rating} on Code Climate" + + create_story(name, details_url) + end + +private + + def create_story(name, description) params = { - "story[name]" => "Refactor #{constant_name} from #{rating} on Code Climate", + "story[name]" => name, "story[story_type]" => "chore", - "story[description]" => details_url, + "story[description]" => description, } if config.labels.present? @@ -37,8 +49,6 @@ def receive_quality parse_story(res) end -private - def parse_story(resp) body = Nokogiri::XML(resp.body) From 53ea3500bad0e382aec2705ce8a72c1c63931306 Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Thu, 27 Mar 2014 14:25:10 -0400 Subject: [PATCH 2/3] Add descriptions to trackers --- lib/cc/services/github_issues.rb | 3 ++- lib/cc/services/lighthouse.rb | 1 + lib/cc/services/pivotal_tracker.rb | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cc/services/github_issues.rb b/lib/cc/services/github_issues.rb index c5f98e7..4ac99b0 100644 --- a/lib/cc/services/github_issues.rb +++ b/lib/cc/services/github_issues.rb @@ -13,8 +13,9 @@ class Config < CC::Service::Config validates :oauth_token, presence: true end - self.issue_tracker = true self.title = "GitHub Issues" + self.description = "Open issues on GitHub" + self.issue_tracker = true BASE_URL = "https://api.github.com" diff --git a/lib/cc/services/lighthouse.rb b/lib/cc/services/lighthouse.rb index 992ec83..c54146b 100644 --- a/lib/cc/services/lighthouse.rb +++ b/lib/cc/services/lighthouse.rb @@ -19,6 +19,7 @@ class Config < CC::Service::Config end self.title = "Lighthouse" + self.description = "Create tickets in Lighthouse" self.issue_tracker = true def receive_test diff --git a/lib/cc/services/pivotal_tracker.rb b/lib/cc/services/pivotal_tracker.rb index 0c18544..c6e5979 100644 --- a/lib/cc/services/pivotal_tracker.rb +++ b/lib/cc/services/pivotal_tracker.rb @@ -15,6 +15,7 @@ class Config < CC::Service::Config end self.title = "Pivotal Tracker" + self.description = "Create stories on Pivotal Tracker" self.issue_tracker = true BASE_URL = "https://www.pivotaltracker.com/services/v3" From 2408d595f3405f499d01174fde6ac246c3b454df Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Thu, 27 Mar 2014 15:08:31 -0400 Subject: [PATCH 3/3] Add vulnerability event handlers --- lib/cc/formatters/ticket_formatter.rb | 27 +++++++++++++++++++++++++++ lib/cc/services/github_issues.rb | 9 +++++++++ lib/cc/services/lighthouse.rb | 9 +++++++++ lib/cc/services/pivotal_tracker.rb | 9 +++++++++ test/github_issues_test.rb | 11 +++++++++++ test/lighthouse_test.rb | 11 +++++++++++ test/pivotal_tracker_test.rb | 11 +++++++++++ 7 files changed, 87 insertions(+) create mode 100644 lib/cc/formatters/ticket_formatter.rb diff --git a/lib/cc/formatters/ticket_formatter.rb b/lib/cc/formatters/ticket_formatter.rb new file mode 100644 index 0000000..1a610c8 --- /dev/null +++ b/lib/cc/formatters/ticket_formatter.rb @@ -0,0 +1,27 @@ +module CC + module Formatters + class TicketFormatter < CC::Service::Formatter + + def format_vulnerability_title + if multiple? + "#{vulnerabilities.size} new #{warning_type} issues found" + else + "New #{warning_type} issue found" << location_info + end + end + + def format_vulnerability_body + if multiple? + "#{vulnerabilities.size} new #{warning_type} issues were found by Code Climate" + else + message = "A #{warning_type} vulnerability was found by Code Climate" + message << location_info + end + + message << ".\n\n" + message << details_url + end + + end + end +end diff --git a/lib/cc/services/github_issues.rb b/lib/cc/services/github_issues.rb index 4ac99b0..68ce470 100644 --- a/lib/cc/services/github_issues.rb +++ b/lib/cc/services/github_issues.rb @@ -29,6 +29,15 @@ def receive_quality create_issue(title, details_url) end + def receive_vulnerability + formatter = CC::Formatters::TicketFormatter.new(self) + + create_issue( + formatter.format_vulnerability_title, + formatter.format_vulnerability_body + ) + end + private def create_issue(title, issue_body) diff --git a/lib/cc/services/lighthouse.rb b/lib/cc/services/lighthouse.rb index c54146b..3b80414 100644 --- a/lib/cc/services/lighthouse.rb +++ b/lib/cc/services/lighthouse.rb @@ -32,6 +32,15 @@ def receive_quality create_ticket(title, details_url) end + def receive_vulnerability + formatter = CC::Formatters::TicketFormatter.new(self) + + create_ticket( + formatter.format_vulnerability_title, + formatter.format_vulnerability_body + ) + end + private def create_ticket(title, ticket_body) diff --git a/lib/cc/services/pivotal_tracker.rb b/lib/cc/services/pivotal_tracker.rb index c6e5979..e65a41e 100644 --- a/lib/cc/services/pivotal_tracker.rb +++ b/lib/cc/services/pivotal_tracker.rb @@ -30,6 +30,15 @@ def receive_quality create_story(name, details_url) end + def receive_vulnerability + formatter = CC::Formatters::TicketFormatter.new(self) + + create_story( + formatter.format_vulnerability_title, + formatter.format_vulnerability_body + ) + end + private def create_story(name, description) diff --git a/test/github_issues_test.rb b/test/github_issues_test.rb index 51e0a01..0e271fb 100644 --- a/test/github_issues_test.rb +++ b/test/github_issues_test.rb @@ -9,6 +9,17 @@ def test_quality ) end + def test_vulnerability + assert_github_receives( + event(:vulnerability, vulnerabilities: [{ + "warning_type" => "critical", + "location" => "app/user.rb line 120" + }]), + "New critical issue found in app/user.rb line 120", + "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed" + ) + end + private def assert_github_receives(event_data, title, ticket_body) diff --git a/test/lighthouse_test.rb b/test/lighthouse_test.rb index 9bf4d7b..dc3f29f 100644 --- a/test/lighthouse_test.rb +++ b/test/lighthouse_test.rb @@ -9,6 +9,17 @@ def test_quality ) end + def test_vulnerability + assert_lighthouse_receives( + event(:vulnerability, vulnerabilities: [{ + "warning_type" => "critical", + "location" => "app/user.rb line 120" + }]), + "New critical issue found in app/user.rb line 120", + "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed" + ) + end + private def assert_lighthouse_receives(event_data, title, ticket_body) diff --git a/test/pivotal_tracker_test.rb b/test/pivotal_tracker_test.rb index 0165d1e..06de158 100644 --- a/test/pivotal_tracker_test.rb +++ b/test/pivotal_tracker_test.rb @@ -9,6 +9,17 @@ def test_quality ) end + def test_vulnerability + assert_pivotal_receives( + event(:vulnerability, vulnerabilities: [{ + "warning_type" => "critical", + "location" => "app/user.rb line 120" + }]), + "New critical issue found in app/user.rb line 120", + "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed" + ) + end + private def assert_pivotal_receives(event_data, name, description)