diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2017-06-13 21:36:42 +0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2017-06-22 17:37:28 +0300 |
commit | 369138ffae7885f77a6d426b1e7bf1161da9a804 (patch) | |
tree | aed2400a54d2c17759ee59f513988e71b11a502f /app/services | |
parent | be68e64c5cdd046e1609745034eb5ae46b4bff1e (diff) |
Add related issues public API
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/issue_links/create_service.rb | 26 | ||||
-rw-r--r-- | app/services/issue_links/destroy_service.rb | 7 | ||||
-rw-r--r-- | app/services/issue_links/list_service.rb | 13 |
3 files changed, 27 insertions, 19 deletions
diff --git a/app/services/issue_links/create_service.rb b/app/services/issue_links/create_service.rb index 5f938e61280..9e0319b6ee8 100644 --- a/app/services/issue_links/create_service.rb +++ b/app/services/issue_links/create_service.rb @@ -6,7 +6,7 @@ module IssueLinks def execute if referenced_issues.blank? - return error('No Issue found for given reference', 401) + return error('No Issue found for given params', 401) end create_issue_links @@ -32,16 +32,26 @@ module IssueLinks def referenced_issues @referenced_issues ||= begin - issue_references = params[:issue_references] - text = issue_references.join(' ') + target_issue = params[:target_issue] - extractor = Gitlab::ReferenceExtractor.new(@issue.project, @current_user) - extractor.analyze(text) + issues = if params[:issue_references].present? + extract_issues_from_references + elsif target_issue + [target_issue] + end - extractor.issues.select do |issue| - can?(current_user, :admin_issue_link, issue) - end + issues&.select { |issue| can?(current_user, :admin_issue_link, issue) } end end + + def extract_issues_from_references + issue_references = params[:issue_references] + text = issue_references.join(' ') + + extractor = Gitlab::ReferenceExtractor.new(@issue.project, @current_user) + extractor.analyze(text) + + extractor.issues + end end end diff --git a/app/services/issue_links/destroy_service.rb b/app/services/issue_links/destroy_service.rb index 0b08d869c9f..082a2e1f400 100644 --- a/app/services/issue_links/destroy_service.rb +++ b/app/services/issue_links/destroy_service.rb @@ -8,6 +8,8 @@ module IssueLinks end def execute + return error('Unauthorized', 401) unless permission_to_remove_relation? + remove_relation create_notes @@ -24,5 +26,10 @@ module IssueLinks SystemNoteService.unrelate_issue(@issue, @referenced_issue, current_user) SystemNoteService.unrelate_issue(@referenced_issue, @issue, current_user) end + + def permission_to_remove_relation? + can?(current_user, :admin_issue_link, @issue) && + can?(current_user, :admin_issue_link, @referenced_issue) + end end end diff --git a/app/services/issue_links/list_service.rb b/app/services/issue_links/list_service.rb index 609b125c31d..aa912ef859a 100644 --- a/app/services/issue_links/list_service.rb +++ b/app/services/issue_links/list_service.rb @@ -22,16 +22,7 @@ module IssueLinks private def issues - related_issues = Issue - .select(['issues.*', 'issue_links.id AS issue_links_id']) - .joins("INNER JOIN issue_links ON - (issue_links.source_id = issues.id AND issue_links.target_id = #{@issue.id}) - OR - (issue_links.target_id = issues.id AND issue_links.source_id = #{@issue.id})") - .preload(project: :namespace) - .reorder('issue_links_id') - - Ability.issues_readable_by_user(related_issues, @current_user) + @issue.related_issues(@current_user, preload: { project: :namespace }) end def destroy_relation_path(issue) @@ -41,7 +32,7 @@ module IssueLinks namespace_project_issue_link_path(@project.namespace, @issue.project, @issue.iid, - issue.issue_links_id) + issue.issue_link_id) end end |