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 7fe3495..68ce470 100644 --- a/lib/cc/services/github_issues.rb +++ b/lib/cc/services/github_issues.rb @@ -13,16 +13,35 @@ 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" + 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 + + 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) + 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..3b80414 100644 --- a/lib/cc/services/lighthouse.rb +++ b/lib/cc/services/lighthouse.rb @@ -19,15 +19,32 @@ class Config < CC::Service::Config end self.title = "Lighthouse" + self.description = "Create tickets in 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 + + 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) + 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..e65a41e 100644 --- a/lib/cc/services/pivotal_tracker.rb +++ b/lib/cc/services/pivotal_tracker.rb @@ -15,15 +15,37 @@ 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" + 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 + + 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) 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 +59,6 @@ def receive_quality parse_story(res) end -private - def parse_story(resp) body = Nokogiri::XML(resp.body) 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)