Add new GitlabDanger class
This class encapsulates our use of the Danger gem.
This commit is contained in:
parent
2cb6393595
commit
45c7c044fe
2 changed files with 130 additions and 0 deletions
54
lib/gitlab_danger.rb
Normal file
54
lib/gitlab_danger.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GitlabDanger
|
||||
LOCAL_RULES ||= %w[
|
||||
changes_size
|
||||
gemfile
|
||||
documentation
|
||||
frozen_string
|
||||
duplicate_yarn_dependencies
|
||||
prettier
|
||||
eslint
|
||||
database
|
||||
].freeze
|
||||
|
||||
CI_ONLY_RULES ||= %w[
|
||||
metadata
|
||||
changelog
|
||||
specs
|
||||
commit_messages
|
||||
roulette
|
||||
single_codebase
|
||||
gitlab_ui_wg
|
||||
ce_ee_vue_templates
|
||||
only_documentation
|
||||
].freeze
|
||||
|
||||
MESSAGE_PREFIX = '==>'.freeze
|
||||
|
||||
attr_reader :gitlab_danger_helper
|
||||
|
||||
def initialize(gitlab_danger_helper)
|
||||
@gitlab_danger_helper = gitlab_danger_helper
|
||||
end
|
||||
|
||||
def self.local_warning_message
|
||||
"#{MESSAGE_PREFIX} Only the following Danger rules can be run locally: #{LOCAL_RULES.join(', ')}"
|
||||
end
|
||||
|
||||
def self.success_message
|
||||
"#{MESSAGE_PREFIX} No Danger rule violations!"
|
||||
end
|
||||
|
||||
def rule_names
|
||||
ci? ? LOCAL_RULES | CI_ONLY_RULES : LOCAL_RULES
|
||||
end
|
||||
|
||||
def html_link(str)
|
||||
self.ci? ? gitlab_danger_helper.html_link(str) : str
|
||||
end
|
||||
|
||||
def ci?
|
||||
!gitlab_danger_helper.nil?
|
||||
end
|
||||
end
|
76
spec/lib/gitlab_danger_spec.rb
Normal file
76
spec/lib/gitlab_danger_spec.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe GitlabDanger do
|
||||
let(:gitlab_danger_helper) { nil }
|
||||
|
||||
subject { described_class.new(gitlab_danger_helper) }
|
||||
|
||||
describe '.local_warning_message' do
|
||||
it 'returns an informational message with rules that can run' do
|
||||
expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changes_size, gemfile, documentation, frozen_string, duplicate_yarn_dependencies, prettier, eslint, database')
|
||||
end
|
||||
end
|
||||
|
||||
describe '.success_message' do
|
||||
it 'returns an informational success message' do
|
||||
expect(described_class.success_message).to eq('==> No Danger rule violations!')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#rule_names' do
|
||||
context 'when running locally' do
|
||||
it 'returns local only rules' do
|
||||
expect(subject.rule_names).to eq(described_class::LOCAL_RULES)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when running under CI' do
|
||||
let(:gitlab_danger_helper) { double('danger_gitlab_helper') }
|
||||
|
||||
it 'returns all rules' do
|
||||
expect(subject.rule_names).to eq(described_class::LOCAL_RULES | described_class::CI_ONLY_RULES)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#html_link' do
|
||||
context 'when running locally' do
|
||||
it 'returns the same string' do
|
||||
str = 'something'
|
||||
|
||||
expect(subject.html_link(str)).to eq(str)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when running under CI' do
|
||||
let(:gitlab_danger_helper) { double('danger_gitlab_helper') }
|
||||
|
||||
it 'returns a HTML link formatted version of the string' do
|
||||
str = 'something'
|
||||
html_formatted_str = %Q{<a href="#{str}">#{str}</a>}
|
||||
|
||||
expect(gitlab_danger_helper).to receive(:html_link).with(str).and_return(html_formatted_str)
|
||||
|
||||
expect(subject.html_link(str)).to eq(html_formatted_str)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#ci?' do
|
||||
context 'when gitlab_danger_helper is not available' do
|
||||
it 'returns false' do
|
||||
expect(subject.ci?).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'when gitlab_danger_helper is available' do
|
||||
let(:gitlab_danger_helper) { double('danger_gitlab_helper') }
|
||||
|
||||
it 'returns true' do
|
||||
expect(subject.ci?).to be_truthy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue