summaryrefslogtreecommitdiff
path: root/lib/gitlab/slash_commands
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2018-03-12 15:29:45 +0000
committerSean McGivern <sean@gitlab.com>2018-03-15 13:09:11 +0000
commit0fa139dfda0b2f5d7b15ba60d7b3a4a6d9a22c49 (patch)
treef79e08530601d79aefa6e16f6ee6626aea1f1c5d /lib/gitlab/slash_commands
parent82b6222022c496290066fbdd6c3c2490bd23622c (diff)
downloadgitlab-ce-0fa139dfda0b2f5d7b15ba60d7b3a4a6d9a22c49.tar.gz
Add slash command for moving an issue
Carried over from https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/8857
Diffstat (limited to 'lib/gitlab/slash_commands')
-rw-r--r--lib/gitlab/slash_commands/command.rb1
-rw-r--r--lib/gitlab/slash_commands/issue_move.rb45
-rw-r--r--lib/gitlab/slash_commands/presenters/issue_move.rb53
3 files changed, 99 insertions, 0 deletions
diff --git a/lib/gitlab/slash_commands/command.rb b/lib/gitlab/slash_commands/command.rb
index 85aaa6b0eba..bb778f37096 100644
--- a/lib/gitlab/slash_commands/command.rb
+++ b/lib/gitlab/slash_commands/command.rb
@@ -5,6 +5,7 @@ module Gitlab
Gitlab::SlashCommands::IssueShow,
Gitlab::SlashCommands::IssueNew,
Gitlab::SlashCommands::IssueSearch,
+ Gitlab::SlashCommands::IssueMove,
Gitlab::SlashCommands::Deploy
].freeze
diff --git a/lib/gitlab/slash_commands/issue_move.rb b/lib/gitlab/slash_commands/issue_move.rb
new file mode 100644
index 00000000000..3985e635983
--- /dev/null
+++ b/lib/gitlab/slash_commands/issue_move.rb
@@ -0,0 +1,45 @@
+module Gitlab
+ module SlashCommands
+ class IssueMove < IssueCommand
+ def self.match(text)
+ %r{
+ \A # the beginning of a string
+ issue\s+move\s+ # the command
+ \#?(?<iid>\d+)\s+ # the issue id, may preceded by hash sign
+ (to\s+)? # aid the command to be much more human-ly
+ (?<project_path>[^\s]+) # named group for id of dest. project
+ }x.match(text)
+ end
+
+ def self.help_message
+ 'issue move <issue_id> (to)? <project_path>'
+ end
+
+ def self.allowed?(project, user)
+ can?(user, :admin_issue, project)
+ end
+
+ def execute(match)
+ old_issue = find_by_iid(match[:iid])
+ target_project = Project.find_by_full_path(match[:project_path])
+
+ unless current_user.can?(:read_project, target_project) && old_issue
+ return Gitlab::SlashCommands::Presenters::Access.new.not_found
+ end
+
+ new_issue = Issues::MoveService.new(project, current_user)
+ .execute(old_issue, target_project)
+
+ presenter(new_issue).present(old_issue)
+ rescue Issues::MoveService::MoveError => e
+ presenter(old_issue).display_move_error(e.message)
+ end
+
+ private
+
+ def presenter(issue)
+ Gitlab::SlashCommands::Presenters::IssueMove.new(issue)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/slash_commands/presenters/issue_move.rb b/lib/gitlab/slash_commands/presenters/issue_move.rb
new file mode 100644
index 00000000000..03921729941
--- /dev/null
+++ b/lib/gitlab/slash_commands/presenters/issue_move.rb
@@ -0,0 +1,53 @@
+# coding: utf-8
+module Gitlab
+ module SlashCommands
+ module Presenters
+ class IssueMove < Presenters::Base
+ include Presenters::IssueBase
+
+ def present(old_issue)
+ in_channel_response(moved_issue(old_issue))
+ end
+
+ def display_move_error(error)
+ message = header_with_list("The action was not successful, because:", [error])
+
+ ephemeral_response(text: message)
+ end
+
+ private
+
+ def moved_issue(old_issue)
+ {
+ attachments: [
+ {
+ title: "#{@resource.title} ยท #{@resource.to_reference}",
+ title_link: resource_url,
+ author_name: author.name,
+ author_icon: author.avatar_url,
+ fallback: "Issue #{@resource.to_reference}: #{@resource.title}",
+ pretext: pretext(old_issue),
+ color: color(@resource),
+ fields: fields,
+ mrkdwn_in: [
+ :title,
+ :pretext,
+ :text,
+ :fields
+ ]
+ }
+ ]
+ }
+ end
+
+ def pretext(old_issue)
+ "Moved issue *#{issue_link(old_issue)}* to *#{issue_link(@resource)}*"
+ end
+
+ def issue_link(issue)
+ "[#{issue.to_reference}](#{project_issue_url(issue.project, issue)})"
+ end
+ end
+ end
+ end
+end