From a069aa494a71450f3a6627b723bd5312bbf20133 Mon Sep 17 00:00:00 2001 From: Omar Mekky Date: Wed, 4 Apr 2018 15:04:03 +0000 Subject: Add banzai filter to detect commit message trailers and properly link the users --- app/models/commit.rb | 3 +- app/views/projects/commit/_commit_box.html.haml | 4 +- app/views/projects/commits/_commit.atom.builder | 2 +- .../feature_detect_co_authored_commits.yml | 6 + lib/banzai/filter/commit_trailers_filter.rb | 152 ++++++++++++++++++ lib/banzai/pipeline/commit_description_pipeline.rb | 11 ++ .../banzai/filter/commit_trailers_filter_spec.rb | 171 +++++++++++++++++++++ spec/support/commit_trailers_spec_helper.rb | 41 +++++ 8 files changed, 386 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/feature_detect_co_authored_commits.yml create mode 100644 lib/banzai/filter/commit_trailers_filter.rb create mode 100644 lib/banzai/pipeline/commit_description_pipeline.rb create mode 100644 spec/lib/banzai/filter/commit_trailers_filter_spec.rb create mode 100644 spec/support/commit_trailers_spec_helper.rb diff --git a/app/models/commit.rb b/app/models/commit.rb index b64462fb768..3f7f36e83c0 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -32,7 +32,8 @@ class Commit COMMIT_SHA_PATTERN = /\h{#{MIN_SHA_LENGTH},40}/.freeze def banzai_render_context(field) - context = { pipeline: :single_line, project: self.project } + pipeline = field == :description ? :commit_description : :single_line + context = { pipeline: pipeline, project: self.project } context[:author] = self.author if self.author context diff --git a/app/views/projects/commit/_commit_box.html.haml b/app/views/projects/commit/_commit_box.html.haml index 461129a3e0e..74c5317428c 100644 --- a/app/views/projects/commit/_commit_box.html.haml +++ b/app/views/projects/commit/_commit_box.html.haml @@ -49,10 +49,10 @@ .commit-box{ data: { project_path: project_path(@project) } } %h3.commit-title - = markdown(@commit.title, pipeline: :single_line, author: @commit.author) + = markdown_field(@commit, :title) - if @commit.description.present? %pre.commit-description - = preserve(markdown(@commit.description, pipeline: :single_line, author: @commit.author)) + = preserve(markdown_field(@commit, :description)) .info-well .well-segment.branch-info diff --git a/app/views/projects/commits/_commit.atom.builder b/app/views/projects/commits/_commit.atom.builder index 50f7e7a3a33..640b5ecf99e 100644 --- a/app/views/projects/commits/_commit.atom.builder +++ b/app/views/projects/commits/_commit.atom.builder @@ -10,5 +10,5 @@ xml.entry do xml.email commit.author_email end - xml.summary markdown(commit.description, pipeline: :single_line), type: 'html' + xml.summary markdown_field(commit, :description), type: 'html' end diff --git a/changelogs/unreleased/feature_detect_co_authored_commits.yml b/changelogs/unreleased/feature_detect_co_authored_commits.yml new file mode 100644 index 00000000000..7b1269ed982 --- /dev/null +++ b/changelogs/unreleased/feature_detect_co_authored_commits.yml @@ -0,0 +1,6 @@ +--- +title: Detect commit message trailers and link users properly to their accounts + on Gitlab +merge_request: 17919 +author: cousine +type: added diff --git a/lib/banzai/filter/commit_trailers_filter.rb b/lib/banzai/filter/commit_trailers_filter.rb new file mode 100644 index 00000000000..ef16df1f3ae --- /dev/null +++ b/lib/banzai/filter/commit_trailers_filter.rb @@ -0,0 +1,152 @@ +module Banzai + module Filter + # HTML filter that replaces users' names and emails in commit trailers + # with links to their GitLab accounts or mailto links to their mentioned + # emails. + # + # Commit trailers are special labels in the form of `*-by:` and fall on a + # single line, ex: + # + # Reported-By: John S. Doe + # + # More info about this can be found here: + # * https://git.wiki.kernel.org/index.php/CommitMessageConventions + class CommitTrailersFilter < HTML::Pipeline::Filter + include ActionView::Helpers::TagHelper + include ApplicationHelper + include AvatarsHelper + + TRAILER_REGEXP = /(?