diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-05-20 21:08:57 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-05-20 21:08:57 +0000 |
commit | ba5b47c2e36d284df6827fed8e59876619aa9501 (patch) | |
tree | 50d6e05041157dea95d57eaf9b16d50501727901 /app/models | |
parent | 983b592cb7cbf3369e1af9e26f527876c6cca360 (diff) | |
parent | 129bb6c2a71c8499daeb5d55f657b0eda8366bc8 (diff) | |
download | gitlab-ce-ba5b47c2e36d284df6827fed8e59876619aa9501.tar.gz |
Merge branch 'eReGeBe/gitlab-ce-feature/milestone-md' into 'master'
Implement special GitLab markdown reference for milestones
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/3453 by @eReGeBe, with two additions:
- Move changelog item to 8.8
- Fix cross-project milestone ref with invalid project, like https://gitlab.com/gitlab-org/gitlab-ce/commit/f7348cd348ad8f4a18d74dd668283a4e236f5790 did for labels
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/13829
See merge request !3897
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/milestone.rb | 58 |
1 files changed, 51 insertions, 7 deletions
diff --git a/app/models/milestone.rb b/app/models/milestone.rb index fe9a281f366..e0c8454a998 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -59,8 +59,27 @@ class Milestone < ActiveRecord::Base end end + def self.reference_prefix + '%' + end + def self.reference_pattern - nil + # NOTE: The iid pattern only matches when all characters on the expression + # are digits, so it will match %2 but not %2.1 because that's probably a + # milestone name and we want it to be matched as such. + @reference_pattern ||= %r{ + (#{Project.reference_pattern})? + #{Regexp.escape(reference_prefix)} + (?: + (?<milestone_iid> + \d+(?!\S\w)\b # Integer-based milestone iid, or + ) | + (?<milestone_name> + [^"\s]+\b | # String-based single-word milestone title, or + "[^"]+" # String-based multi-word milestone surrounded in quotes + ) + ) + }x end def self.link_reference_pattern @@ -81,13 +100,26 @@ class Milestone < ActiveRecord::Base end end - def to_reference(from_project = nil) - escaped_title = self.title.gsub("]", "\\]") - - h = Gitlab::Routing.url_helpers - url = h.namespace_project_milestone_url(self.project.namespace, self.project, self) + ## + # Returns the String necessary to reference this Milestone in Markdown + # + # format - Symbol format to use (default: :iid, optional: :name) + # + # Examples: + # + # Milestone.first.to_reference # => "%1" + # Milestone.first.to_reference(format: :name) # => "%\"goal\"" + # Milestone.first.to_reference(project) # => "gitlab-org/gitlab-ce%1" + # + def to_reference(from_project = nil, format: :iid) + format_reference = milestone_format_reference(format) + reference = "#{self.class.reference_prefix}#{format_reference}" - "[#{escaped_title}](#{url})" + if cross_project_reference?(from_project) + project.to_reference + reference + else + reference + end end def reference_link_text(from_project = nil) @@ -159,4 +191,16 @@ class Milestone < ActiveRecord::Base issues.where(id: ids). update_all(["position = CASE #{conditions} ELSE position END", *pairs]) end + + private + + def milestone_format_reference(format = :iid) + raise ArgumentError, 'Unknown format' unless [:iid, :name].include?(format) + + if format == :name && !name.include?('"') + %("#{name}") + else + iid + end + end end |