Created
February 20, 2017 09:51
-
-
Save linyows/97bfaf44ec02c8f1984d6adaf0c5276a to your computer and use it in GitHub Desktop.
Notify opend Pull-Requests to Slack
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 'octokit' | |
require 'netrc' | |
require 'json' | |
# Config | |
channel = '#development' | |
mention = '@dev' | |
slack_token = 'xoxb-xxxxxx-xxxxxxxxxxxxxxxxxxxx' | |
repos = %w( | |
our-org/our-project1 | |
our-org/our-project2 | |
other-org/other-project | |
) | |
# For GH:E Endpoint | |
Octokit.configure do |c| | |
c.api_endpoint = "https://our.ghe.com/api/v3/" | |
c.web_endpoint = "https://our.ghe.com/" | |
end | |
# Customize | |
default_message = 'チェックしてね' | |
labels = { | |
'reviewed' => { limit: 1, message: 'レビュー済みがあります!' }, | |
'thnking' => { limit: 1, message: 'だれか悩んでます!' }, | |
'needs-review' => { limit: 3, message: 'レビュー溜まってませんか?' }, | |
'ready-for-merge' => { limit: 3, message: 'デプロイ待ちがあります!' }, | |
'in-review' => { limit: 5, message: 'レビュー中が多くないですか?' }, | |
'WIP' => { limit: 5, message: 'WIPが放置されていませんか?' } | |
} | |
client = Octokit::Client.new :netrc => true | |
label_colors = {} | |
messages = [] | |
labeled_issues = repos.each.with_object({}) do |repo, memo| | |
labels.keys.each do |label| | |
issues = client.issues(repo, labels: label).each.with_object([]) do |i, m| | |
label_colors[label] = i.labels.each.with_object('') do |l, m2| | |
m2 << l.color if l.name == label | |
end | |
m << "<#{i.html_url}|#{i.title}> by #{i.user.login}" | |
end | |
unless issues.empty? | |
if memo[label].nil? | |
memo[label] = [] | |
end | |
memo[label].concat(issues) | |
end | |
end | |
end | |
attachments = labeled_issues.each.with_object([]) do |(label, issues), memo| | |
text = issues.join("\n") | |
l = label.gsub('-', ' ') | |
if issues.count >= labels[label][:limit] | |
messages << labels[label][:message] | |
end | |
memo << { | |
color: label_colors[label], | |
title: "#{l.upcase == l ? l : l.capitalize}s", | |
text: text, | |
fallback: text, | |
mrkdwn_in: ['text'] | |
} | |
end | |
default_message = 'Wow, We did it! :tada:' if attachments.empty? | |
body = { | |
username: 'octocat', | |
channel: channel, | |
icon_emoji: ':octocat:', | |
text: "#{mention}: #{messages.empty? ? default_message : messages.join(' ')}", | |
link_names: 1, | |
mrkdwn: true, | |
attachments: JSON.dump(attachments) | |
} | |
Faraday.new('https://slack.com') do |c| | |
c.request :url_encoded | |
c.adapter Faraday.default_adapter | |
end.post("/api/chat.postMessage?token=#{slack_token}", body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment