Commit 7e204cf3 authored by Stan Hu's avatar Stan Hu

Added comment notification events to HipChat and Slack services.

Supports four different event types all bundled under the "note" event type:

- comments on a commit
- comments on an issue
- comments on a merge request
- comments on a code snippet
parent 8b53d9ef
Please view this file on the master branch, on stable branches it's out of date.
v 7.9.0 (unreleased)
- Added issue and merge request events to HipChat and Slack service (Stan Hu)
- Added comment notification events to HipChat and Slack services (Stan Hu)
- Added issue and merge request events to HipChat and Slack services (Stan Hu)
- Fix merge request URL passed to Webhooks. (Stan Hu)
- Fix bug that caused a server error when editing a comment to "+1" or "-1" (Stan Hu)
- Move labels/milestones tabs to sidebar
......
......@@ -52,7 +52,8 @@ class Projects::ServicesController < Projects::ApplicationController
:build_key, :server, :teamcity_url, :build_type,
:description, :issues_url, :new_issue_url, :restrict_to_branch, :channel,
:colorize_messages, :channels,
:push_events, :issues_events, :merge_requests_events, :tag_push_events
:push_events, :issues_events, :merge_requests_events, :tag_push_events,
:note_events
)
end
end
......@@ -44,4 +44,8 @@ module GitlabRoutingHelper
def merge_request_url(entity, *args)
namespace_project_merge_request_url(entity.project.namespace, entity.project, entity, *args)
end
def snippet_url(entity, *args)
namespace_project_snippet_url(entity.project.namespace, entity.project, entity, *args)
end
end
......@@ -99,5 +99,4 @@ module Mentionable
preexisting = references(p, original)
create_cross_references!(p, a, preexisting)
end
end
......@@ -308,6 +308,10 @@ class Note < ActiveRecord::Base
end
end
def hook_attrs
attributes
end
def set_diff
# First lets find notes with same diff
# before iterating over all mr diffs
......@@ -466,6 +470,10 @@ class Note < ActiveRecord::Base
for_merge_request? && for_diff_line?
end
def for_project_snippet?
noteable_type == "Snippet"
end
# override to return commits, which are not active record
def noteable
if for_commit?
......
......@@ -15,8 +15,8 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
require 'asana'
class AsanaService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class AssemblaService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class BambooService < CiService
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
require "addressable/uri"
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class CampfireService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class GitlabIssueTrackerService < IssueTrackerService
......
......@@ -45,7 +45,7 @@ class HipchatService < Service
end
def supported_events
%w(push issue merge_request)
%w(push issue merge_request note)
end
def execute(data)
......@@ -73,6 +73,8 @@ class HipchatService < Service
create_issue_message(data) unless is_update?(data)
when "merge_request"
create_merge_request_message(data) unless is_update?(data)
when "note"
create_note_message(data)
end
end
......@@ -108,6 +110,14 @@ class HipchatService < Service
message
end
def format_body(body)
if body
body = body.truncate(200, separator: ' ', omission: '...')
end
"<pre>#{body}</pre>"
end
def create_issue_message(data)
username = data[:user][:username]
......@@ -123,8 +133,8 @@ class HipchatService < Service
message = "#{username} #{state} issue #{issue_link} in #{project_link}: <b>#{title}</b>"
if description
description = description.truncate(200, separator: ' ', omission: '...')
message << "<pre>#{description}</pre>"
description = format_body(description)
message << description
end
message
......@@ -148,8 +158,62 @@ class HipchatService < Service
"#{project_link}: <b>#{title}</b>"
if description
description = description.truncate(200, separator: ' ', omission: '...')
message << "<pre>#{description}</pre>"
description = format_body(description)
message << description
end
message
end
def format_title(title)
"<b>" + title.lines.first.chomp + "</b>"
end
def create_note_message(data)
data = HashWithIndifferentAccess.new(data)
username = data[:user][:username]
repo_attr = HashWithIndifferentAccess.new(data[:repository])
obj_attr = HashWithIndifferentAccess.new(data[:object_attributes])
note = obj_attr[:note]
note_url = obj_attr[:url]
noteable_type = obj_attr[:noteable_type]
case noteable_type
when "Commit"
commit_attr = HashWithIndifferentAccess.new(data[:commit])
subject_desc = commit_attr[:id]
subject_desc = Commit.truncate_sha(subject_desc)
subject_type = "commit"
title = format_title(commit_attr[:message])
when "Issue"
subj_attr = HashWithIndifferentAccess.new(data[:issue])
subject_id = subj_attr[:iid]
subject_desc = "##{subject_id}"
subject_type = "issue"
title = format_title(subj_attr[:title])
when "MergeRequest"
subj_attr = HashWithIndifferentAccess.new(data[:merge_request])
subject_id = subj_attr[:iid]
subject_desc = "##{subject_id}"
subject_type = "merge request"
title = format_title(subj_attr[:title])
when "Snippet"
subj_attr = HashWithIndifferentAccess.new(data[:snippet])
subject_id = subj_attr[:id]
subject_desc = "##{subject_id}"
subject_type = "snippet"
title = format_title(subj_attr[:title])
end
subject_html = "<a href=\"#{note_url}\">#{subject_type} #{subject_desc}</a>"
message = "#{username} commented on #{subject_html} in #{project_link}: "
message << title
if note
note = format_body(note)
message << note
end
message
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class IssueTrackerService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class JiraService < IssueTrackerService
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class PivotaltrackerService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class PushoverService < Service
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class RedmineService < IssueTrackerService
......
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class SlackService < Service
......@@ -43,7 +44,7 @@ class SlackService < Service
end
def supported_events
%w(push issue merge_request)
%w(push issue merge_request note)
end
def execute(data)
......@@ -69,6 +70,8 @@ class SlackService < Service
IssueMessage.new(data) unless is_update?(data)
when "merge_request"
MergeMessage.new(data) unless is_update?(data)
when "note"
NoteMessage.new(data)
end
opt = {}
......@@ -99,3 +102,4 @@ end
require "slack_service/issue_message"
require "slack_service/push_message"
require "slack_service/merge_message"
require "slack_service/note_message"
class SlackService
class NoteMessage < BaseMessage
attr_reader :message
attr_reader :username
attr_reader :project_name
attr_reader :project_link
attr_reader :note
attr_reader :note_url
attr_reader :title
def initialize(params)
params = HashWithIndifferentAccess.new(params)
@username = params[:user][:username]
@project_name = params[:project_name]
@project_url = params[:project_url]
obj_attr = params[:object_attributes]
obj_attr = HashWithIndifferentAccess.new(obj_attr)
@note = obj_attr[:note]
@note_url = obj_attr[:url]
noteable_type = obj_attr[:noteable_type]
case noteable_type
when "Commit"
create_commit_note(HashWithIndifferentAccess.new(params[:commit]))
when "Issue"
create_issue_note(HashWithIndifferentAccess.new(params[:issue]))
when "MergeRequest"
create_merge_note(HashWithIndifferentAccess.new(params[:merge_request]))
when "Snippet"
create_snippet_note(HashWithIndifferentAccess.new(params[:snippet]))
end
end
def attachments
description_message
end
private
def format_title(title)
title.lines.first.chomp
end
def create_commit_note(commit)
commit_sha = commit[:id]
commit_sha = Commit.truncate_sha(commit_sha)
commit_link = "[commit #{commit_sha}](#{@note_url})"
title = format_title(commit[:message])
@message = "#{@username} commented on #{commit_link} in #{project_link}: *#{title}*"
end
def create_issue_note(issue)
issue_iid = issue[:iid]
note_link = "[issue ##{issue_iid}](#{@note_url})"
title = format_title(issue[:title])
@message = "#{@username} commented on #{note_link} in #{project_link}: *#{title}*"
end
def create_merge_note(merge_request)
merge_request_id = merge_request[:iid]
merge_request_link = "[merge request ##{merge_request_id}](#{@note_url})"
title = format_title(merge_request[:title])
@message = "#{@username} commented on #{merge_request_link} in #{project_link}: *#{title}*"
end
def create_snippet_note(snippet)
snippet_id = snippet[:id]
snippet_link = "[snippet ##{snippet_id}](#{@note_url})"
title = format_title(snippet[:title])
@message = "#{@username} commented on #{snippet_link} in #{project_link}: *#{title}*"
end
def description_message
[{ text: format(@note), color: attachment_color }]
end
def project_link
"[#{@project_name}](#{@project_url})"
end
end
end
......@@ -15,6 +15,7 @@
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
#
class TeamcityService < CiService
......
......@@ -28,6 +28,7 @@ class Service < ActiveRecord::Base
default_value_for :issues_events, true
default_value_for :merge_requests_events, true
default_value_for :tag_push_events, true
default_value_for :note_events, true
after_initialize :initialize_properties
......@@ -42,6 +43,7 @@ class Service < ActiveRecord::Base
scope :tag_push_hooks, -> { where(tag_push_events: true, active: true) }
scope :issue_hooks, -> { where(issues_events: true, active: true) }
scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
scope :note_hooks, -> { where(note_events: true, active: true) }
def activated?
active
......
......@@ -59,6 +59,10 @@ class Snippet < ActiveRecord::Base
content
end
def hook_attrs
attributes
end
def size
0
end
......
......@@ -17,10 +17,22 @@ module Notes
note.references.each do |mentioned|
Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
end
execute_hooks(note)
end
end
note
end
def hook_data(note)
Gitlab::NoteDataBuilder.build(note, current_user)
end
def execute_hooks(note)
note_data = hook_data(note)
# TODO: Support Webhooks
note.project.execute_services(note_data, :note_hooks)
end
end
end
......@@ -47,6 +47,14 @@
%strong Tag push events
%p.light
This url will be triggered when a new tag is pushed to the repository
- if @service.supported_events.include?("note")
%div
= f.check_box :note_events, class: 'pull-left'
.prepend-left-20
= f.label :note_events, class: 'list-label' do
%strong Comments
%p.light
This url will be triggered when someone adds a comment
- if @service.supported_events.include?("issue")
%div
= f.check_box :issues_events, class: 'pull-left'
......
class AddNoteEventsToServices < ActiveRecord::Migration
def change
add_column :services, :note_events, :boolean, default: true, null: false
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150223022001) do
ActiveRecord::Schema.define(version: 20150225065047) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -371,6 +371,7 @@ ActiveRecord::Schema.define(version: 20150223022001) do
t.boolean "issues_events", default: true
t.boolean "merge_requests_events", default: true
t.boolean "tag_push_events", default: true
t.boolean "note_events", default: true, null: false
end
add_index "services", ["created_at", "id"], name: "index_services_on_created_at_and_id", using: :btree
......
module Gitlab
class NoteDataBuilder
class << self
# Produce a hash of post-receive data
#
# For all notes:
#
# data = {
# object_kind: "note",
# user: {
# name: String,
# username: String,
# avatar_url: String
# }
# project_id: Integer,
# repository: {
# name: String,
# url: String,
# description: String,
# homepage: String,
# }
# object_attributes: {
# <hook data for note>
# }
# <note-specific data>: {
# }
# note-specific data is a hash with one of the following keys and contains
# the hook data for that type.
# - commit
# - issue
# - merge_request
# - snippet
#
def build(note, user)
project = note.project
data = build_base_data(project, user, note)
if note.for_commit?
data[:commit] = build_data_for_commit(project, user, note)
elsif note.for_issue?
data[:issue] = note.noteable.hook_attrs
elsif note.for_merge_request?
data[:merge_request] = note.noteable.hook_attrs
elsif note.for_project_snippet?
data[:snippet] = note.noteable.hook_attrs
end
data
end
def build_base_data(project, user, note)
base_data = {
object_kind: "note",
user: user.hook_attrs,
project_id: project.id,
repository: {
name: project.name,
url: project.url_to_repo,
description: project.description,
homepage: project.web_url,
},
object_attributes: note.hook_attrs
}
base_data[:object_attributes][:url] =
Gitlab::UrlBuilder.new(:note).build(note.id)
base_data
end
def build_data_for_commit(project, user, note)
# commit_id is the SHA hash
commit = project.repository.commit(note.commit_id)
commit.hook_attrs(project)
end
end
end
end
......@@ -13,6 +13,9 @@ module Gitlab
build_issue_url(id)
when :merge_request
build_merge_request_url(id)
when :note
build_note_url(id)
end
end
......@@ -27,5 +30,31 @@ module Gitlab
merge_request = MergeRequest.find(id)
merge_request_url(merge_request, host: Gitlab.config.gitlab['url'])
end
def build_note_url(id)
note = Note.find(id)
if note.for_commit?
namespace_project_commit_url(namespace_id: note.project.namespace,
id: note.commit_id,
project_id: note.project,
host: Gitlab.config.gitlab['url'],
anchor: "note_#{note.id}")
elsif note.for_issue?
issue = Issue.find(note.noteable_id)
issue_url(issue,
host: Gitlab.config.gitlab['url'],
anchor: "note_#{note.id}")
elsif note.for_merge_request?
merge_request = MergeRequest.find(note.noteable_id)
merge_request_url(merge_request,
host: Gitlab.config.gitlab['url'],
anchor: "note_#{note.id}")
elsif note.for_project_snippet?
snippet = Snippet.find(note.noteable_id)
snippet_url(snippet,
host: Gitlab.config.gitlab['url'],
anchor: "note_#{note.id}")
end
end
end
end
......@@ -40,7 +40,7 @@ FactoryGirl.define do
source_branch "master"
target_branch "feature"
merge_status :can_be_merged
merge_status "can_be_merged"
trait :with_diffs do
end
......
......@@ -30,6 +30,7 @@ FactoryGirl.define do
factory :note_on_issue, traits: [:on_issue], aliases: [:votable_note]
factory :note_on_merge_request, traits: [:on_merge_request]
factory :note_on_merge_request_diff, traits: [:on_merge_request, :on_diff]
factory :note_on_project_snippet, traits: [:on_project_snippet]
trait :on_commit do
project factory: :project
......@@ -52,6 +53,11 @@ FactoryGirl.define do
noteable_type "Issue"
end
trait :on_project_snippet do
noteable_id 1
noteable_type "Snippet"
end
trait :with_attachment do