summaryrefslogtreecommitdiff
path: root/app/services/mattermost/deploy_service.rb
blob: 761abfde0f1605f67d1dcce752d38f52f0146fa3 (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
module Mattermost
  class DeployService < BaseService
    def execute
      environment_name, action = parse_command
      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

      build = environment.last_deployment.other_actions.find { |action| action.name = action }

      generate_response(build.play(current_user))
    end

    private

    def single_resource(build)
      {
        response_type: :in_channel,
        text: "Deploy started: "
      }
    end

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

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