Files
gitlabhq/lib/gitlab/url_builder.rb
T
Rémy CoutableandYorick Peterse b255cbf396 Merge branch 'slack_wiki_notifications' into 'master'
add slack notifications for wiki pages

## What does this MR do?

Lets the Slack service be configured to send notifications when wiki pages are created or edited.

## Are there points in the code the reviewer needs to double check?

I'm just starting to get familiar with the Gitlab codebase and I was unsure on how to get the wiki page url to pass it to the slack message, on whether or not I needed to refactor the create/update methods for wiki pages from the controller to a service (but seemed necessary to test it better), and if I needed to add a column to the web hooks table or if the services table would have been enough. Please let me know if I should change anything and I will improve the MR, thanks for checking :)

## Why was this MR needed?

Related to #563 and fixes #4233.



See merge request !2998
2016-04-20 10:49:31 +02:00

69 lines
1.6 KiB
Ruby

module Gitlab
class UrlBuilder
include Gitlab::Routing.url_helpers
include GitlabRoutingHelper
include ActionView::RecordIdentifier
attr_reader :object
def self.build(object)
new(object).url
end
def url
case object
when Commit
commit_url
when Issue
issue_url(object)
when MergeRequest
merge_request_url(object)
when Note
note_url
when WikiPage
wiki_page_url
else
raise NotImplementedError.new("No URL builder defined for #{object.class}")
end
end
private
def initialize(object)
@object = object
end
def commit_url(opts = {})
return '' if object.project.nil?
namespace_project_commit_url({
namespace_id: object.project.namespace,
project_id: object.project,
id: object.id
}.merge!(opts))
end
def note_url
if object.for_commit?
commit_url(id: object.commit_id, anchor: dom_id(object))
elsif object.for_issue?
issue = Issue.find(object.noteable_id)
issue_url(issue, anchor: dom_id(object))
elsif object.for_merge_request?
merge_request = MergeRequest.find(object.noteable_id)
merge_request_url(merge_request, anchor: dom_id(object))
elsif object.for_snippet?
snippet = Snippet.find(object.noteable_id)
project_snippet_url(snippet, anchor: dom_id(object))
end
end
def wiki_page_url
"#{Gitlab.config.gitlab.url}#{object.wiki.wiki_base_path}/#{object.slug}"
end
end
end