2018-09-05 08:27:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
require_relative File.expand_path('../../lib/gitlab/danger/commit_linter', __dir__)
|
2018-09-18 10:14:38 -04:00
|
|
|
|
2020-03-27 14:07:48 -04:00
|
|
|
COMMIT_MESSAGE_GUIDELINES = "https://docs.gitlab.com/ee/development/contributing/merge_request_workflow.html#commit-messages-guidelines"
|
|
|
|
MORE_INFO = "For more information, take a look at our [Commit message guidelines](#{COMMIT_MESSAGE_GUIDELINES})."
|
|
|
|
THE_DANGER_JOB_TEXT = "the `danger-review` job"
|
2020-01-16 13:08:46 -05:00
|
|
|
MAX_COMMITS_COUNT = 10
|
2020-06-02 11:08:24 -04:00
|
|
|
MAX_COMMITS_COUNT_EXCEEDED_MESSAGE = <<~MSG
|
|
|
|
This merge request includes more than %<max_commits_count>d commits. Each commit should meet the following criteria:
|
|
|
|
|
|
|
|
1. Have a well-written commit message.
|
|
|
|
1. Has all tests passing when used on its own (e.g. when using git checkout SHA).
|
|
|
|
1. Can be reverted on its own without also requiring the revert of commit that came before it.
|
|
|
|
1. Is small enough that it can be reviewed in isolation in under 30 minutes or so.
|
|
|
|
|
|
|
|
If this merge request contains commits that do not meet this criteria and/or contains intermediate work, please rebase these commits into a smaller number of commits or split this merge request into multiple smaller merge requests.
|
|
|
|
MSG
|
2018-09-18 10:14:38 -04:00
|
|
|
|
2019-09-16 08:06:26 -04:00
|
|
|
def gitlab_danger
|
|
|
|
@gitlab_danger ||= GitlabDanger.new(helper.gitlab_helper)
|
|
|
|
end
|
|
|
|
|
2020-03-27 14:07:48 -04:00
|
|
|
def fail_commit(commit, message, more_info: true)
|
|
|
|
self.fail(build_message(commit, message, more_info: more_info))
|
2018-09-05 08:27:01 -04:00
|
|
|
end
|
|
|
|
|
2020-03-27 14:07:48 -04:00
|
|
|
def warn_commit(commit, message, more_info: true)
|
|
|
|
self.warn(build_message(commit, message, more_info: more_info))
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_message(commit, message, more_info: true)
|
|
|
|
[message].tap do |full_message|
|
|
|
|
full_message << ". #{MORE_INFO}" if more_info
|
|
|
|
full_message.unshift("#{commit.sha}: ") if commit.sha
|
|
|
|
end.join
|
2018-09-05 08:27:01 -04:00
|
|
|
end
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def squash_mr?
|
|
|
|
gitlab_danger.ci? ? gitlab.mr_json['squash'] : false
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
2018-09-05 08:27:01 -04:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def wip_mr?
|
|
|
|
gitlab_danger.ci? ? gitlab.mr_json['work_in_progress'] : false
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
|
|
|
|
2020-03-27 14:07:48 -04:00
|
|
|
def danger_job_link
|
|
|
|
gitlab_danger.ci? ? "[#{THE_DANGER_JOB_TEXT}](#{ENV['CI_JOB_URL']})" : THE_DANGER_JOB_TEXT
|
|
|
|
end
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
# Perform various checks against commits. We're not using
|
|
|
|
# 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.
|
|
|
|
def lint_commit(commit)
|
|
|
|
linter = Gitlab::Danger::CommitLinter.new(commit)
|
2019-11-15 13:06:24 -05:00
|
|
|
|
2019-02-20 08:25:12 -05:00
|
|
|
# For now we'll ignore merge commits, as getting rid of those is a problem
|
|
|
|
# separate from enforcing good commit messages.
|
2020-01-16 13:08:46 -05:00
|
|
|
return linter if linter.merge?
|
2019-02-20 08:25:12 -05:00
|
|
|
|
|
|
|
# We ignore revert commits as they are well structured by Git already
|
2020-01-16 13:08:46 -05:00
|
|
|
return linter if linter.revert?
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
# If MR is set to squash, we ignore fixup commits
|
|
|
|
return linter if linter.fixup? && squash_mr?
|
2019-09-10 10:49:19 -04:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
if linter.fixup?
|
2020-03-27 14:07:48 -04:00
|
|
|
msg = "Squash or fixup commits must be squashed before merge, or enable squash merge option and re-run #{danger_job_link}."
|
2020-01-16 13:08:46 -05:00
|
|
|
if wip_mr? || squash_mr?
|
2020-03-27 14:07:48 -04:00
|
|
|
warn_commit(commit, msg, more_info: false)
|
2019-09-10 10:49:19 -04:00
|
|
|
else
|
2020-03-27 14:07:48 -04:00
|
|
|
fail_commit(commit, msg, more_info: false)
|
2019-09-10 10:49:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Makes no sense to process other rules for fixup commits, they trigger just more noise
|
2020-01-16 13:08:46 -05:00
|
|
|
return linter
|
2019-09-10 10:49:19 -04:00
|
|
|
end
|
|
|
|
|
2019-07-16 12:51:31 -04:00
|
|
|
# Fail if a suggestion commit is used and squash is not enabled
|
2020-01-16 13:08:46 -05:00
|
|
|
if linter.suggestion?
|
|
|
|
unless squash_mr?
|
2020-03-27 14:07:48 -04:00
|
|
|
fail_commit(commit, "If you are applying suggestions, enable squash in the merge request and re-run #{danger_job_link}.", more_info: false)
|
2019-07-16 12:51:31 -04:00
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
return linter
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
linter.lint
|
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def lint_mr_title(mr_title)
|
|
|
|
commit = Struct.new(:message, :sha).new(mr_title)
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
Gitlab::Danger::CommitLinter.new(commit).lint_subject("merge request title")
|
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def count_non_fixup_commits(commit_linters)
|
|
|
|
commit_linters.count { |commit_linter| !commit_linter.fixup? }
|
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def lint_commits(commits)
|
|
|
|
commit_linters = commits.map { |commit| lint_commit(commit) }
|
|
|
|
failed_commit_linters = commit_linters.select { |commit_linter| commit_linter.failed? }
|
|
|
|
warn_or_fail_commits(failed_commit_linters, default_to_fail: !squash_mr?)
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
if count_non_fixup_commits(commit_linters) > MAX_COMMITS_COUNT
|
2020-06-02 11:08:24 -04:00
|
|
|
self.warn(format(MAX_COMMITS_COUNT_EXCEEDED_MESSAGE, max_commits_count: MAX_COMMITS_COUNT))
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
if squash_mr?
|
2020-02-12 04:09:11 -05:00
|
|
|
multi_line_commit_linter = commit_linters.detect { |commit_linter| !commit_linter.merge? && commit_linter.multi_line? }
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-02-12 04:09:11 -05:00
|
|
|
if multi_line_commit_linter && multi_line_commit_linter.failed?
|
2020-01-16 13:08:46 -05:00
|
|
|
warn_or_fail_commits(multi_line_commit_linter)
|
|
|
|
else
|
|
|
|
title_linter = lint_mr_title(gitlab.mr_json['title'])
|
|
|
|
if title_linter.failed?
|
|
|
|
warn_or_fail_commits(title_linter)
|
|
|
|
end
|
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def warn_or_fail_commits(failed_linters, default_to_fail: true)
|
|
|
|
level = default_to_fail ? :fail : :warn
|
|
|
|
|
|
|
|
Array(failed_linters).each do |linter|
|
|
|
|
linter.problems.each do |problem_key, problem_desc|
|
|
|
|
case problem_key
|
|
|
|
when :subject_above_warning
|
|
|
|
warn_commit(linter.commit, problem_desc)
|
|
|
|
else
|
|
|
|
self.__send__("#{level}_commit", linter.commit, problem_desc) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
end
|
2019-02-20 08:25:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-20 10:07:34 -05:00
|
|
|
lint_commits(git.commits)
|