Skip to content
代码片段 群组 项目
提交 34e703d2 编辑于 作者: Kassio Borges's avatar Kassio Borges 提交者: Peter Leitzen
浏览文件

Use Danger to notify about outdated rubocop todos

When removing files, contributors might forget to remove the related
rubocop todos. To avoid that, this new Danger plugin will add comments
on MRs on deleted files that still have related rubocop todos.

Changelog: added
上级 f8776498
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
require_relative '../../tooling/danger/outdated_todo'
module Danger
class Todos < ::Danger::Plugin
def check_outdated_todos(filenames)
Tooling::Danger::OutdatedTodo.new(filenames, context: self).check
end
end
end
# frozen_string_literal: true
todos.check_outdated_todos(git.deleted_files)
---
Cop1:
Exclude:
- 'app/controllers/application_controller.rb'
- 'app/controllers/acme_challenges_controller.rb'
---
Cop2:
Exclude:
- 'app/controllers/application_controller.rb'
# frozen_string_literal: true
require 'fast_spec_helper'
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../tooling/danger/outdated_todo'
RSpec.describe Tooling::Danger::OutdatedTodo, feature_category: :tooling do
let(:fake_danger) { double }
let(:filenames) { ['app/controllers/application_controller.rb'] }
let(:todos) do
[
File.join('spec', 'fixtures', 'tooling', 'danger', 'rubocop_todo', '**', '*.yml')
]
end
subject(:plugin) { described_class.new(filenames, context: fake_danger, todos: todos) }
context 'when the filenames are mentioned in single todo' do
let(:filenames) { ['app/controllers/acme_challenges_controller.rb'] }
it 'warns about mentions' do
expect(fake_danger)
.to receive(:warn)
.with <<~MESSAGE
`app/controllers/acme_challenges_controller.rb` was removed but is mentioned in:
- `spec/fixtures/tooling/danger/rubocop_todo/cop1.yml:5`
MESSAGE
plugin.check
end
end
context 'when the filenames are mentioned in multiple todos' do
let(:filenames) do
[
'app/controllers/application_controller.rb',
'app/controllers/acme_challenges_controller.rb'
]
end
it 'warns about mentions' do
expect(fake_danger)
.to receive(:warn)
.with(<<~FIRSTMESSAGE)
`app/controllers/application_controller.rb` was removed but is mentioned in:
- `spec/fixtures/tooling/danger/rubocop_todo/cop1.yml:4`
- `spec/fixtures/tooling/danger/rubocop_todo/cop2.yml:4`
FIRSTMESSAGE
expect(fake_danger)
.to receive(:warn)
.with(<<~SECONDMESSAGE)
`app/controllers/acme_challenges_controller.rb` was removed but is mentioned in:
- `spec/fixtures/tooling/danger/rubocop_todo/cop1.yml:5`
SECONDMESSAGE
plugin.check
end
end
context 'when the filenames are not mentioned in todos' do
let(:filenames) { ['any/inexisting/file.rb'] }
it 'does not warn' do
expect(fake_danger).not_to receive(:warn)
plugin.check
end
end
context 'when there is no todos' do
let(:filenames) { ['app/controllers/acme_challenges_controller.rb'] }
let(:todos) { [] }
it 'does not warn' do
expect(fake_danger).not_to receive(:warn)
plugin.check
end
end
end
# frozen_string_literal: true
module Tooling
module Danger
class OutdatedTodo
TODOS_GLOBS = %w[
.rubocop_todo/**/*.yml
spec/support/rspec_order_todo.yml
].freeze
def initialize(filenames, context:, todos: TODOS_GLOBS)
@filenames = filenames
@context = context
@todos_globs = todos
end
def check
filenames.each do |filename|
check_filename(filename)
end
end
private
attr_reader :filenames, :context
def check_filename(filename)
mentions = all_mentions_for(filename)
return if mentions.empty?
context.warn <<~MESSAGE
`#{filename}` was removed but is mentioned in:
#{mentions.join("\n")}
MESSAGE
end
def all_mentions_for(filename)
todos
.filter_map { |todo| mentioned_lines(filename, todo) }
.flatten
.map { |todo| "- `#{todo}`" }
end
def mentioned_lines(filename, todo)
File
.foreach(todo)
.with_index(1)
.select { |text, _line| text.match?(/.*#{filename}.*/) }
.map { |_text, line| "#{todo}:#{line}" }
end
def todos
@todos ||= @todos_globs.flat_map { |value| Dir.glob(value) }
end
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册