summaryrefslogtreecommitdiff
path: root/app/services/mattermost/deploy_service.rb
blob: 2179c05a587bceb7e05d272003168fe6dd8c7c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Mattermost
  class DeployService < BaseService
    def execute
      environment_name, action_name = parse_command
      return respond_404 unless environment_name

      environment = project.environments.find_by(name: environment_name)

      return respond_404 unless can?(current_user, :read_environment, environment)

      deployment = environment.last_deployment
      return respond_404 unless can?(current_user, :create_deployment, deployment) && deployment.deployable

      manual_action = deployment.manual_actions.find { |action| action.name = action_name }
      
      if manual_action
        new_build = manual_action.play(current_user)

        {
          response_type: :in_channel,
          text: "Action '#{action_name}' started on '#{environment_name}' you can [follow the progress](#{link(new_build)})."
        }
      else
        {
          response_type: :ephemeral,
          text: "No action '#{action_name}' defined for #{environment_name}."
        }
      end
    end

    private

    def single_resource(build)
    end

    def link(build)
      Gitlab::Routing.
        url_helpers.
        namespace_project_build_url(project.namespace, project, build)
    end

    def parse_command
      matches = params[:text].match(/\A(?<name>\w+) to (?<action>\w+)/)

      matches ? [matches[:name], matches[:action]] : nil
    end
  end
end