diff --git a/app/models/issue.rb b/app/models/issue.rb index 1d61be583..4e1ec3e1b 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -67,6 +67,17 @@ class Issue < ActiveRecord::Base recipients end + def self.find_by_hash_tag hash_tag, current_ability, project, delimiter = '#' + hash_tag =~ /([a-zA-Z0-9\-_]*\/)?([a-zA-Z0-9\-_]*)?#{delimiter}([0-9]+)/ + owner_uname = Regexp.last_match[1].presence || Regexp.last_match[2].presence || project.owner.uname + project_name = Regexp.last_match[1] ? Regexp.last_match[2] : project.name + serial_id = Regexp.last_match[3] + project = Project.find_by_owner_and_name(owner_uname.chomp('/'), project_name) + return nil unless project + return nil unless current_ability.can? :show, project + project.issues.where(:serial_id => serial_id).first + end + protected def set_serial_id diff --git a/lib/modules/models/markdown.rb b/lib/modules/models/markdown.rb index 743976da1..1890b5923 100644 --- a/lib/modules/models/markdown.rb +++ b/lib/modules/models/markdown.rb @@ -9,8 +9,14 @@ module Modules # # Supported reference formats are: # * @foo for team members - # * #123 for issues - # * !123 for pull requests + # * for issues: + # * #123 + # * abf#123 + # * abf/rosa-build#123 + # * for pull requests: + # * !123 + # * abf!123 + # * abf/rosa-build!123 # * 123456 for commits # # It also parses Emoji codes to insert images. See @@ -96,14 +102,14 @@ module Modules end REFERENCE_PATTERN = %r{ - (?\W)? # Prefix - ( # Reference - @(?[a-zA-Z][a-zA-Z0-9_\-\.]*) # User uname - |\#(?\d+) # Issue ID - |!(?\d+) # PR ID - |(?[\h]{6,40}) # Commit ID + (?[\W\/])? # Prefix + ( # Reference + @(?[a-zA-Z][a-zA-Z0-9_\-\.]*) # User uname + |(?(?:[a-zA-Z0-9\-_]*\/)?(?:[a-zA-Z0-9\-_]*)?\#[0-9]+) # Issue ID + |(?(?:[a-zA-Z0-9\-_]*\/)?(?:[a-zA-Z0-9\-_]*)?\![0-9]+) # PR ID + |(?[\h]{6,40}) # Commit ID ) - (?\W)? # Suffix + (?\W)? # Suffix }x.freeze TYPES = [:user, :issue, :pull_request, :commit].freeze @@ -166,17 +172,18 @@ module Modules end def reference_issue(identifier) - if issue = @project.issues.where(serial_id: identifier).first + if issue = Issue.find_by_hash_tag(identifier, current_ability, @project) url = project_issue_path(@project.owner, @project.name, issue.serial_id) title = "#{Issue.model_name.human}: #{issue.title}" - link_to("##{identifier}", url, html_options.merge(title: title, class: "gfm gfm-issue #{html_options[:class]}")) + link_to(identifier, url, html_options.merge(title: title, class: "gfm gfm-issue #{html_options[:class]}")) end end def reference_pull_request(identifier) - if pull_request = @project.pull_requests.includes(:issue).where(issues: {serial_id: identifier}).first + issue = Issue.find_by_hash_tag(identifier, current_ability, @project, '!') + if pull_request = issue.pull_request title = "#{PullRequest.model_name.human}: #{pull_request.title}" - link_to("!#{identifier}", project_pull_request_path(@project, pull_request), html_options.merge(title: title, class: "gfm gfm-pull_request #{html_options[:class]}")) + link_to(identifier, project_pull_request_path(@project, pull_request), html_options.merge(title: title, class: "gfm gfm-pull_request #{html_options[:class]}")) end end @@ -189,4 +196,4 @@ module Modules end end end -end \ No newline at end of file +end