Last active
May 14, 2020 13:09
-
-
Save a-voronov/9f97a40b6e03789a2aee9194e9ddc5be to your computer and use it in GitHub Desktop.
Gitlab Merge Requests Reminder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
$ANIMALS = ['๐ See-No-Evil Monkey', '๐ Hear-No-Evil Monkey', '๐ Speak-No-Evil Monkey', '๐ต Monkey', '๐ Monkey', '๐ฆ Gorilla', '๐ถ Dog', '๐ Dog', '๐ฉ Poodle', '๐บ Wolf', '๐ฆ Fox', '๐ฆ Raccoon', '๐ฑ Cat', '๐ Cat', '๐ฆ Lion', '๐ฏ Tiger', '๐ Tiger', '๐ Leopard', '๐ด Horse', '๐ Horse', '๐ฆ Unicorn', '๐ฆ Zebra', '๐ฎ Cow', '๐ Ox', '๐ Water Buffalo', '๐ Cow', '๐ท Pig', '๐ Pig', '๐ Boar', '๐ Ram', '๐ Ewe', '๐ Goat', '๐ช Camel', '๐ซ Camel', '๐ฆ Llama', '๐ฆ Giraffe', '๐ Elephant', '๐ฆ Rhinoceros', '๐ฆ Hippopotamus', '๐ญ Mouse', '๐ Mouse', '๐ Rat', '๐น Hamster', '๐ฐ Rabbit', '๐ Rabbit', '๐ฟ Chipmunk', '๐ฆ Hedgehog', '๐ฆ Bat', '๐ป Bear', '๐จ Koala', '๐ผ Panda', '๐ฆ Kangaroo', '๐ฆก Badger', '๐ฆ Turkey', '๐ Chicken', '๐ Rooster', '๐ฃ Chick', '๐ค Baby Chick', '๐ฅ Baby Chick', '๐ฆ Bird', '๐ง Penguin', '๐ Dove', '๐ฆ Eagle', '๐ฆ Duck', '๐ฆข Swan', '๐ฆ Owl', '๐ฆ Peacock', '๐ฆ Parrot', '๐ธ Frog', '๐ Crocodile', '๐ข Turtle', '๐ฆ Lizard', '๐ Snake', '๐ฒ Dragon', '๐ Dragon', '๐ฆ Sauropod', '๐ฆ T-Rex', '๐ณ Spouting Whale', '๐ Whale', '๐ฌ Dolphin', '๐ Fish', '๐ Tropical Fish', '๐ก Blowfish', '๐ฆ Shark', '๐ Octopus', '๐ Spiral Shell', '๐ฆ Crab', '๐ฆ Lobster', '๐ฆ Shrimp', '๐ฆ Squid', '๐ Snail', '๐ฆ Butterfly', '๐ Bug', '๐ Ant', '๐ Honeybee', '๐ Lady Beetle', '๐ฆ Cricket', '๐ท Spider', '๐ฆ Scorpion', '๐ฆ Mosquito', '๐ฆ Microbe'] | |
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)} <#{mr['web_url']}|#{mr['title']}> " + mr_status(mr) | |
}.join("\n") | |
} | |
br = "\n\n" | |
message_start = "*Good Morning! โ๏ธ๐ *" | |
mrs_message = "Seems like there're some Merge Requests waiting for the review. Now is the best time to do it! ๐" | |
alt_message = "Seems like we don't have any Merge Requests waiting for the review! Good job! ๐ช๐" | |
message_end = "\n*#{$ANIMALS.sample} wishes you a great day! ๐ค *" | |
message_body = unless messages_per_project.empty? then mrs_message + br + messages_per_project.join(br) else alt_message end | |
slack([message_start, message_body, message_end].join(br)) | |
end | |
def group_mrs(client) | |
client. | |
get("/groups/#{url_encode($GITLAB_GROUP_ID)}/merge_requests", query: { state: 'opened', order_by: 'created_at', 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, per_page: 100). | |
map(&:to_hash) | |
end | |
def project_name(project) | |
if project | |
project['name'].upcase | |
else | |
'unknown project ๐คทโโ๏ธ' | |
end | |
end | |
def mr_age(mr) | |
age = DateTime.now.mjd - Date.parse(mr['created_at']).mjd | |
case age - 1 | |
when 0 | |
'๐ฃ' | |
when 1..3 | |
['๐ฅบ', '๐', '๐โโ๏ธ', '๐', '๐', '๐ค', '๐', 'โ๏ธ'].sample | |
when 3..10 | |
['๐งโโ๏ธ', '๐งโโ๏ธ'].sample | |
else | |
'โ ๏ธ' | |
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 --------------- # | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements
Usage
run ruby script by providing necessary arguments:
'https://yourcompany.gitlab.com/api/v4'
'xxxxxxxxxxxxxxxxxxxxxxxx'
42
'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
use help for more info: