Class: Scan::SlackPoster

Inherits:
Object
  • Object
show all
Defined in:
scan/lib/scan/slack_poster.rb

Instance Method Summary collapse

Instance Method Details

#run(results) ⇒ Object


8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'scan/lib/scan/slack_poster.rb', line 8

def run(results)
  return if Scan.config[:skip_slack]
  return if Scan.config[:slack_only_on_failure] && results[:failures] == 0
  return if Scan.config[:slack_url].to_s.empty?

  if Scan.config[:slack_channel].to_s.length > 0
    channel = Scan.config[:slack_channel]
    channel = ('#' + channel) unless ['#', '@'].include?(channel[0]) # send message to channel by default
  end

  username = Scan.config[:slack_use_webhook_configured_username_and_icon] ? nil : Scan.config[:slack_username]
  icon_url = Scan.config[:slack_use_webhook_configured_username_and_icon] ? nil : Scan.config[:slack_icon_url]
  fields = []

  if results[:build_errors]
    fields << {
      title: 'Build Errors',
      value: results[:build_errors].to_s,
      short: true
    }
  end

  if results[:failures]
    fields << {
      title: 'Test Failures',
      value: results[:failures].to_s,
      short: true
    }
  end

  if results[:tests] && results[:failures]
    fields << {
      title: 'Successful Tests',
      value: (results[:tests] - results[:failures]).to_s,
      short: true
    }
  end

  Fastlane::Actions::SlackAction.run({
    message: "#{Scan.project.app_name} Tests:\n#{Scan.config[:slack_message]}",
    channel: channel,
    slack_url: Scan.config[:slack_url].to_s,
    success: results[:build_errors].to_i == 0 && results[:failures].to_i == 0,
    username: username,
    icon_url: icon_url,
    payload: {},
    attachment_properties: {
      fields: fields
    }
  })
end