Class: Gitlab::Chat::Responder::Mattermost

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

Constant Summary collapse

SUCCESS_COLOR =
'#00c100'
FAILURE_COLOR =
'#e40303'
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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitlab/chat/responder/mattermost.rb', line 62

def failure
  send_response(
    response_type: :in_channel,
    attachments: [
      {
        color: FAILURE_COLOR,
        text: "ChatOps job started by #{user_ref} failed!",
        fields: [
          {
            short: true,
            title: "ID",
            value: "#{build_ref}"
          },
          {
            short: true,
            title: "Name",
            value: build.name
          }
        ]
      }
    ]
  )
end

#scheduled_outputObject

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



87
88
89
90
91
92
# File 'lib/gitlab/chat/responder/mattermost.rb', line 87

def scheduled_output
  {
    response_type: :ephemeral,
    text: "Your ChatOps job #{build_ref} has been created!"
  }
end

#send_response(body) ⇒ Object

Sends a response back to Mattermost

body - The message payload to send back to Mattermost, as a Hash.



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

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

#success(output) ⇒ Object

Sends the output for a build that completed successfully.

output - The output produced by the chat command.



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
59
# File 'lib/gitlab/chat/responder/mattermost.rb', line 30

def success(output)
  return if output.empty?

  send_response(
    response_type: :in_channel,
    attachments: [
      {
        color: SUCCESS_COLOR,
        text: "ChatOps job started by #{user_ref} completed successfully",
        fields: [
          {
            short: true,
            title: "ID",
            value: "#{build_ref}"
          },
          {
            short: true,
            title: "Name",
            value: build.name
          },
          {
            short: false,
            title: "Output",
            value: success_message(output)
          }
        ]
      }
    ]
  )
end