diff --git a/danger/ci_templates/Dangerfile b/danger/ci_templates/Dangerfile index bc8bba388c6e0a3f816a2b03be74cc8f99c0cd9f..04120e3270db44c5c5387fad34efafa4eefbb000 100644 --- a/danger/ci_templates/Dangerfile +++ b/danger/ci_templates/Dangerfile @@ -1,31 +1,3 @@ # frozen_string_literal: true -CI_CD_TEMPLATE_MESSAGE = <<~MSG -This merge request requires a CI/CD Template review. To make sure these -changes are reviewed, take the following steps: - -1. Ensure the merge request has the ~"ci::templates" label. - If the merge request modifies CI/CD Template files, Danger will do this for you. -1. Prepare your MR for a CI/CD Template review according to the - [template development guide](https://docs.gitlab.com/ee/development/cicd/templates.html). -1. Assign and `@` mention the CI/CD Template reviewer suggested by Reviewer Roulette. -MSG - -CI_CD_TEMPLATE_FILES_MESSAGE = <<~MSG -The following files require a review from the CI/CD Templates maintainers: -MSG - -return unless helper.ci? - -template_paths_to_review = helper.changes_by_category[:ci_template] - -if helper.mr_labels.include?('ci::templates') || template_paths_to_review.any? - message('This merge request adds or changes files that require a ' \ - 'review from the CI/CD Templates maintainers.') - - markdown(CI_CD_TEMPLATE_MESSAGE) - - if template_paths_to_review.any? - markdown(CI_CD_TEMPLATE_FILES_MESSAGE + helper.markdown_list(template_paths_to_review)) - end -end +ci_templates.check! diff --git a/danger/plugins/ci_templates.rb b/danger/plugins/ci_templates.rb new file mode 100644 index 0000000000000000000000000000000000000000..0ad457693b45ef64e1051dad65cf50b507d29716 --- /dev/null +++ b/danger/plugins/ci_templates.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require_relative '../../tooling/danger/ci_templates' + +module Danger + class CiTemplates < ::Danger::Plugin + include Tooling::Danger::CiTemplates + end +end diff --git a/spec/tooling/danger/ci_templates_spec.rb b/spec/tooling/danger/ci_templates_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..b37e7754597545de8f3696e84575aca535595fec --- /dev/null +++ b/spec/tooling/danger/ci_templates_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'gitlab/dangerfiles/spec_helper' +require 'fast_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../danger/plugins/ci_templates' + +RSpec.describe Tooling::Danger::CiTemplates, feature_category: :tooling do + include_context 'with dangerfile' + subject(:ci_templates) { fake_danger.new(helper: fake_helper) } + + let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } + + before do + allow(fake_helper).to receive(:ci?).and_return(ci_env) + end + + describe '#check!' do + context 'when not in ci environment' do + let(:ci_env) { false } + + it 'does not add the warnings' do + expect(ci_templates).not_to receive(:message) + expect(ci_templates).not_to receive(:markdown) + expect(ci_templates).not_to receive(:warn) + + ci_templates.check! + end + end + + context 'when in ci environment' do + let(:ci_env) { true } + let(:modified_files) { %w[lib/gitlab/ci/templates/ci_template.rb] } + let(:fake_changes) { instance_double(Gitlab::Dangerfiles::Changes, files: modified_files) } + + context 'when there are updates to the ci templates' do + before do + allow(fake_changes).to receive(:by_category).with(:ci_template).and_return(fake_changes) + allow(fake_helper).to receive(:changes).and_return(fake_changes) + allow(ci_templates).to receive(:message) + allow(ci_templates).to receive(:markdown) + allow(fake_helper).to receive(:markdown_list).and_return(modified_files) + end + + it 'adds the danger message, markdown and warning' do + expect(ci_templates).to receive(:message) + expect(ci_templates).to receive(:markdown) + expect(ci_templates).to receive(:warn) + + ci_templates.check! + end + + context 'when there are files returned by markdown_list' do + it 'returns markdown message' do + expect(fake_helper).to receive(:markdown_list).with(modified_files) + expect(ci_templates).to receive(:markdown).with(/#{modified_files}/) + + ci_templates.check! + end + end + end + + context 'when there are no updated ci templates' do + it 'does not add the danger message, markdown and warning' do + expect(ci_templates).not_to receive(:message) + expect(ci_templates).not_to receive(:markdown) + expect(ci_templates).not_to receive(:warn) + + ci_templates.check! + end + end + end + end +end diff --git a/tooling/danger/ci_templates.rb b/tooling/danger/ci_templates.rb new file mode 100644 index 0000000000000000000000000000000000000000..f682dd37d89283ccc4b89531ebd9808c811f1758 --- /dev/null +++ b/tooling/danger/ci_templates.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Tooling + module Danger + module CiTemplates + CI_CD_TEMPLATE_MESSAGE = <<~MSG + This merge request requires a CI/CD Template review. To make sure these + changes are reviewed, take the following steps: + + 1. Ensure the merge request has the ~"ci::templates" label. + If the merge request modifies CI/CD Template files, Danger will do this for you. + 1. Prepare your MR for a CI/CD Template review according to the + [template development guide](https://docs.gitlab.com/ee/development/cicd/templates.html). + 1. Assign and `@` mention the CI/CD Template reviewer suggested by Reviewer Roulette. + MSG + + CI_CD_TEMPLATE_MODIFIED_WARNING = <<~MSG + This merge request adds or changes templates, please consider updating the corresponding + [Gitlab Component](https://gitlab.com/components). + MSG + + def check! + return unless helper.ci? + + return unless helper.mr_labels.include?('ci::templates') || changes.any? + + message('This merge request adds or changes files that require a ' \ + 'review from the CI/CD Templates maintainers.') + + markdown(CI_CD_TEMPLATE_MESSAGE) + + return unless changes.any? + + markdown(<<~MSG + The following files require a review from the CI/CD Templates maintainers: + #{helper.markdown_list(changes)} + MSG + ) + warn(CI_CD_TEMPLATE_MODIFIED_WARNING) + end + + private + + def changes + helper.changes.by_category(:ci_template).files + end + end + end +end