-
Notifications
You must be signed in to change notification settings - Fork 2
/
mr-reminder.rb
135 lines (114 loc) · 3.38 KB
/
mr-reminder.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'getoptlong'
require 'gitlab'
opts = GetoptLong.new(
['--help', '-h', GetoptLong::NO_ARGUMENT],
['--endpoint', '-e', GetoptLong::REQUIRED_ARGUMENT],
['--token', '-t', GetoptLong::REQUIRED_ARGUMENT],
['--group', '-g', GetoptLong::REQUIRED_ARGUMENT],
['--webhook', '-w', GetoptLong::REQUIRED_ARGUMENT]
)
$GITLAB_ENDPOINT = nil
$GITLAB_PRIVATE_TOKEN = nil
$GITLAB_GROUP_ID = nil
$SLACK_WEBHOOK = nil
opts.each do |opt, arg|
case opt
when '--help'
puts <<-EOF
-h, --help:
show help
--endpoint 'https://yourcompany.gitlab.com/api/v4', -e 'https://yourcompany.gitlab.com/api/v4':
gitlab API endpoint
--token 'xxxxxxxxxx', -t 'xxxxxxxxxx':
gitlab private token (you may have service user with read-only rights for this purpose)
--group x, -g x:
gitlab group id (supposing your team projects are in one group)
--webhook 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX', -w 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX':
slack webhook url
EOF
exit 0
when '--endpoint'
$GITLAB_ENDPOINT = arg.to_s
when '--token'
$GITLAB_PRIVATE_TOKEN = arg.to_s
when '--group'
$GITLAB_GROUP_ID = arg.to_i
when '--webhook'
$SLACK_WEBHOOK = arg.to_s
end
end
if $GITLAB_ENDPOINT.nil? or $GITLAB_PRIVATE_TOKEN.nil? or $GITLAB_GROUP_ID.nil? or $SLACK_WEBHOOK.nil?
puts 'Missing one of the arguments (try --help)'
exit 0
end
def run
client = Gitlab.client(endpoint: $GITLAB_ENDPOINT, private_token: $GITLAB_PRIVATE_TOKEN)
mrs = group_mrs(client)
projects = group_projects(client)
messages_per_project = mrs.map { |project_id, project_mrs|
project = projects.find { |p| p['id'] == project_id }
"*#{project_name(project)}*\n" + project_mrs.map { |mr|
" _#{mr_age(mr)} old_ · <#{mr['web_url']}|#{mr['title']}> " + mr_status(mr)
}.join("\n")
}
br = "\n\n"
unless messages_per_project.empty?
slack(messages_per_project.join(br))
end
end
def group_mrs(client)
client.
get("/groups/#{url_encode($GITLAB_GROUP_ID)}/merge_requests", query: { state: 'opened', order_by: 'created_at', sort: 'asc', per_page: 100 }).
map(&:to_hash).
reject { |mr| mr['work_in_progress'] }.
group_by { |mr| mr['project_id'] }
end
def group_projects(client)
client.
group_projects($GITLAB_GROUP_ID, { include_subgroups: true, per_page: 100 }).
map(&:to_hash)
end
def project_name(project)
if project
project['name']
else
'unknown project'
end
end
def mr_age(mr)
hrs = ((Time.parse(DateTime.now.to_s) - Time.parse(mr['created_at'])) / 3600).round
if hrs < 2
"#{hrs} hr"
elsif hrs < 24
"#{hrs} hrs"
else
days = (hrs / 24).round
if days > 1
"#{days} days"
else
"#{days} day"
end
end
end
def mr_status(mr)
if mr['merge_status'] == 'can_be_merged' then '✔︎' else '✘' end
end
def url_encode(url)
URI.encode(url.to_s, /\W/)
end
def slack(message)
puts message
begin
require 'net/http'
uri = URI($SLACK_WEBHOOK)
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
request = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
request.body = { "text" => "#{message}" }.to_json
response = http.request(request)
puts "response #{response.body}"
end
rescue => e
puts "failed #{e}"
end
end
run