Class: Gitlab::SlashCommands::Run

Inherits:
BaseCommand show all
Defined in:
lib/gitlab/slash_commands/run.rb

Overview

Slash command for triggering chatops jobs.

Constant Summary

Constants inherited from BaseCommand

BaseCommand::QUERY_LIMIT

Instance Attribute Summary

Attributes inherited from BaseCommand

#chat_name, #current_user, #params, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

can?, #collection, #initialize

Constructor Details

This class inherits a constructor from Gitlab::SlashCommands::BaseCommand

Class Method Details

.allowed?(project, user) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/gitlab/slash_commands/run.rb', line 19

def self.allowed?(project, user)
  can?(user, :create_pipeline, project)
end

.available?(project) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/gitlab/slash_commands/run.rb', line 15

def self.available?(project)
  Chat.available? && project.builds_enabled?
end

.help_messageObject



11
12
13
# File 'lib/gitlab/slash_commands/run.rb', line 11

def self.help_message
  'run <command> <arguments>'
end

.match(text) ⇒ Object



7
8
9
# File 'lib/gitlab/slash_commands/run.rb', line 7

def self.match(text)
  /\Arun\s+(?<command>\S+)(\s+(?<arguments>.+))?\z/m.match(text)
end

Instance Method Details

#execute(match) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/slash_commands/run.rb', line 23

def execute(match)
  command = Chat::Command.new(
    project: project,
    chat_name: chat_name,
    name: match[:command],
    arguments: match[:arguments],
    channel: params[:channel_id],
    response_url: params[:response_url]
  )

  presenter = Gitlab::SlashCommands::Presenters::Run.new
  pipeline = command.try_create_pipeline

  if pipeline&.persisted?
    presenter.present(pipeline)
  else
    presenter.failed_to_schedule(command.name)
  end
end