Class: MrBump::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/mr_bump/slack.rb

Overview

This class uses a slack webhook to push notifications to slack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_config, config) ⇒ Slack

Returns a new instance of Slack.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/mr_bump/slack.rb', line 11

def initialize(git_config, config)
  opts = config['slack'] || {}
  raise ArgumentError, 'No Slack webhook found.' unless opts['webhook_url']
  @webhook = opts['webhook_url']
  @username = opts['username'] || 'Mr Bump'
  @jira_url = opts['jira_url']
  @icon = Array(opts['icon']).sample
  @config = config
  @git = git_config
end

Instance Attribute Details

#gitObject

Returns the value of attribute git.



9
10
11
# File 'lib/mr_bump/slack.rb', line 9

def git
  @git
end

#iconObject

Returns the value of attribute icon.



9
10
11
# File 'lib/mr_bump/slack.rb', line 9

def icon
  @icon
end

#jira_urlObject

Returns the value of attribute jira_url.



9
10
11
# File 'lib/mr_bump/slack.rb', line 9

def jira_url
  @jira_url
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/mr_bump/slack.rb', line 9

def username
  @username
end

#webhookObject

Returns the value of attribute webhook.



9
10
11
# File 'lib/mr_bump/slack.rb', line 9

def webhook
  @webhook
end

Instance Method Details

#attatchment(version, changes) ⇒ Object

rubocop:disable Metrics/MethodLength



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mr_bump/slack.rb', line 66

def attatchment(version, changes)
  {
    fallback: changes,
    color: '#009de4',
    author_name: @git.user,
    author_icon: @git.user_icon,
    author_link: @git.user_link,
    fields: [
      { title: 'Version', value: version, short: true },
      { title: 'Repository', value: @git.repo_name, short: true },
      { title: 'Change Log', value: changes, short: false },
      jira_field(changes),
      { value: git_shas(changes), short: true }
    ],
    mrkdwn_in: %w(text title fallback fields)
  }
end

#bump(version, changes) ⇒ Object



22
23
24
25
26
27
# File 'lib/mr_bump/slack.rb', line 22

def bump(version, changes)
  options = {}
  options[:icon_url] = @icon if @icon
  options[:attachments] = [attatchment(version, changes)]
  notifier.ping 'There is a new version bump!', options
end

#git_shas(changes) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/mr_bump/slack.rb', line 57

def git_shas(changes)
  jira_ids(changes).map do |ticket|
    if ticket != 'UNKNOWN' && !@git.get_sha(ticket).nil?
      "<#{@git.repo_url}/commit/#{@git.get_sha(ticket)}|Git merge link - #{ticket}>\n"
    end
  end.join
end

#jira_field(changes) ⇒ Object



51
52
53
54
55
# File 'lib/mr_bump/slack.rb', line 51

def jira_field(changes)
  if @jira_url && !@jira_url.empty?
    { value: jira_urls(changes), short: true, unfurl_links: false }
  end
end

#jira_ids(changes) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/mr_bump/slack.rb', line 33

def jira_ids(changes)
  changes.split(/$\n?(?=\s*\*)/).map do |i|
    begin
      MrBump::Change.from_md(@config, i).dev_id
    rescue ArgumentError
      nil
    end
  end.compact
end

#jira_urls(changes) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/mr_bump/slack.rb', line 43

def jira_urls(changes)
  jira_ids(changes).map do |ticket|
    if ticket != 'UNKNOWN'
      "<#{@jira_url}/browse/#{ticket}|Jira link - #{ticket}>\n"
    end
  end.join
end

#notifierObject



29
30
31
# File 'lib/mr_bump/slack.rb', line 29

def notifier
  @notifier ||= ::Slack::Notifier.new(@webhook, username: @username)
end