Class: Fastlane::Actions::SlackAction::Runner
- Inherits:
-
Object
- Object
- Fastlane::Actions::SlackAction::Runner
- Defined in:
- fastlane/lib/fastlane/actions/slack.rb
Class Method Summary collapse
-
.deep_merge(a, b) ⇒ Object
Adapted from stackoverflow.com/a/30225093/158525.
- .generate_slack_attachments(options) ⇒ Object
-
.trim_message(message) ⇒ Object
As there is a text limit in the notifications, we are usually interested in the last part of the message e.g.
Instance Method Summary collapse
-
#initialize(slack_url) ⇒ Runner
constructor
A new instance of Runner.
- #post_message(channel:, username:, attachments:, link_names:, icon_url:, fail_on_error:) ⇒ Object
- #run(options) ⇒ Object
Constructor Details
#initialize(slack_url) ⇒ Runner
Returns a new instance of Runner.
10 11 12 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 10 def initialize(slack_url) @notifier = Fastlane::Notification::Slack.new(slack_url) end |
Class Method Details
.deep_merge(a, b) ⇒ Object
Adapted from stackoverflow.com/a/30225093/158525
158 159 160 161 162 163 164 165 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 158 def self.deep_merge(a, b) merger = proc do |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 end a.merge(b, &merger) end |
.generate_slack_attachments(options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 72 def self.() color = ([:success] ? 'good' : 'danger') should_add_payload = ->(payload_name) { [:default_payloads].map(&:to_sym).include?(payload_name.to_sym) } = { fallback: [:message], text: [:message], pretext: [:pretext], color: color, mrkdwn_in: ["pretext", "text", "fields", "message"], fields: [] } # custom user payloads [:fields] += [:payload].map do |k, v| { title: k.to_s, value: Fastlane::Notification::Slack::LinkConverter.convert(v.to_s), short: false } end # Add the lane to the Slack message # This might be nil, if slack is called as "one-off" action if should_add_payload[:lane] && Actions.lane_context[Actions::SharedValues::LANE_NAME] [:fields] << { title: 'Lane', value: Actions.lane_context[Actions::SharedValues::LANE_NAME], short: true } end # test_result if should_add_payload[:test_result] [:fields] << { title: 'Result', value: ([:success] ? 'Success' : 'Error'), short: true } end # git branch if Actions.git_branch && should_add_payload[:git_branch] [:fields] << { title: 'Git Branch', value: Actions.git_branch, short: true } end # git_author if Actions. && should_add_payload[:git_author] if FastlaneCore::Env.truthy?('FASTLANE_SLACK_HIDE_AUTHOR_ON_SUCCESS') && [:success] # We only show the git author if the build failed else [:fields] << { title: 'Git Author', value: Actions., short: true } end end # last_git_commit if Actions. && should_add_payload[:last_git_commit] [:fields] << { title: 'Git Commit', value: Actions., short: false } end # last_git_commit_hash if Actions.last_git_commit_hash(true) && should_add_payload[:last_git_commit_hash] [:fields] << { title: 'Git Commit Hash', value: Actions.last_git_commit_hash(short: true), short: false } end # merge additional properties deep_merge(, [:attachment_properties]) end |
.trim_message(message) ⇒ Object
As there is a text limit in the notifications, we are usually interested in the last part of the message e.g. for tests
63 64 65 66 67 68 69 70 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 63 def self.() # We want the last 7000 characters, instead of the first 7000, as the error is at the bottom start_index = [.length - 7000, 0].max = [start_index..-1] # We want line breaks to be shown on slack output so we replace # input non-interpreted line break with interpreted line break .gsub('\n', "\n") end |
Instance Method Details
#post_message(channel:, username:, attachments:, link_names:, icon_url:, fail_on_error:) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 41 def (channel:, username:, attachments:, link_names:, icon_url:, fail_on_error:) @notifier.post_to_legacy_incoming_webhook( channel: channel, username: username, link_names: link_names, icon_url: icon_url, attachments: ) UI.success('Successfully sent Slack notification') rescue => error UI.error("Exception: #{error}") = "Error pushing Slack message, maybe the integration has no permission to post on this channel? Try removing the channel parameter in your Fastfile, this is usually caused by a misspelled or changed group/channel name or an expired SLACK_URL" if fail_on_error UI.user_error!() else UI.error() end end |
#run(options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 14 def run() [:message] = self.class.([:message].to_s || '') [:message] = Fastlane::Notification::Slack::LinkConverter.convert([:message]) [:pretext] = [:pretext].gsub('\n', "\n") unless [:pretext].nil? if [:channel].to_s.length > 0 channel = [:channel] channel = ('#' + [:channel]) unless ['#', '@'].include?(channel[0]) # send message to channel by default end username = [:use_webhook_configured_username_and_icon] ? nil : [:username] = self.class.() link_names = [:link_names] icon_url = [:use_webhook_configured_username_and_icon] ? nil : [:icon_url] ( channel: channel, username: username, attachments: [], link_names: link_names, icon_url: icon_url, fail_on_error: [:fail_on_error] ) end |