Class: MrBump::Slack
- Inherits:
-
Object
- Object
- MrBump::Slack
- Defined in:
- lib/mr_bump/slack.rb
Overview
This class uses a slack webhook to push notifications to slack
Instance Attribute Summary collapse
-
#git ⇒ Object
Returns the value of attribute git.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#jira_url ⇒ Object
Returns the value of attribute jira_url.
-
#username ⇒ Object
Returns the value of attribute username.
-
#webhook ⇒ Object
Returns the value of attribute webhook.
Instance Method Summary collapse
-
#attatchment(version, changes) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #bump(version, changes) ⇒ Object
- #git_shas(changes) ⇒ Object
-
#initialize(git_config, config) ⇒ Slack
constructor
A new instance of Slack.
- #jira_field(changes) ⇒ Object
- #jira_ids(changes) ⇒ Object
- #jira_urls(changes) ⇒ Object
- #notifier ⇒ Object
Constructor Details
#initialize(git_config, config) ⇒ Slack
Returns a new instance of Slack.
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
#git ⇒ Object
Returns the value of attribute git.
9 10 11 |
# File 'lib/mr_bump/slack.rb', line 9 def git @git end |
#icon ⇒ Object
Returns the value of attribute icon.
9 10 11 |
# File 'lib/mr_bump/slack.rb', line 9 def icon @icon end |
#jira_url ⇒ Object
Returns the value of attribute jira_url.
9 10 11 |
# File 'lib/mr_bump/slack.rb', line 9 def jira_url @jira_url end |
#username ⇒ Object
Returns the value of attribute username.
9 10 11 |
# File 'lib/mr_bump/slack.rb', line 9 def username @username end |
#webhook ⇒ Object
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) = {} [:icon_url] = @icon if @icon [:attachments] = [attatchment(version, changes)] notifier.ping 'There is a new version bump!', 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 |
#notifier ⇒ Object
29 30 31 |
# File 'lib/mr_bump/slack.rb', line 29 def notifier @notifier ||= ::Slack::Notifier.new(@webhook, username: @username) end |