diff --git a/app/models/environment.rb b/app/models/environment.rb index 5278efd71d2b008de8d132f118b6e2b8f2a5c395..e6dd7717e466e3abb3949f5b7dae222ddae77841 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -19,7 +19,7 @@ class Environment < ActiveRecord::Base allow_nil: true, addressable_url: true - delegate :stop_action, to: :last_deployment, allow_nil: true + delegate :stop_action, :manual_actions, to: :last_deployment, allow_nil: true scope :available, -> { with_state(:available) } scope :stopped, -> { with_state(:stopped) } @@ -99,4 +99,12 @@ class Environment < ActiveRecord::Base stop stop_action.play(current_user) end + + def actions_for(environment) + return [] unless manual_actions + + manual_actions.select |action| + action.expanded_environment_name = environment + end + end end diff --git a/lib/gitlab/chat_commands/deploy.rb b/lib/gitlab/chat_commands/deploy.rb new file mode 100644 index 0000000000000000000000000000000000000000..8165d52575d5527be8eed7489ac828a79274f6e6 --- /dev/null +++ b/lib/gitlab/chat_commands/deploy.rb @@ -0,0 +1,34 @@ +module Gitlab + module ChatCommands + class Deploy < BaseCommand + def self.match(text) + /\Adeploy\s+(?.*)\s+to+\s+(?.*)\z/.match(text) + end + + def self.help_message + 'deploy to ' + end + + def self.allowed?(project, user) + can?(user, :create_deployment, project) + end + + def execute(match) + from = match[:from] + to = match[:to] + + environment = project.environments.find_by(name: from) + return unless environment + + actions = environment.actions_for(to) + return unless actions.any? + + if actions.one? + actions.first.play(current_user) + else + Result.new(:error, 'Too many actions defined') + end + end + end + end +end diff --git a/lib/gitlab/chat_commands/result.rb b/lib/gitlab/chat_commands/result.rb new file mode 100644 index 0000000000000000000000000000000000000000..324d7ef43a3bc30d5165770d326fe7447699b195 --- /dev/null +++ b/lib/gitlab/chat_commands/result.rb @@ -0,0 +1,5 @@ +module Gitlab + module ChatCommands + Result = Struct.new(:type, :message) + end +end diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb index bfbb089eb02d4bce220ad9639ca06f453e4f20a7..8ecab01a35905613ad9fd379c629d21edd5b02a2 100644 --- a/lib/mattermost/presenter.rb +++ b/lib/mattermost/presenter.rb @@ -24,20 +24,22 @@ module Mattermost end end - def present(resource) - return not_found unless resource - - if resource.respond_to?(:count) - if resource.count > 1 - return multiple_resources(resource) - elsif resource.count == 0 - return not_found + def present(subject) + return not_found unless subject + + if subject.is_a?(Gitlab::ChatCommands::Result) + show_result(subject) + elsif subject.respond_to?(:count) + if subject.try(:many?) + multiple_resources(subject) + elsif subject.count == 0 + not_found else - resource = resource.first + single_resource(subject) end + else + single_resource(subject) end - - single_resource(resource) end def access_denied @@ -46,6 +48,10 @@ module Mattermost private + def show_result(result) + ephemeral_response(result.message) + end + def not_found ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:") end @@ -54,7 +60,7 @@ module Mattermost return error(resource) if resource.errors.any? || !resource.persisted? message = "### #{title(resource)}" - message << "\n\n#{resource.description}" if resource.description + message << "\n\n#{resource.description}" if resource.try(:description) in_channel_response(message) end @@ -74,7 +80,10 @@ module Mattermost end def title(resource) - "[#{resource.to_reference} #{resource.title}](#{url(resource)})" + reference = resource.try(:to_reference) || resource.try(:id) + title = resource.try(:title) || resource.try(:name) + + "[#{reference} #{title}](#{url(resource)})" end def header_with_list(header, items)