Class: Gitlab::Chat::Command

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/chat/command.rb

Overview

Class for scheduling chat pipelines.

A Command takes care of creating a `Ci::Pipeline` with all the data necessary to execute a chat command. This includes data such as the chat data (e.g. the response URL) and any environment variables that should be exposed to the chat command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(project:, chat_name:, name:, arguments:, channel:, response_url:) ⇒ Command

project - The Project to schedule the command for. chat_name - The ChatName belonging to the user that scheduled the

command.

name - The name of the chat command to run. arguments - The arguments (as a String) to pass to the command. channel - The channel the message was sent from. response_url - The URL to send the response back to.


24
25
26
27
28
29
30
31
# File 'lib/gitlab/chat/command.rb', line 24

def initialize(project:, chat_name:, name:, arguments:, channel:, response_url:)
  @project = project
  @chat_name = chat_name
  @name = name
  @arguments = arguments
  @channel = channel
  @response_url = response_url
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def arguments
  @arguments
end

#channelObject (readonly)

Returns the value of attribute channel


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def channel
  @channel
end

#chat_nameObject (readonly)

Returns the value of attribute chat_name


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def chat_name
  @chat_name
end

#nameObject (readonly)

Returns the value of attribute name


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def project
  @project
end

#response_urlObject (readonly)

Returns the value of attribute response_url


14
15
16
# File 'lib/gitlab/chat/command.rb', line 14

def response_url
  @response_url
end

Instance Method Details

#branchObject


83
84
85
# File 'lib/gitlab/chat/command.rb', line 83

def branch
  strong_memoize(:branch) { project.default_branch }
end

#build_chat_data(pipeline) ⇒ Object

pipeline - The `Ci::Pipeline` to create the chat data for.


72
73
74
75
76
77
# File 'lib/gitlab/chat/command.rb', line 72

def build_chat_data(pipeline)
  pipeline.build_chat_data(
    chat_name_id: chat_name.id,
    response_url: response_url
  )
end

#build_environment_variables(pipeline) ⇒ Object

pipeline - The `Ci::Pipeline` to create the environment variables for.


64
65
66
67
68
69
# File 'lib/gitlab/chat/command.rb', line 64

def build_environment_variables(pipeline)
  pipeline.variables.build(
    [{ key: 'CHAT_INPUT', value: arguments },
     { key: 'CHAT_CHANNEL', value: channel }]
  )
end

#commitObject


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

def commit
  strong_memoize(:commit) do
    project.commit(branch)&.id if branch
  end
end

#create_pipelineObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gitlab/chat/command.rb', line 43

def create_pipeline
  service = ::Ci::CreatePipelineService.new(
    project,
    chat_name.user,
    ref: branch,
    sha: commit,
    chat_data: {
      chat_name_id: chat_name.id,
      command: name,
      arguments: arguments,
      response_url: response_url
    }
  )

  service.execute(:chat) do |pipeline|
    build_environment_variables(pipeline)
    build_chat_data(pipeline)
  end
end

#try_create_pipelineObject

Tries to create a new pipeline.

This method will return a pipeline that may be persisted, or `nil` if the pipeline could not be created.


37
38
39
40
41
# File 'lib/gitlab/chat/command.rb', line 37

def try_create_pipeline
  return unless valid?

  create_pipeline
end

#valid?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/gitlab/chat/command.rb', line 79

def valid?
  branch && commit
end