Commit 5a0f8c5d authored by Paul Gascou-Vaillancourt's avatar Paul Gascou-Vaillancourt Committed by Lin Jen-Shin

Create changelog plugin

- Moved helper methods from Dangerfile into a new Danger plugin
- Added tests for all public methods in the plugin
parent e9350d05
......@@ -5,6 +5,7 @@ require_relative 'lib/gitlab/danger/request_helper'
danger.import_plugin('danger/plugins/helper.rb')
danger.import_plugin('danger/plugins/roulette.rb')
danger.import_plugin('danger/plugins/changelog.rb')
unless helper.release_automation?
GitlabDanger.new(helper.gitlab_helper).rule_names.each do |file|
......
......@@ -3,7 +3,6 @@
require 'yaml'
NO_CHANGELOG_LABELS = %w[backstage ci-build meta].freeze
SEE_DOC = "See [the documentation](https://docs.gitlab.com/ce/development/changelog.html)."
CREATE_CHANGELOG_MESSAGE = <<~MSG
You can create one with:
......@@ -21,18 +20,6 @@ bin/changelog --ee -m %<mr_iid>s "%<mr_title>s"
Note: Merge requests with %<labels>s do not trigger this check.
MSG
def ee_changelog?(changelog_path)
changelog_path =~ /unreleased-ee/
end
def ce_port_changelog?(changelog_path)
helper.ee? && !ee_changelog?(changelog_path)
end
def categories_need_changelog?
(helper.changes_by_category.keys - %i[docs none]).any?
end
def check_changelog(path)
yaml = YAML.safe_load(File.read(path))
......@@ -41,7 +28,7 @@ def check_changelog(path)
if yaml["merge_request"].nil?
message "Consider setting `merge_request` to #{gitlab.mr_json["iid"]} in #{gitlab.html_link(path)}. #{SEE_DOC}"
elsif yaml["merge_request"] != gitlab.mr_json["iid"] && !ce_port_changelog?(path)
elsif yaml["merge_request"] != gitlab.mr_json["iid"] && !changelog.ce_port_changelog?(path)
fail "Merge request ID was not set to #{gitlab.mr_json["iid"]}! #{SEE_DOC}"
end
rescue Psych::SyntaxError, Psych::DisallowedClass, Psych::BadAlias
......@@ -51,27 +38,18 @@ rescue StandardError => e
warn "There was a problem trying to check the Changelog. Exception: #{e.name} - #{e.message}"
end
def presented_no_changelog_labels
NO_CHANGELOG_LABELS.map { |label| "~#{label}" }.join(', ')
end
def sanitized_mr_title
gitlab.mr_json["title"].gsub(/^WIP: */, '').gsub(/`/, '\\\`')
end
changelog_needed = categories_need_changelog? && (gitlab.mr_labels & NO_CHANGELOG_LABELS).empty?
changelog_found = git.added_files.find { |path| path =~ %r{\A(ee/)?(changelogs/unreleased)(-ee)?/} }
if git.modified_files.include?("CHANGELOG.md")
fail "**CHANGELOG.md was edited.** Please remove the additions and create a CHANGELOG entry.\n\n" +
format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: sanitized_mr_title, labels: presented_no_changelog_labels)
format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: changelog.sanitized_mr_title, labels: changelog.presented_no_changelog_labels)
end
if changelog_needed
changelog_found = changelog.found
if changelog.needed?
if changelog_found
check_changelog(changelog_found)
else
message "**[CHANGELOG missing](https://docs.gitlab.com/ce/development/changelog.html)**: If this merge request [doesn't need a CHANGELOG entry](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry), feel free to ignore this message.\n\n" +
format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: sanitized_mr_title, labels: presented_no_changelog_labels)
format(CREATE_CHANGELOG_MESSAGE, mr_iid: gitlab.mr_json["iid"], mr_title: changelog.sanitized_mr_title, labels: changelog.presented_no_changelog_labels)
end
end
# frozen_string_literal: true
require_relative '../../lib/gitlab/danger/changelog'
module Danger
class Changelog < Plugin
# Put the helper code somewhere it can be tested
include Gitlab::Danger::Changelog
end
end
# frozen_string_literal: true
module Gitlab
module Danger
module Changelog
NO_CHANGELOG_LABELS = %w[backstage ci-build meta].freeze
NO_CHANGELOG_CATEGORIES = %i[docs none].freeze
def needed?
categories_need_changelog? && (gitlab.mr_labels & NO_CHANGELOG_LABELS).empty?
end
def found
git.added_files.find { |path| path =~ %r{\A(ee/)?(changelogs/unreleased)(-ee)?/} }
end
def presented_no_changelog_labels
NO_CHANGELOG_LABELS.map { |label| "~#{label}" }.join(', ')
end
def sanitized_mr_title
gitlab.mr_json["title"].gsub(/^WIP: */, '').gsub(/`/, '\\\`')
end
def ee_changelog?(changelog_path)
changelog_path =~ /unreleased-ee/
end
def ce_port_changelog?(changelog_path)
helper.ee? && !ee_changelog?(changelog_path)
end
private
def categories_need_changelog?
(helper.changes_by_category.keys - NO_CHANGELOG_CATEGORIES).any?
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
require_relative 'danger_spec_helper'
require 'gitlab/danger/changelog'
describe Gitlab::Danger::Changelog do
using RSpec::Parameterized::TableSyntax
include DangerSpecHelper
let(:added_files) { nil }
let(:fake_git) { double('fake-git', added_files: added_files) }
let(:mr_labels) { nil }
let(:mr_json) { nil }
let(:fake_gitlab) { double('fake-gitlab', mr_labels: mr_labels, mr_json: mr_json) }
let(:changes_by_category) { nil }
let(:ee?) { false }
let(:fake_helper) { double('fake-helper', changes_by_category: changes_by_category, ee?: ee?) }
let(:fake_danger) { new_fake_danger.include(described_class) }
subject(:changelog) { fake_danger.new(git: fake_git, gitlab: fake_gitlab, helper: fake_helper) }
describe '#needed?' do
subject { changelog.needed? }
[
{ docs: nil },
{ none: nil },
{ docs: nil, none: nil }
].each do |categories|
let(:changes_by_category) { categories }
it "is falsy when categories don't require a changelog" do
is_expected.to be_falsy
end
end
where(:categories, :labels) do
{ backend: nil } | %w[backend backstage]
{ frontend: nil, docs: nil } | ['ci-build']
{ engineering_productivity: nil, none: nil } | ['meta']
end
with_them do
let(:changes_by_category) { categories }
let(:mr_labels) { labels }
it "is falsy when labels require no changelog" do
is_expected.to be_falsy
end
end
where(:categories, :labels) do
{ frontend: nil, docs: nil } | ['database::review pending', 'feature']
{ backend: nil } | ['backend', 'technical debt']
{ engineering_productivity: nil, none: nil } | ['frontend']
end
with_them do
let(:changes_by_category) { categories }
let(:mr_labels) { labels }
it "is truthy when categories and labels require a changelog" do
is_expected.to be_truthy
end
end
end
describe '#found' do
subject { changelog.found }
context 'added files contain a changelog' do
[
'changelogs/unreleased/entry.md',
'ee/changelogs/unreleased/entry.md',
'changelogs/unreleased-ee/entry.md',
'ee/changelogs/unreleased-ee/entry.md'
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to be_truthy }
end
end
context 'added files do not contain a changelog' do
[
'app/models/model.rb',
'app/assets/javascripts/file.js'
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to eq(nil) }
end
end
end
describe '#presented_no_changelog_labels' do
subject { changelog.presented_no_changelog_labels }
it 'returns the labels formatted' do
is_expected.to eq('~backstage, ~ci-build, ~meta')
end
end
describe '#sanitized_mr_title' do
subject { changelog.sanitized_mr_title }
[
'WIP: My MR title',
'My MR title'
].each do |mr_title|
let(:mr_json) { { "title" => mr_title } }
it { is_expected.to eq("My MR title") }
end
end
describe '#ee_changelog?' do
context 'is ee changelog' do
[
'changelogs/unreleased-ee/entry.md',
'ee/changelogs/unreleased-ee/entry.md'
].each do |file_path|
subject { changelog.ee_changelog?(file_path) }
it { is_expected.to be_truthy }
end
end
context 'is not ee changelog' do
[
'changelogs/unreleased/entry.md',
'ee/changelogs/unreleased/entry.md'
].each do |file_path|
subject { changelog.ee_changelog?(file_path) }
it { is_expected.to be_falsy }
end
end
end
describe '#ce_port_changelog?' do
where(:helper_ee?, :file_path, :expected) do
true | 'changelogs/unreleased-ee/entry.md' | false
true | 'ee/changelogs/unreleased-ee/entry.md' | false
false | 'changelogs/unreleased-ee/entry.md' | false
false | 'ee/changelogs/unreleased-ee/entry.md' | false
true | 'changelogs/unreleased/entry.md' | true
true | 'ee/changelogs/unreleased/entry.md' | true
false | 'changelogs/unreleased/entry.md' | false
false | 'ee/changelogs/unreleased/entry.md' | false
end
with_them do
let(:ee?) { helper_ee? }
subject { changelog.ce_port_changelog?(file_path) }
it { is_expected.to eq(expected) }
end
end
end
# frozen_string_literal: true
module DangerSpecHelper
def new_fake_danger
Class.new do
attr_reader :git, :gitlab, :helper
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def initialize(git: nil, gitlab: nil, helper: nil)
@git = git
@gitlab = gitlab
@helper = helper
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
end
end
end
......@@ -2,29 +2,22 @@
require 'fast_spec_helper'
require 'rspec-parameterized'
require_relative 'danger_spec_helper'
require 'gitlab/danger/helper'
describe Gitlab::Danger::Helper do
using RSpec::Parameterized::TableSyntax
class FakeDanger
include Gitlab::Danger::Helper
attr_reader :git, :gitlab
def initialize(git:, gitlab:)
@git = git
@gitlab = gitlab
end
end
include DangerSpecHelper
let(:fake_git) { double('fake-git') }
let(:mr_author) { nil }
let(:fake_gitlab) { double('fake-gitlab', mr_author: mr_author) }
subject(:helper) { FakeDanger.new(git: fake_git, gitlab: fake_gitlab) }
let(:fake_danger) { new_fake_danger.include(described_class) }
subject(:helper) { fake_danger.new(git: fake_git, gitlab: fake_gitlab) }
describe '#gitlab_helper' do
context 'when gitlab helper is not available' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment