Class: Gitlab::Chat::Responder::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/gitlab/chat/responder/slack.rb

Constant Summary collapse

SUCCESS_COLOR =
'#B3ED8E'
FAILURE_COLOR =
'#FF5640'
RESPONSE_TYPE =
:in_channel
MESSAGE_SIZE_LIMIT =

Slack breaks messages apart if they’re around 4 KB in size. We use a slightly smaller limit here to account for user mentions.

3.5.kilobytes

Instance Attribute Summary

Attributes inherited from Base

#build

Instance Method Summary collapse

Methods inherited from Base

#initialize, #pipeline, #project

Constructor Details

This class inherits a constructor from Gitlab::Chat::Responder::Base

Instance Method Details

#failureObject

Sends the output for a build that failed.



41
42
43
44
45
46
# File 'lib/gitlab/chat/responder/slack.rb', line 41

def failure
  send_response(
    text: message_text("<#{build_url}|Sorry, the build failed!>"),
    response_type: RESPONSE_TYPE
  )
end

#scheduled_outputObject

Returns the output to send back after a command has been scheduled.



49
50
51
52
53
54
# File 'lib/gitlab/chat/responder/slack.rb', line 49

def scheduled_output
  # We return an empty message so that Slack still shows the input
  # command, without polluting the channel with standard "The job has
  # been scheduled" (or similar) responses.
  { text: '' }
end

#send_response(output) ⇒ Object

Sends a response back to Slack

output - The output to send back to Slack, as a Hash.



18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/chat/responder/slack.rb', line 18

def send_response(output)
  Gitlab::HTTP.post(
    pipeline.chat_data.response_url,
    {
      headers: { Accept: 'application/json' },
      body: output.to_json
    }
  )
end

#success(output) ⇒ Object

Sends the output for a build that completed successfully.

output - The output produced by the chat command.



31
32
33
34
35
36
37
38
# File 'lib/gitlab/chat/responder/slack.rb', line 31

def success(output)
  return if output.empty?

  send_response(
    text: message_text(limit_output(output)),
    response_type: RESPONSE_TYPE
  )
end