summaryrefslogtreecommitdiff
path: root/danger/commit_messages/Dangerfile
diff options
context:
space:
mode:
Diffstat (limited to 'danger/commit_messages/Dangerfile')
-rw-r--r--danger/commit_messages/Dangerfile45
1 files changed, 42 insertions, 3 deletions
diff --git a/danger/commit_messages/Dangerfile b/danger/commit_messages/Dangerfile
index e982e8ed7c5..c5ebb9b457e 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,10 +10,38 @@
# 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
+def warn_commit(commit, message)
+ warn("#{commit.sha}: #{message}")
+end
+
def lines_changed_in_commit(commit)
commit.diff_parent.stats[:total][:lines]
end
@@ -33,6 +63,7 @@ end
def lint_commits(commits)
failures = false
+ emoji_checker = EmojiChecker.new
unicode_emoji_regex = %r((
[\u{1F300}-\u{1F5FF}] |
@@ -60,13 +91,21 @@ def lint_commits(commits)
failures = true
end
- if subject.length > 50
+ if subject.length > 72
fail_commit(
commit,
- 'The commit subject may not be longer than 50 characters'
+ 'The commit subject may not be longer than 72 characters'
)
failures = true
+ elsif subject.length > 50
+ warn_commit(
+ commit,
+ "This commit's subject line could be improved. " \
+ 'Commit subjects are ideally no longer than roughly 50 characters, ' \
+ 'though we allow up to 72 characters in the subject. ' \
+ 'If possible, try to reduce the length of the subject to roughly 50 characters.'
+ )
end
unless subject_starts_with_capital?(subject)
@@ -117,7 +156,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:`. ' \