/
issue.rb
68 lines (57 loc) · 1.73 KB
/
issue.rb
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
module Gitmine
class Issue
CONFIG_FILE = './.gitmine.yml'
attr_reader :id, :subject, :status
# Config from .gitmine.yml
def self.config
@@config ||= YAML.load_file(CONFIG_FILE)
end
# Extract the issue_id from a commit message.
# Examples:
# CommitMsgToIssueId.parse("Message for Issue #123.")
# => 123
# CommitMsgToIssueId.parse("#123.")
# => nil
#
def self.parse_for_issue_id(msg)
match = msg.match(/Issue #(\d+)/)
match ? match[1] : nil
end
# Parse the commit_message and get the associated issue if any.
def self.get_for_commit(commit_message)
issue_id = parse_for_issue_id(commit_message)
issue_id ? Issue.find(issue_id) : nil
end
# Get the issue from redmine
def self.find(issue_id)
Issue.new.tap { |issue|
issue.build_via_issue_id(issue_id)
}
end
# Get attributes from redmine and set them all
def build_via_issue_id(issue_id)
@id = issue_id
data = http_get(issue_id).parsed_response['issue']
@subject = data['subject']
@status = data['status']['name']
end
# Add a note to the Issue
def add_note(note)
p self.class.put(url(self.id),
:query => {:notes => note},
:body => "") # nginx reject requests without body
end
include HTTParty
base_uri "#{config['host']}/issues/"
basic_auth config['api_key'], '' # username is api_key, password is empty
headers 'Content-type' => 'text/xml' # by-pass rails authenticity token mechanism
protected
# Url to redmine/issues
def url(id)
"/#{id}.xml"
end
def http_get(issue_id)
self.class.get(url(issue_id))
end
end
end