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.



54
55
56
57
58
59
# File 'lib/gitlab/chat/responder/slack.rb', line 54

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.



62
63
64
65
66
67
# File 'lib/gitlab/chat/responder/slack.rb', line 62

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
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/chat/responder/slack.rb', line 18

def send_response(output)
  response = Integrations::Clients::HTTP.post(
    pipeline.chat_data.response_url,
    {
      headers: { Accept: 'application/json' },
      body: output.to_json
    }
  )

  unless response.success?
    Gitlab::AppLogger.warn(
      message: 'Posting chat response failed',
      error_message: response.message,
      code: response.code
    )
  end

  response
end

#success(output) ⇒ Object

Sends the output for a build that completed successfully.

output - The output produced by the chat command.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/chat/responder/slack.rb', line 41

def success(output)
  if output.empty?
    Gitlab::AppLogger.info(message: 'Chat pipeline successful, but output is empty')
    return
  end

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