From 392b7b9c2ce816d73ac5f21a1f217f13dd8400d5 Mon Sep 17 00:00:00 2001 From: Shuky Dvir Date: Tue, 25 Sep 2012 23:37:24 +0300 Subject: [PATCH] adding support for Bitbucket issues with tests + small fix for gtalk icon --- Gemfile | 5 ++++- Gemfile.lock | 9 +++++++++ app/assets/images/bitbucket_create.png | Bin 0 -> 2599 bytes app/assets/images/bitbucket_goto.png | Bin 0 -> 3700 bytes app/assets/images/bitbucket_inactive.png | Bin 0 -> 1994 bytes app/assets/images/gtalk_inactive.png | Bin 0 -> 1960 bytes app/models/issue_trackers/bitbucket_issues_tracker.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/views/issue_trackers/bitbucket_issues_body.txt.erb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/fabricators/issue_tracker_fabricator.rb | 5 +++++ spec/models/issue_trackers/bitbucket_issues_tracker_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 app/assets/images/bitbucket_create.png create mode 100644 app/assets/images/bitbucket_goto.png create mode 100644 app/assets/images/bitbucket_inactive.png create mode 100644 app/assets/images/gtalk_inactive.png create mode 100644 app/models/issue_trackers/bitbucket_issues_tracker.rb create mode 100644 app/views/issue_trackers/bitbucket_issues_body.txt.erb create mode 100644 spec/models/issue_trackers/bitbucket_issues_tracker_spec.rb diff --git a/Gemfile b/Gemfile index a5eeca3..b4a3a2f 100644 --- a/Gemfile +++ b/Gemfile @@ -33,10 +33,13 @@ gem 'rack-ssl-enforcer' gem 'fabrication', "~> 1.3.0" # Both for tests, and loading demo data gem 'rails_autolink', '~> 1.0.9' gem 'campy' +gem 'xmpp4r' +gem 'hipchat-api' +gem 'bitbucket_rest_api' # Please don't update this to airbrake - We override the send_notice method # to handle internal errors. -gem 'hoptoad_notifier', "~> 2.4" +gem 'hoptoad_notifier', '~> 2.4' platform :ruby do gem 'mongo', '= 1.6.2' diff --git a/Gemfile.lock b/Gemfile.lock index 1b06128..8d4b5a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,13 @@ GEM addressable (2.3.2) arel (3.0.2) bcrypt-ruby (3.0.1) + bitbucket_rest_api (0.1.1) + faraday (~> 0.8.1) + faraday_middleware (~> 0.8.1) + hashie (~> 1.2.0) + multi_json (~> 1.3) + nokogiri (~> 1.5.2) + simple_oauth bson (1.6.2) bson_ext (1.6.2) bson (~> 1.6.2) @@ -256,6 +263,7 @@ GEM libwebsocket (~> 0.1.3) multi_json (~> 1.0) rubyzip + simple_oauth (0.1.9) slop (2.4.4) sprockets (2.1.3) hike (~> 1.2) @@ -297,6 +305,7 @@ PLATFORMS DEPENDENCIES SystemTimer actionmailer_inline_css (~> 1.3.0) + bitbucket_rest_api bson (= 1.6.2) bson_ext (= 1.6.2) campy diff --git a/app/assets/images/bitbucket_create.png b/app/assets/images/bitbucket_create.png new file mode 100644 index 0000000..378129d Binary files /dev/null and b/app/assets/images/bitbucket_create.png differ diff --git a/app/assets/images/bitbucket_goto.png b/app/assets/images/bitbucket_goto.png new file mode 100644 index 0000000..df1f490 Binary files /dev/null and b/app/assets/images/bitbucket_goto.png differ diff --git a/app/assets/images/bitbucket_inactive.png b/app/assets/images/bitbucket_inactive.png new file mode 100644 index 0000000..d049ce5 Binary files /dev/null and b/app/assets/images/bitbucket_inactive.png differ diff --git a/app/assets/images/gtalk_inactive.png b/app/assets/images/gtalk_inactive.png new file mode 100644 index 0000000..b21c170 Binary files /dev/null and b/app/assets/images/gtalk_inactive.png differ diff --git a/app/models/issue_trackers/bitbucket_issues_tracker.rb b/app/models/issue_trackers/bitbucket_issues_tracker.rb new file mode 100644 index 0000000..2a19722 --- /dev/null +++ b/app/models/issue_trackers/bitbucket_issues_tracker.rb @@ -0,0 +1,47 @@ +class IssueTrackers::BitbucketIssuesTracker < IssueTracker + Label = "bitbucket" + Note = 'Please configure your Bitbucket repository in the BITBUCKET REPO field above.' + Fields = [ + [:api_token, { + :placeholder => "Your username on Bitbucket account", + :label => "Username" + }], + [:project_id, { + :placeholder => "Password for your Bitbucket account", + :label => "Password" + }] + ] + + def check_params + if Fields.detect {|f| self[f[0]].blank? } + errors.add :base, 'You must specify your Bitbucket username and password' + end + end + + def repo_name + app.bitbucket_repo + end + + def create_issue(problem, reported_by = nil) + bitbucket = BitBucket.new basic_auth: "#{api_token}:#{project_id}" + + begin + issue = bitbucket.issues.create api_token, repo_name.split('/')[1], title: issue_title(problem), content: body_template.result(binding), priority: 'critical' + problem.update_attributes( + :issue_link => "https://bitbucket.org/#{repo_name}/issue/#{issue.local_id}/", + :issue_type => Label + ) + rescue BitBucket::Error::Unauthorized + raise IssueTrackers::AuthenticationError, "Could not authenticate with BitBucket. Please check your username and password." + end + end + + def body_template + @@body_template ||= ERB.new(File.read(Rails.root + "app/views/issue_trackers/bitbucket_issues_body.txt.erb")) + end + + def url + "https://www.bitbucket.org/#{repo_name}/issues" + end +end + diff --git a/app/views/issue_trackers/bitbucket_issues_body.txt.erb b/app/views/issue_trackers/bitbucket_issues_body.txt.erb new file mode 100644 index 0000000..b2f0480 --- /dev/null +++ b/app/views/issue_trackers/bitbucket_issues_body.txt.erb @@ -0,0 +1,58 @@ +[[<%= app_err_url problem.app, problem %>| [See this exception on Errbit]]] + +---- + +<% if notice = problem.notices.first %> + <%= notice.message %> + +---- + + == Summary == + <% if notice.request['url'].present? %> + === URL === + [[<%= notice.request['url'] %>]] + <% end %> + +---- + + === Where === + <%= notice.where %> + +---- + + === Occured === + <%= notice.created_at.to_s(:micro) %> + +---- + + === Similar === + <%= (notice.problem.notices_count - 1).to_s %> + +---- + + == Params == +{{{ +<%= pretty_hash(notice.params) %> +}}} + +---- + + == Session == +{{{ +<%= pretty_hash(notice.session) %> +}}} + +---- + + == Backtrace == + <% for line in notice.backtrace %>| <%= line['number'] %>: | <%= line['file'].to_s.sub(/^\[PROJECT_ROOT\]/, '') %> -> **<%= line['method'] %>** | + <% end %> + +---- + + == Environment == + <% for key, val in notice.env_vars %> + | <%= key %>: | <%= val %> | + <% end %> +<% end %> + diff --git a/spec/fabricators/issue_tracker_fabricator.rb b/spec/fabricators/issue_tracker_fabricator.rb index 719738f..3c9f979 100644 --- a/spec/fabricators/issue_tracker_fabricator.rb +++ b/spec/fabricators/issue_tracker_fabricator.rb @@ -24,3 +24,8 @@ Fabricator :github_issues_tracker, :from => :issue_tracker, :class_name => "Issu project_id 'test_account/test_project' username 'test_username' end + +Fabricator :bitbucket_issues_tracker, :from => :issue_tracker, :class_name => "IssueTrackers::BitbucketIssuesTracker" do + project_id 'password' + api_token 'test_username' +end diff --git a/spec/models/issue_trackers/bitbucket_issues_tracker_spec.rb b/spec/models/issue_trackers/bitbucket_issues_tracker_spec.rb new file mode 100644 index 0000000..18894f9 --- /dev/null +++ b/spec/models/issue_trackers/bitbucket_issues_tracker_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe IssueTrackers::BitbucketIssuesTracker do + it "should create an issue on BitBucket Issues with problem params, and set issue link for problem" do + repo = "test_user/test_repo" + notice = Fabricate :notice + notice.app.bitbucket_repo = repo + tracker = Fabricate :bitbucket_issues_tracker, :app => notice.app + problem = notice.problem + + number = 123 + @issue_link = "https://bitbucket.org/#{repo}/issue/#{number}/" + body = < 200, :headers => {}, :body => body ) + + problem.app.issue_tracker.create_issue(problem) + problem.reload + + requested = have_requested(:post, "https://#{tracker.api_token}:#{tracker.project_id}@bitbucket.org/api/1.0/repositories/test_username/test_repo/issues/") + WebMock.should requested.with(:title => /[production][foo#bar] FooError: Too Much Bar/) + WebMock.should requested.with(:content => /See this exception on Errbit/) + + problem.issue_link.should == @issue_link + end +end + -- libgit2 0.21.2