Commit db2d5289 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '212152-move-convenience-methods-for-labels-into-danger-helper' into 'master'

Move convenience methods for labels into Danger helper

Closes #212152

See merge request gitlab-org/gitlab!29403
parents 420e57ab 86073898
......@@ -11,22 +11,13 @@ MSG
usage_data_changed_files = git.modified_files.grep(%r{usage_data})
def has_label?(label)
gitlab.mr_labels.include?(label)
end
def labels_for_merge_request(labels)
labels_list = labels.map { |label| %Q{~"#{label}"} }.join(' ')
"/label #{labels_list}"
end
if usage_data_changed_files.any?
warn format(TELEMETRY_CHANGED_FILES_MESSAGE)
markdown(USAGE_DATA_FILES_MESSAGE + helper.markdown_list(usage_data_changed_files))
telemetry_labels = ['telemetry']
telemetry_labels << 'telemetry::review pending' unless has_label?('telemetry::reviewed')
telemetry_labels << 'telemetry::review pending' unless helper.mr_has_labels?('telemetry::reviewed')
markdown(labels_for_merge_request(telemetry_labels))
markdown(helper.prepare_labels_for_mr(telemetry_labels))
end
......@@ -191,6 +191,20 @@ module Gitlab
gitlab_helper.mr_json['web_url'].include?('/gitlab-org/security/')
end
def mr_has_labels?(*labels)
return false unless gitlab_helper
labels = labels.flatten.uniq
(labels & gitlab_helper.mr_labels) == labels
end
def prepare_labels_for_mr(labels)
return '' unless labels.any?
labels_list = labels.map { |label| %Q{~"#{label}"} }.join(' ')
"/label #{labels_list}"
end
private
def has_database_scoped_labels?(current_mr_labels)
......
......@@ -363,4 +363,50 @@ describe Gitlab::Danger::Helper do
expect(helper).to be_security_mr
end
end
describe '#mr_has_label?' do
it 'returns false when `gitlab_helper` is unavailable' do
expect(helper).to receive(:gitlab_helper).and_return(nil)
expect(helper.mr_has_labels?('telemetry')).to be_falsey
end
context 'when mr has labels' do
before do
mr_labels = ['telemetry', 'telemetry::reviewed']
expect(fake_gitlab).to receive(:mr_labels).and_return(mr_labels)
end
it 'returns true with a matched label' do
expect(helper.mr_has_labels?('telemetry')).to be_truthy
end
it 'returns false with unmatched label' do
expect(helper.mr_has_labels?('database')).to be_falsey
end
it 'returns true with an array of labels' do
expect(helper.mr_has_labels?(['telemetry', 'telemetry::reviewed'])).to be_truthy
end
it 'returns true with multi arguments with matched labels' do
expect(helper.mr_has_labels?('telemetry', 'telemetry::reviewed')).to be_truthy
end
it 'returns false with multi arguments with unmatched labels' do
expect(helper.mr_has_labels?('telemetry', 'telemetry::non existing')).to be_falsey
end
end
end
describe '#prepare_labels_for_mr' do
it 'composes the labels string' do
mr_labels = ['telemetry', 'telemetry::reviewed']
expect(helper.prepare_labels_for_mr(mr_labels)).to eq('/label ~"telemetry" ~"telemetry::reviewed"')
end
it 'returns empty string for empty array' do
expect(helper.prepare_labels_for_mr([])).to eq('')
end
end
end
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