Class: Highway::Steps::Library::SlackStep

Inherits:
Step
  • Object
show all
Defined in:
lib/highway/steps/library/slack.rb

Class Method Summary collapse

Methods inherited from Step

root_parameter

Class Method Details

.nameObject



17
18
19
# File 'lib/highway/steps/library/slack.rb', line 17

def self.name
  "slack"
end

.parametersObject



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
# File 'lib/highway/steps/library/slack.rb', line 21

def self.parameters
  [
    Parameters::Single.new(
      name: "avatar",
      required: false,
      type: Types::AnyOf.new(
        url: Types::Url.new(),
        emoji: Types::String.regex(/\:[a-z0-9_-]+\:/),
      ),
    ),
    Parameters::Single.new(
      name: "channel",
      required: true,
      type: Types::String.regex(/#[a-z0-9_-]+/)
    ),
    Parameters::Single.new(
      name: "username",
      required: false,
      type: Types::String.new(),
      default: "Highway",
    ),
    Parameters::Single.new(
      name: "webhook",
      required: true,
      type: Types::Url.new(),
    ),
  ]
end

.run(parameters:, context:, report:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/highway/steps/library/slack.rb', line 50

def self.run(parameters:, context:, report:)

  username = parameters["username"]
  webhook = parameters["webhook"].to_s
  avatar_emoji = parameters["avatar"][:value] if parameters["avatar"]&[:tag] == :emoji
  avatar_url = parameters["avatar"][:value].to_s if parameters["avatar"]&[:tag] == :url

  attachments = [
    generate_build_attachments(context),
    generate_tests_attachments(context),
    generate_deployment_attachments(context),
  ]

  attachments = attachments.flatten.compact

  attachments.each { |attachment|
    attachment[:mrkdwn_in] = [:text, :fields]
    attachment[:fallback] ||= attachment.fetch(:fields, []).reduce([]) { |memo, field| memo + ["#{field[:title]}: #{field[:value]}."] }.join(" ")
  }

  attachments.each { |attachment|
    attachment[:fields].each { |field| field[:value] = Slack::Notifier::Util::LinkFormatter.format(field[:value]) }
    attachment[:fallback] = Slack::Notifier::Util::LinkFormatter.format(attachment[:fallback])
  }

  notifier = Slack::Notifier.new(webhook)

  notifier.post({
    username: username,
    icon_emoji: avatar_emoji,
    icon_url: avatar_url,
    attachments: attachments,
  })

  context.interface.success("Successfully posted a report message on Slack.")

end