Skip to content
Snippets Groups Projects
Commit 4dd1f906 authored by Oswaldo Ferreir's avatar Oswaldo Ferreir
Browse files

Add support for patch link extension for commit links on GFM

parent 166b4575
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -30,6 +30,8 @@ class Commit
 
MIN_SHA_LENGTH = Gitlab::Git::Commit::MIN_SHA_LENGTH
COMMIT_SHA_PATTERN = /\h{#{MIN_SHA_LENGTH},40}/.freeze
# Used by GFM to match and present link extensions on node texts and hrefs.
LINK_EXTENSION_PATTERN = /(patch)/.freeze
 
def banzai_render_context(field)
pipeline = field == :description ? :commit_description : :single_line
Loading
Loading
@@ -143,7 +145,8 @@ class Commit
end
 
def self.link_reference_pattern
@link_reference_pattern ||= super("commit", /(?<commit>#{COMMIT_SHA_PATTERN})/)
@link_reference_pattern ||=
super("commit", /(?<commit>#{COMMIT_SHA_PATTERN})?(\.(?<extension>#{LINK_EXTENSION_PATTERN}))?/)
end
 
def to_reference(from = nil, full: false)
Loading
Loading
---
title: Add support for patch link extension for commit links on GitLab Flavored Markdown
merge_request:
author:
type: added
Loading
Loading
@@ -213,6 +213,10 @@ module Banzai
extras << "comment #{$1}"
end
 
extension = matches[:extension] if matches.names.include?("extension")
extras << extension if extension
extras
end
 
Loading
Loading
Loading
Loading
@@ -207,4 +207,35 @@ describe Banzai::Filter::CommitReferenceFilter do
expect(reference_filter(act).to_html).to match(%r{<a.+>#{Regexp.escape(invalidate_reference(reference))}</a>})
end
end
context 'URL reference for a commit patch' do
let(:namespace) { create(:namespace) }
let(:project2) { create(:project, :public, :repository, namespace: namespace) }
let(:commit) { project2.commit }
let(:link) { urls.project_commit_url(project2, commit.id) }
let(:extension) { '.patch' }
let(:reference) { link + extension }
it 'links to a valid reference' do
doc = reference_filter("See #{reference}")
expect(doc.css('a').first.attr('href'))
.to eq reference
end
it 'has valid text' do
doc = reference_filter("See #{reference}")
expect(doc.text).to eq("See #{commit.reference_link_text(project)} (patch)")
end
it 'does not link to patch when extension match is after the path' do
invalidate_commit_reference = reference_filter("#{link}/builds.patch")
doc = reference_filter("See (#{invalidate_commit_reference})")
expect(doc.css('a').first.attr('href')).to eq "#{link}/builds"
expect(doc.text).to eq("See (#{commit.reference_link_text(project)} (builds).patch)")
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment