Commit dc49f6cd authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '20321-optional-only-jira-issue-link-on-gitlab-commit-no-jira-comment' into 'master'

Add ability to make Jira comments optional

See merge request gitlab-org/gitlab!19004
parents ea27c0a7 3eee5743
...@@ -18,6 +18,7 @@ module ServiceParams ...@@ -18,6 +18,7 @@ module ServiceParams
:channels, :channels,
:color, :color,
:colorize_messages, :colorize_messages,
:comment_on_event_enabled,
:confidential_issues_events, :confidential_issues_events,
:default_irc_uri, :default_irc_uri,
:description, :description,
......
...@@ -31,6 +31,26 @@ module ServicesHelper ...@@ -31,6 +31,26 @@ module ServicesHelper
"#{event}_events" "#{event}_events"
end end
def service_event_action_field_name(action)
"#{action}_on_event_enabled"
end
def event_action_title(action)
case action
when "comment"
s_("ProjectService|Comment")
else
action.humanize
end
end
def event_action_description(action)
case action
when "comment"
s_("ProjectService|Comment will be posted on each event")
end
end
def service_save_button(service) def service_save_button(service)
button_tag(class: 'btn btn-success', type: 'submit', disabled: service.deprecated?, data: { qa_selector: 'save_changes_button' }) do button_tag(class: 'btn btn-success', type: 'submit', disabled: service.deprecated?, data: { qa_selector: 'save_changes_button' }) do
icon('spinner spin', class: 'hidden js-btn-spinner') + icon('spinner spin', class: 'hidden js-btn-spinner') +
......
...@@ -32,6 +32,10 @@ class JiraService < IssueTrackerService ...@@ -32,6 +32,10 @@ class JiraService < IssueTrackerService
%w(commit merge_request) %w(commit merge_request)
end end
def self.supported_event_actions
%w(comment)
end
# {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1 # {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
def self.reference_pattern(only_long: true) def self.reference_pattern(only_long: true)
@reference_pattern ||= /(?<issue>\b#{Gitlab::Regex.jira_issue_key_regex})/ @reference_pattern ||= /(?<issue>\b#{Gitlab::Regex.jira_issue_key_regex})/
...@@ -268,19 +272,27 @@ class JiraService < IssueTrackerService ...@@ -268,19 +272,27 @@ class JiraService < IssueTrackerService
return unless client_url.present? return unless client_url.present?
jira_request do jira_request do
remote_link = find_remote_link(issue, remote_link_props[:object][:url]) create_issue_link(issue, remote_link_props)
if remote_link create_issue_comment(issue, message)
remote_link.save!(remote_link_props)
elsif issue.comments.build.save!(body: message)
new_remote_link = issue.remotelink.build
new_remote_link.save!(remote_link_props)
end
log_info("Successfully posted", client_url: client_url) log_info("Successfully posted", client_url: client_url)
"SUCCESS: Successfully posted to #{client_url}." "SUCCESS: Successfully posted to #{client_url}."
end end
end end
def create_issue_link(issue, remote_link_props)
remote_link = find_remote_link(issue, remote_link_props[:object][:url])
remote_link ||= issue.remotelink.build
remote_link.save!(remote_link_props)
end
def create_issue_comment(issue, message)
return unless comment_on_event_enabled
issue.comments.build.save!(body: message)
end
def find_remote_link(issue, url) def find_remote_link(issue, url)
links = jira_request { issue.remotelink.all } links = jira_request { issue.remotelink.all }
return unless links return unless links
......
...@@ -155,6 +155,14 @@ class Service < ApplicationRecord ...@@ -155,6 +155,14 @@ class Service < ApplicationRecord
end end
end end
def configurable_event_actions
self.class.supported_event_actions
end
def self.supported_event_actions
%w()
end
def supported_events def supported_events
self.class.supported_events self.class.supported_events
end end
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
- if @service.configurable_events.present? - if @service.configurable_events.present?
.form-group.row .form-group.row
.col-sm-2.text-right Trigger %label.col-form-label.col-sm-2= _('Trigger')
.col-sm-10 .col-sm-10
- @service.configurable_events.each do |event| - @service.configurable_events.each do |event|
...@@ -35,6 +35,22 @@ ...@@ -35,6 +35,22 @@
%p.text-muted %p.text-muted
= @service.class.event_description(event) = @service.class.event_description(event)
- if @service.configurable_event_actions.present?
.form-group.row
%label.col-form-label.col-sm-2= _('Event Actions')
.col-sm-10
- @service.configurable_event_actions.each do |action|
.form-group
.form-check
= form.check_box service_event_action_field_name(action), class: 'form-check-input'
= form.label service_event_action_field_name(action), class: 'form-check-label' do
%strong
= event_action_description(action)
%p.text-muted
= event_action_description(action)
- @service.global_fields.each do |field| - @service.global_fields.each do |field|
- type = field[:type] - type = field[:type]
......
---
title: Add ability to make Jira comments optional
merge_request: 19004
author:
type: added
# frozen_string_literal: true
class AddCommentActionsToServices < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_column_with_default(:services, :comment_on_event_enabled, :boolean, default: true)
end
def down
remove_column(:services, :comment_on_event_enabled)
end
end
...@@ -3577,6 +3577,7 @@ ActiveRecord::Schema.define(version: 2019_11_24_150431) do ...@@ -3577,6 +3577,7 @@ ActiveRecord::Schema.define(version: 2019_11_24_150431) do
t.boolean "confidential_note_events", default: true t.boolean "confidential_note_events", default: true
t.boolean "deployment_events", default: false, null: false t.boolean "deployment_events", default: false, null: false
t.string "description", limit: 500 t.string "description", limit: 500
t.boolean "comment_on_event_enabled", default: true, null: false
t.index ["project_id"], name: "index_services_on_project_id" t.index ["project_id"], name: "index_services_on_project_id"
t.index ["template"], name: "index_services_on_template" t.index ["template"], name: "index_services_on_template"
t.index ["type"], name: "index_services_on_type" t.index ["type"], name: "index_services_on_type"
......
...@@ -20,7 +20,7 @@ Here's how the integration responds when you take the following actions in GitLa ...@@ -20,7 +20,7 @@ Here's how the integration responds when you take the following actions in GitLa
- **Mention a Jira issue ID** in a commit message or MR (merge request). - **Mention a Jira issue ID** in a commit message or MR (merge request).
- GitLab hyperlinks to the Jira issue. - GitLab hyperlinks to the Jira issue.
- The Jira issue adds an issue link to the commit/MR in GitLab. - The Jira issue adds an issue link to the commit/MR in GitLab.
- The Jira issue adds a comment reflecting the comment made in GitLab, the comment author, and a link to the commit/MR in GitLab. - The Jira issue adds a comment reflecting the comment made in GitLab, the comment author, and a link to the commit/MR in GitLab, unless this commenting to Jira is [disabled](#disabling-comments-on-jira-issues).
- **Mention that a commit or MR 'closes', 'resolves', or 'fixes' a Jira issue ID**. When the commit is made on the project's default branch (usually master) or the change is merged to the default branch: - **Mention that a commit or MR 'closes', 'resolves', or 'fixes' a Jira issue ID**. When the commit is made on the project's default branch (usually master) or the change is merged to the default branch:
- GitLab's merge request page displays a note that it "Closed" the Jira issue, with a link to the issue. (Note: Before the merge, an MR will display that it "Closes" the Jira issue.) - GitLab's merge request page displays a note that it "Closed" the Jira issue, with a link to the issue. (Note: Before the merge, an MR will display that it "Closes" the Jira issue.)
- The Jira issue shows the activity and the Jira issue is closed, or otherwise transitioned. - The Jira issue shows the activity and the Jira issue is closed, or otherwise transitioned.
...@@ -95,6 +95,15 @@ with all Jira projects in your Jira instance and you'll see the Jira link on the ...@@ -95,6 +95,15 @@ with all Jira projects in your Jira instance and you'll see the Jira link on the
![Jira service page](img/jira_service_page_v12_2.png) ![Jira service page](img/jira_service_page_v12_2.png)
### Disabling comments on Jira issues
When you reference a Jira issue, it will always link back to the source commit/MR in GitLab, however, you can control whether GitLab will also cross-post a comment to the Jira issue. That functionality is enabled by default.
To disable the automated commenting on Jira issues:
1. Open the [Integrations page](project_services.md#accessing-the-project-services) and select **Jira**.
1. In the **Event Action** section, uncheck **Comment**.
## Jira issues ## Jira issues
By now you should have [configured Jira](#configuring-jira) and enabled the By now you should have [configured Jira](#configuring-jira) and enabled the
......
...@@ -486,6 +486,12 @@ module API ...@@ -486,6 +486,12 @@ module API
name: :jira_issue_transition_id, name: :jira_issue_transition_id,
type: String, type: String,
desc: 'The ID of a transition that moves issues to a closed state. You can find this number under the Jira workflow administration (**Administration > Issues > Workflows**) by selecting **View** under **Operations** of the desired workflow of your project. The ID of each state can be found inside the parenthesis of each transition name under the **Transitions (id)** column ([see screenshot][trans]). By default, this ID is set to `2`' desc: 'The ID of a transition that moves issues to a closed state. You can find this number under the Jira workflow administration (**Administration > Issues > Workflows**) by selecting **View** under **Operations** of the desired workflow of your project. The ID of each state can be found inside the parenthesis of each transition name under the **Transitions (id)** column ([see screenshot][trans]). By default, this ID is set to `2`'
},
{
required: false,
name: :comment_on_event_enabled,
type: Boolean,
desc: 'Enable comments inside Jira issues on each GitLab event (commit / merge request)'
} }
], ],
'mattermost-slash-commands' => [ 'mattermost-slash-commands' => [
......
...@@ -6929,6 +6929,9 @@ msgstr "" ...@@ -6929,6 +6929,9 @@ msgstr ""
msgid "Estimated" msgid "Estimated"
msgstr "" msgstr ""
msgid "Event Actions"
msgstr ""
msgid "EventFilterBy|Filter by all" msgid "EventFilterBy|Filter by all"
msgstr "" msgstr ""
...@@ -13370,6 +13373,12 @@ msgstr "" ...@@ -13370,6 +13373,12 @@ msgstr ""
msgid "ProjectService|%{service_title}: status on" msgid "ProjectService|%{service_title}: status on"
msgstr "" msgstr ""
msgid "ProjectService|Comment"
msgstr ""
msgid "ProjectService|Comment will be posted on each event"
msgstr ""
msgid "ProjectService|Integrations" msgid "ProjectService|Integrations"
msgstr "" msgstr ""
...@@ -18432,6 +18441,9 @@ msgstr "" ...@@ -18432,6 +18441,9 @@ msgstr ""
msgid "Trending" msgid "Trending"
msgstr "" msgstr ""
msgid "Trigger"
msgstr ""
msgid "Trigger pipelines for mirror updates" msgid "Trigger pipelines for mirror updates"
msgstr "" msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
describe ServicesHelper do
describe 'event_action_title' do
it { expect(event_action_title('comment')).to eq 'Comment' }
it { expect(event_action_title('something')).to eq 'Something' }
end
describe 'event_action_description' do
it { expect(event_action_description('comment')).to eq 'Comment will be posted on each event' }
it { expect(event_action_description('something')).to eq nil }
end
end
...@@ -443,6 +443,7 @@ Service: ...@@ -443,6 +443,7 @@ Service:
- note_events - note_events
- pipeline_events - pipeline_events
- job_events - job_events
- comment_on_event_enabled
- category - category
- default - default
- wiki_page_events - wiki_page_events
......
...@@ -431,6 +431,16 @@ describe JiraService do ...@@ -431,6 +431,16 @@ describe JiraService do
).once ).once
end end
context 'when "comment_on_event_enabled" is set to false' do
it 'creates Remote Link reference but does not create comment' do
allow(@jira_service).to receive_messages(comment_on_event_enabled: false)
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).not_to have_requested(:post, @comment_url)
expect(WebMock).to have_requested(:post, @remote_link_url)
end
end
it 'does not send comment or remote links to issues already closed' do it 'does not send comment or remote links to issues already closed' do
allow_any_instance_of(JIRA::Resource::Issue).to receive(:resolution).and_return(true) allow_any_instance_of(JIRA::Resource::Issue).to receive(:resolution).and_return(true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment