forked from hebinbin/git-hooks-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-commit
More file actions
114 lines (86 loc) · 2.88 KB
/
Copy pathpost-commit
File metadata and controls
114 lines (86 loc) · 2.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env ruby
require 'json'
require 'net/https'
module JIRA
class HttpClient
DEFAULT = {
:site => "https://******.atlassian.net",
:path => "rest/api/latest/issue",
:ticket_number => "",
:resource => "",
:username => "",
:password => "",
:use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE
}
attr_reader :options
def initialize(options)
@options = DEFAULT.merge(options)
end
def uri
URI.parse "#{@options[:site]}/#{@options[:path]}/#{@options[:ticket_number]}/#{@options[:resource]}"
end
def make_request(http_method, body='', header={})
request_data = Net::HTTP.const_get(http_method.to_s.capitalize).new(uri.request_uri, header)
request_data.body = body unless body.nil?
request_data.basic_auth(@options[:username], @options[:password])
# send request data to jira rest api
http_conn.request request_data
end
def http_conn
http_conn = Net::HTTP.new uri.host, uri.port
http_conn.use_ssl = @options[:use_ssl]
http_conn.verify_mode = @options[:verify_mode]
http_conn
end
end
end
def header
{ "content-type" => "application/json" }
end
TICKET_REGEXP = /[A-Z]+-\d+/ # WEB-12345
TIME_REGEXP = /\d+[.]+\d+h/ # 0.5h, 2.0h
cur_br_name = %x{git branch}.split("\n").select { |i| i[0] == "*" }.first.gsub("* ", "")
commit_message = %x{git log -1 --abbrev-commit}.split("\n").last.strip
ticket_number = commit_message.match(TICKET_REGEXP).to_s
worked_time = commit_message.match(TIME_REGEXP).to_s
puts "log #{ticket_number} worked time to #{worked_time}"
jira_configs = %x{git config --list}.split("\n").select do |config|
config.match('jira.username') or config.match("jira.password")
end
if jira_configs.length != 2
puts "you did not set username or password in your git config"
puts "please go to your jira to change status from Open to In Progess"
else
jira_info = jira_configs.inject({}) do |hash, arr|
tmp = arr.split("=")
hash.merge Hash[*tmp]
end #{ "jira.username" => "your username", "jira.password" => "your password"}
# log my worked time
options = {
:ticket_number => ticket_number,
:resource => "worklog",
:username => jira_info["jira.username"],
:password => jira_info["jira.password"]
}
body = JSON.generate(
{
:comment => commit_message,
:timeSpent => worked_time
}
)
JIRA::HttpClient.new(options).make_request('post', body, header)
# log commit message to jira comment
options = {
:ticket_number => ticket_number,
:resource => "comment",
:username => jira_info["jira.username"],
:password => jira_info["jira.password"]
}
body = JSON.generate(
{
:body => "Branch name: #{cur_br_name} \n #{commit_message}"
}
)
JIRA::HttpClient.new(options).make_request('post', body, header)
end