Por ejemplo, necesito verificar la corrección de la denominación de la sucursal de git; debe contener una ID de ticket similar a esta:
module Rails class GitBranchName < RuboCop::Cop::Cop MSG = "Use correct branch name by pattern '{TicketID}-{Description}'. TicketID is mandatory for linking with the task tracker and should be at least 2 digits" def on_send(node = nil) branch = `git rev-parse --abbrev-ref HEAD` return if starts_from_ticket_number?(branch) p "Current branch name: '#{branch}'" # add_offense(node, severity: :warning) end private def starts_from_ticket_number?(name) gitflow_prefixes = [:bug, :bugfix, :feature, :fix, :hotfix, :origin, :release, :wip] name.match?(/(#{gitflow_prefixes.join('/|')})?\d{2,}/) end end endPero como veo, Rubocop procesa solo nodos de texto y verifica solo líneas de texto. Entonces, ¿es posible definir una regla que se ejecutará solo una vez para verificar una verificación personalizada no relacionada con el código sino solo con la lógica comercial?
También he creado una discusión aquí https://github.com/rubocop/rubocop/discussions/10470
Como usted señala, rubocop analiza el código , ya sea como nodos AST o líneas de texto/archivos completos. Por lo tanto, la respuesta es: podría ser posible, pero no hagas esto.
Recomendaría mantener estas cosas separadas, por ejemplo, tener una rake check que ejecute rubocop y también ejecute rake check_commits , y luego verifique sus confirmaciones de git.
Terminé agregando un script personalizado para verificar el nombre de la rama y agregarlo a la canalización de CI:
bin/git_check
#!/usr/bin/env ruby # frozen_string_literal: true # :nocov: class GitBranchNameValidator MSG = "Use correct branch name by pattern '{TicketID}-{Description}'. TicketID is mandatory for linking with the task tracker and should be at least 2 digits" class << self def call branch = `git rev-parse --abbrev-ref HEAD`.split("\n").first return if starts_from_ticket_number?(branch) puts "Current branch name: '#{branch}'" puts MSG exit 1 end private def starts_from_ticket_number?(name) gitflow_prefixes = [:bug, :bugfix, :feature, :fix, :hotfix, :origin, :release, :wip] name.match?(/(#{gitflow_prefixes.join('/|')})?\d{2,}/) end end end GitBranchNameValidator.call # :nocov: