From 1295712176f14cf79a9fe7c513bc19661ae8e73e Mon Sep 17 00:00:00 2001 From: Jozef Vaclavik Date: Tue, 30 Oct 2012 17:31:39 +0100 Subject: [PATCH] Working on Gitlab integration --- app/assets/images/gitlab_create.png | Bin 0 -> 4580 bytes app/assets/images/gitlab_goto.png | Bin 0 -> 4580 bytes app/assets/images/gitlab_inactive.png | Bin 0 -> 4451 bytes app/models/issue_trackers/gitlab_tracker.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ app/views/issue_trackers/gitlab_body.txt.erb | 45 +++++++++++++++++++++++++++++++++++++++++++++ spec/models/issue_trackers/github_issues_tracker_spec 2.rb | 30 ++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 0 deletions(-) create mode 100644 app/assets/images/gitlab_create.png create mode 100644 app/assets/images/gitlab_goto.png create mode 100644 app/assets/images/gitlab_inactive.png create mode 100644 app/models/issue_trackers/gitlab_tracker.rb create mode 100644 app/views/issue_trackers/gitlab_body.txt.erb create mode 100644 spec/models/issue_trackers/github_issues_tracker_spec 2.rb diff --git a/app/assets/images/gitlab_create.png b/app/assets/images/gitlab_create.png new file mode 100644 index 0000000..9c4f8b5 Binary files /dev/null and b/app/assets/images/gitlab_create.png differ diff --git a/app/assets/images/gitlab_goto.png b/app/assets/images/gitlab_goto.png new file mode 100644 index 0000000..9c4f8b5 Binary files /dev/null and b/app/assets/images/gitlab_goto.png differ diff --git a/app/assets/images/gitlab_inactive.png b/app/assets/images/gitlab_inactive.png new file mode 100644 index 0000000..0917fd2 Binary files /dev/null and b/app/assets/images/gitlab_inactive.png differ diff --git a/app/models/issue_trackers/gitlab_tracker.rb b/app/models/issue_trackers/gitlab_tracker.rb new file mode 100644 index 0000000..3c469a0 --- /dev/null +++ b/app/models/issue_trackers/gitlab_tracker.rb @@ -0,0 +1,43 @@ +if defined? Gitlab + class IssueTrackers::GitlabTracker < IssueTracker + Label = "gitlab" + Fields = [ + [:account, { + :label => "Gitlab URL", + :placeholder => "e.g. https://example.net" + }], + [:api_token, { + :placeholder => "API Token for your account" + }], + [:project_id, { + :label => "Ticket Project Short Name / ID", + :placeholder => "Gitlab Project where issues will be created" + }] + ] + + def check_params + if Fields.detect {|f| self[f[0]].blank?} + errors.add :base, 'You must specify your Gitlab URL, API token and Project ID' + end + end + + def create_issue(problem, reported_by = nil) + Gitlab.configure do |config| + config.endpoint = "#{account}/api/v2" + config.private_token = api_token + config.user_agent = 'Errbit User Agent' + end + title = issue_title problem + description = body_template.result(binding) + Gitlab.create_issue(project_id, title, { :description => description, :labels => "errbit" } ) + end + + def body_template + @@body_template ||= ERB.new(File.read(Rails.root + "app/views/issue_trackers/gitlab_body.txt.erb").gsub(/^\s*/, '')) + end + + def url + "#{account}/#{project_id}/issues" + end + end +end diff --git a/app/views/issue_trackers/gitlab_body.txt.erb b/app/views/issue_trackers/gitlab_body.txt.erb new file mode 100644 index 0000000..3c01a35 --- /dev/null +++ b/app/views/issue_trackers/gitlab_body.txt.erb @@ -0,0 +1,45 @@ +[See this exception on Errbit](<%= app_problem_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'] %>](<%= 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 ## +``` +<% notice.backtrace_lines.each do |line| %><%= line.number %>: <%= line.file_relative %> -> **<%= line.method %>** +<% end %> +``` + +## Environment ## + + +<% for key, val in notice.env_vars %> + + + + +<% end %> +
<%= key %>:<%= val %>
+<% end %> + diff --git a/spec/models/issue_trackers/github_issues_tracker_spec 2.rb b/spec/models/issue_trackers/github_issues_tracker_spec 2.rb new file mode 100644 index 0000000..d5fc833 --- /dev/null +++ b/spec/models/issue_trackers/github_issues_tracker_spec 2.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe IssueTrackers::GitlabTracker do + it "should create an issue on Gitlab with problem params" do + notice = Fabricate :notice + tracker = Fabricate :gitlab_tracker, :app => notice.app + problem = notice.problem + + number = 5 + @issue_link = "#{tracker.account}/#{tracker.project_id}/issues/#{number}/#{tracker.api_token}" + body = < 201, :headers => {'Location' => @issue_link}, :body => body ) + + problem.app.issue_tracker.create_issue(problem) + problem.reload + + requested = have_requested(:post, "#{tracker.account}/#{tracker.project_id}/issues/#{tracker.api_token}") + WebMock.should requested.with(:body => /[production][foo#bar] FooError: Too Much Bar/) + WebMock.should requested.with(:body => /See this exception on Errbit/) + + problem.issue_link.should == @issue_link + end +end + -- libgit2 0.21.2