Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-11-21 23:27:58 +0300
committerAlejandro Rodríguez <alejorro70@gmail.com>2016-11-21 23:56:43 +0300
commitcdfe21421e97203885434f5136c4693311381443 (patch)
tree8ec682b60f03915f0f075c11ea36c5dfc4f62835 /lib
parent0c3cc083f0585bd71cafd1460666f3532ce03133 (diff)
Merge branch 'chatops-deploy-command' into 'master'
Add deploy chat command This adds a new ChatOps command: ``` /trigger deploy <environment> to <environment> ``` See merge request !7619
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/chat_commands/command.rb1
-rw-r--r--lib/gitlab/chat_commands/deploy.rb44
-rw-r--r--lib/gitlab/chat_commands/result.rb5
-rw-r--r--lib/mattermost/presenter.rb35
4 files changed, 72 insertions, 13 deletions
diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb
index 5f131703d40..0ec358debc7 100644
--- a/lib/gitlab/chat_commands/command.rb
+++ b/lib/gitlab/chat_commands/command.rb
@@ -4,6 +4,7 @@ module Gitlab
COMMANDS = [
Gitlab::ChatCommands::IssueShow,
Gitlab::ChatCommands::IssueCreate,
+ Gitlab::ChatCommands::Deploy,
].freeze
def execute
diff --git a/lib/gitlab/chat_commands/deploy.rb b/lib/gitlab/chat_commands/deploy.rb
new file mode 100644
index 00000000000..c0b93cca68c
--- /dev/null
+++ b/lib/gitlab/chat_commands/deploy.rb
@@ -0,0 +1,44 @@
+module Gitlab
+ module ChatCommands
+ class Deploy < BaseCommand
+ def self.match(text)
+ /\Adeploy\s+(?<from>.*)\s+to+\s+(?<to>.*)\z/.match(text)
+ end
+
+ def self.help_message
+ 'deploy <environment> to <target-environment>'
+ end
+
+ def self.available?(project)
+ project.builds_enabled?
+ end
+
+ def self.allowed?(project, user)
+ can?(user, :create_deployment, project)
+ end
+
+ def execute(match)
+ from = match[:from]
+ to = match[:to]
+
+ actions = find_actions(from, to)
+ return unless actions.present?
+
+ if actions.one?
+ actions.first.play(current_user)
+ else
+ Result.new(:error, 'Too many actions defined')
+ end
+ end
+
+ private
+
+ def find_actions(from, to)
+ environment = project.environments.find_by(name: from)
+ return unless environment
+
+ environment.actions_for(to).select(&:starts_environment?)
+ 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 00000000000..324d7ef43a3
--- /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 bfbb089eb02..6b12081575d 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.many?
+ multiple_resources(subject)
+ elsif subject.none?
+ 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)