Module: PrNotifier

Defined in:
lib/pr-notifier.rb,
lib/pr-notifier/github.rb,
lib/pr-notifier/version.rb

Defined Under Namespace

Classes: Github, PullRequest

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.message(pull_requests:) ⇒ Object



32
33
34
# File 'lib/pr-notifier.rb', line 32

def message(pull_requests:) 
  ERB.new(template, nil, '-').result(binding)
end

.run(gh_token: nil, repos: nil, webhook_url: nil, channel: nil, username: nil, template: nil, dry_run: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pr-notifier.rb', line 8

def run(gh_token: nil, repos: nil, webhook_url: nil, channel: nil, username: nil, template: nil, dry_run: false)
  gh_token ||= ENV['GH_TOKEN']
  repos = (repos || ENV['GH_REPOS'])&.split(',')
  webhook_url ||= ENV["SLACK_WEBHOOK_URL"]
  channel ||= ENV["SLACK_CHANNEL"]
  username ||= ENV["SLACK_USERNAME"]
  @template = template || ENV["TEMPLATE"]

  raise "Please set GitHub Token" if gh_token.nil?
  raise "Please set repos" if repos.size == 0
  raise "Please set slack webhook_url" if webhook_url.nil?

  gh_client = Github.new(access_token: gh_token)
  prs = repos.each_with_object([]) { |repo, prs| prs.concat(gh_client.fetch_prs_by(repo)) }

  send_message = message(pull_requests: prs)
  if dry_run
    puts send_message
  else
    notifier = Slack::Notifier.new(webhook_url, channel: channel, username: username)
    notifier.ping(message(pull_requests: prs))
  end
end

.templateObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pr-notifier.rb', line 36

def template
  @template || "<% pull_requests.each do |pr| -%>\n:memo: <%= pr.title %>\n    <%= pr.url %>\n<% if pr.reviewers.size > 0 -%>\n    reviewers: <%= pr.reviewers.join(\", \") %>\n<% end -%>\n<% end -%>\n"
end