redmine_tracker.rb
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
if defined? RedmineClient
class IssueTrackers::RedmineTracker < IssueTracker
Label = "redmine"
Fields = [
[:account, {
:label => "Redmine URL",
:placeholder => "http://www.redmine.org/"
}],
[:api_token, {
:placeholder => "API Token for your account"
}],
[:username, {
:placeholder => "Your username"
}],
[:password, {
:placeholder => "Your password"
}],
[:project_id, {
:label => "Ticket Project",
:placeholder => "Redmine Project where tickets will be created"
}],
[:alt_project_id, {
:optional => true,
:label => "App Project",
:placeholder => "Where app's files & revisions can be viewed. (Leave blank to use the above project by default)"
}]
]
def check_params
if Fields.detect {|f| self[f[0]].blank? && !f[1][:optional]}
errors.add :base, 'You must specify your Redmine URL, API token, Username, Password and Project ID'
end
end
def create_issue(problem, reported_by = nil)
token = api_token
acc = account
user = username
passwd = password
RedmineClient::Base.configure do
self.token = token
self.user = user
self.password = passwd
self.site = acc
self.format = :xml
end
issue = RedmineClient::Issue.new(:project_id => project_id)
issue.subject = issue_title problem
issue.description = body_template.result(binding)
issue.save!
problem.update_attributes(
:issue_link => "#{RedmineClient::Issue.site.to_s.sub(/#{RedmineClient::Issue.site.path}$/, '')}#{RedmineClient::Issue.element_path(issue.id, :project_id => project_id)}".sub(/\.xml\?project_id=#{project_id}$/, "\?project_id=#{project_id}"),
:issue_type => Label
)
end
def url_to_file(file_path, line_number = nil)
# alt_project_id let's users specify a different project for tickets / app files.
project = self.alt_project_id.present? ? self.alt_project_id : self.project_id
url = "#{self.account.gsub(/\/$/, '')}/projects/#{project}/repository/revisions/#{app.repository_branch}/entry/#{file_path.sub(/\[PROJECT_ROOT\]/, '').sub(/^\//,'')}"
line_number ? url << "#L#{line_number}" : url
end
def body_template
@@body_template ||= ERB.new(File.read(Rails.root + "app/views/issue_trackers/redmine_body.txt.erb"))
end
def url
acc_url = account.start_with?('http') ? account : "http://#{account}"
acc_url = acc_url.gsub(/\/$/, '')
URI.parse("#{acc_url}/projects/#{project_id}").to_s
rescue URI::InvalidURIError
end
end
end