diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-10-07 14:55:47 +0200 |
---|---|---|
committer | Z.J. van de Weg <zegerjan@gitlab.com> | 2016-10-10 20:12:33 +0200 |
commit | aad504d1ad172c8f9f0c31687077a9d9119980e7 (patch) | |
tree | 4cee37d270a08b9f98d084ce4bce9ab0020c33ea | |
parent | 0ce0c1be95728191e976f8f72843fd59719b06c7 (diff) | |
download | gitlab-ce-aad504d1ad172c8f9f0c31687077a9d9119980e7.tar.gz |
WIP
-rw-r--r-- | app/controllers/slash_commands_controller.rb | 15 | ||||
-rw-r--r-- | app/services/mattermost/deploy_service.rb | 34 | ||||
-rw-r--r-- | spec/services/mattermost/deploy_service_spec.rb | 54 |
3 files changed, 89 insertions, 14 deletions
diff --git a/app/controllers/slash_commands_controller.rb b/app/controllers/slash_commands_controller.rb index d75dec7a0bc..a0ff1612fb5 100644 --- a/app/controllers/slash_commands_controller.rb +++ b/app/controllers/slash_commands_controller.rb @@ -3,7 +3,7 @@ class SlashCommandsController < ApplicationController skip_before_action :verify_authenticity_token skip_before_action :authenticate_user! - before_action :project + before_action :find_project def trigger if service @@ -22,13 +22,22 @@ class SlashCommandsController < ApplicationController } end - def project + def project_not_found(path) + { + response_type: :ephemeral, + text: "We were unable to find a project for: ${path}" + } + end + + def find_project path = "#{params[:team_domain]}/#{params[:channel_name]}" @project = Project.find_with_namespace(path) + + render json: project_not_found(path) unless can?(user, :read_project, @project) end def user - User.find_by(username: params[:user_name]) + @user ||= User.find_by(username: params[:user_name]) end def service diff --git a/app/services/mattermost/deploy_service.rb b/app/services/mattermost/deploy_service.rb index 761abfde0f1..f23ad3007d8 100644 --- a/app/services/mattermost/deploy_service.rb +++ b/app/services/mattermost/deploy_service.rb @@ -1,17 +1,24 @@ module Mattermost class DeployService < BaseService def execute - environment_name, action = parse_command + environment_name, action_name = 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)) + return respond_404 unless can?(current_user, :create_deployment, deployment) + + build = deployment.manual_actions.find { |action| action.name = action_name } + + if build + new_build = build.play(current_user) + generate_response(new_build) + else + { + response_type: :ephemeral, + text: "No action '#{action_name}' defined for #{environment_name}." + } + end end private @@ -19,15 +26,20 @@ module Mattermost def single_resource(build) { response_type: :in_channel, - text: "Deploy started: " + text: "Action '#{action_name}' started on '#{environment_name}' you can [follow the progress](#{link(new_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\/deploy (?<name>\w+) to (?<action>\w+)/) - respond_404 unless matches + matches = params[:text].match(/\A(?<name>\w+) to (?<action>\w+)/) - matches[:name], matches[:action] + matches ? [matches[:name], matches[:action]] : nil end end end diff --git a/spec/services/mattermost/deploy_service_spec.rb b/spec/services/mattermost/deploy_service_spec.rb new file mode 100644 index 00000000000..1622ec4ae1a --- /dev/null +++ b/spec/services/mattermost/deploy_service_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe Mattermost::DeployService, services: true do + let(:project) { create(:empty_project) } + let(:user) { create(:user) } + + let(:service) { described_class.new(project, user, params) } + + shared_examples 'a 404 response' do + it 'responds with a 404 message' do + expect(subject[:response_type]).to be :ephemeral + expect(subject[:text]).to start_with '404 not found!' + end + end + + describe '#execute' do + let(:params) { { text: 'envname to action' } } + subject { service.execute } + + context 'when the environment can not be found' do + it_behaves_like 'a 404 response' + end + + context 'the environment exists' do + let!(:deployment) { create(:deployment) } + let(:project) { deployment.environment.project } + + context 'the user has no access' do + it_behaves_like 'a 404 response' + end + + context 'the user has access' do + before do + project.team << [user, :master] + end + + let(:user) { create(:user) } + let(:pipeline) { create(:pipeline) } + let!(:build) { create(:build, :manual) } + let(:params) { { text: "#{environment.name} to #{build.name}" } } + + it 'informs the user when it does not exist' do + it_behaves_like 'a 404 response' + end + + it 'executes the action if it exists' do + allow(deployment).to receive(:manual_actions).and_return([build]) + + expect(build).to receive(:manual_actions) + end + end + end + end +end |