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
:channels,
:color,
:colorize_messages,
:comment_on_event_enabled,
:confidential_issues_events,
:default_irc_uri,
:description,
......
......@@ -31,6 +31,26 @@ module ServicesHelper
"#{event}_events"
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)
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') +
......
......@@ -32,6 +32,10 @@ class JiraService < IssueTrackerService
%w(commit merge_request)
end
def self.supported_event_actions
%w(comment)
end
# {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
def self.reference_pattern(only_long: true)
@reference_pattern ||= /(?<issue>\b#{Gitlab::Regex.jira_issue_key_regex})/
......@@ -268,19 +272,27 @@ class JiraService < IssueTrackerService
return unless client_url.present?
jira_request do
remote_link = find_remote_link(issue, remote_link_props[:object][:url])
if remote_link
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
create_issue_link(issue, remote_link_props)
create_issue_comment(issue, message)
log_info("Successfully posted", client_url: client_url)
"SUCCESS: Successfully posted to #{client_url}."
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)
links = jira_request { issue.remotelink.all }
return unless links
......
......@@ -155,6 +155,14 @@ class Service < ApplicationRecord
end
end
def configurable_event_actions
self.class.supported_event_actions
end
def self.supported_event_actions
%w()
end
def supported_events
self.class.supported_events
end
......
......@@ -16,7 +16,7 @@
- if @service.configurable_events.present?
.form-group.row
.col-sm-2.text-right Trigger
%label.col-form-label.col-sm-2= _('Trigger')
.col-sm-10
- @service.configurable_events.each do |event|
......@@ -35,6 +35,22 @@
%p.text-muted
= @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|
- 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
t.boolean "confidential_note_events", default: true
t.boolean "deployment_events", default: false, null: false
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 ["template"], name: "index_services_on_template"
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
- **Mention a Jira issue ID** in a commit message or MR (merge request).
- GitLab hyperlinks to the Jira issue.
- 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:
- 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.
......@@ -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)
### 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
By now you should have [configured Jira](#configuring-jira) and enabled the
......
......@@ -486,6 +486,12 @@ module API
name: :jira_issue_transition_id,
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`'
},
{
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' => [
......
......@@ -6929,6 +6929,9 @@ msgstr ""
msgid "Estimated"
msgstr ""
msgid "Event Actions"
msgstr ""
msgid "EventFilterBy|Filter by all"
msgstr ""
......@@ -13370,6 +13373,12 @@ msgstr ""
msgid "ProjectService|%{service_title}: status on"
msgstr ""
msgid "ProjectService|Comment"
msgstr ""
msgid "ProjectService|Comment will be posted on each event"
msgstr ""
msgid "ProjectService|Integrations"
msgstr ""
......@@ -18432,6 +18441,9 @@ msgstr ""
msgid "Trending"
msgstr ""
msgid "Trigger"
msgstr ""
msgid "Trigger pipelines for mirror updates"
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:
- note_events
- pipeline_events
- job_events
- comment_on_event_enabled
- category
- default
- wiki_page_events
......
......@@ -431,6 +431,16 @@ describe JiraService do
).once
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
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