diff options
author | Robert Speicher <robert@gitlab.com> | 2018-09-18 20:07:06 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-09-18 20:07:06 +0000 |
commit | d83c1559ad0fdb5d7c0aa42c885ff5a4ee6a0ded (patch) | |
tree | 1c31a4abcca648eca03bb73866cefa5297a82d6e | |
parent | 7e6ed7c797aaac39ab591cc926fa6285c34b24c6 (diff) | |
parent | 4ef1dd09ddc5701de5ed72887d9e682f4650e732 (diff) | |
download | gitlab-ce-d83c1559ad0fdb5d7c0aa42c885ff5a4ee6a0ded.tar.gz |
Merge branch 'relax-danger-emoji-matching' into 'master'
Improve Emoji detection in commit messages
Closes gitlab-org/release/framework#15
See merge request gitlab-org/gitlab-ce!21799
-rw-r--r-- | danger/commit_messages/Dangerfile | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/danger/commit_messages/Dangerfile b/danger/commit_messages/Dangerfile index e982e8ed7c5..a152ff837a9 100644 --- a/danger/commit_messages/Dangerfile +++ b/danger/commit_messages/Dangerfile @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'json' + # rubocop: disable Style/SignalException # rubocop: disable Metrics/CyclomaticComplexity # rubocop: disable Metrics/PerceivedComplexity @@ -8,6 +10,30 @@ # https://github.com/jonallured/danger-commit_lint because its output is not # very helpful, and it doesn't offer the means of ignoring merge commits. +class EmojiChecker + DIGESTS = File.expand_path('../../fixtures/emojis/digests.json', __dir__) + ALIASES = File.expand_path('../../fixtures/emojis/aliases.json', __dir__) + + # A regex that indicates a piece of text _might_ include an Emoji. The regex + # alone is not enough, as we'd match `:foo:bar:baz`. Instead, we use this + # regex to save us from having to check for all possible emoji names when we + # know one definitely is not included. + LIKELY_EMOJI = /:[\+a-z0-9_\-]+:/ + + def initialize + names = JSON.parse(File.read(DIGESTS)).keys + + JSON.parse(File.read(ALIASES)).keys + + @emoji = names.map { |name| ":#{name}:" } + end + + def includes_emoji?(text) + return false unless text.match?(LIKELY_EMOJI) + + @emoji.any? { |emoji| text.include?(emoji) } + end +end + def fail_commit(commit, message) fail("#{commit.sha}: #{message}") end @@ -33,6 +59,7 @@ end def lint_commits(commits) failures = false + emoji_checker = EmojiChecker.new unicode_emoji_regex = %r(( [\u{1F300}-\u{1F5FF}] | @@ -117,7 +144,7 @@ def lint_commits(commits) failures = true end - if commit.message.match?(/:[\+a-z0-9_\-]+:/) + if emoji_checker.includes_emoji?(commit.message) fail_commit( commit, 'Avoid the use of Markdown Emoji such as `:+1:`. ' \ |