Skip to content
代码片段 群组 项目
未验证 提交 6e3ac145 编辑于 作者: Kasia Misirli's avatar Kasia Misirli 提交者: GitLab
浏览文件

Add Danger message when ci templates are updated

Add specs for ci template check tooling

Fix the formatting and file path with changes.category method

Add check! spec and fix a bug in existing code
上级 8d44d143
No related branches found
No related tags found
无相关合并请求
# 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!
# frozen_string_literal: true
require_relative '../../tooling/danger/ci_templates'
module Danger
class CiTemplates < ::Danger::Plugin
include Tooling::Danger::CiTemplates
end
end
# 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
# 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
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册