Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to implement a custom Rubocop rule for checking git branch naming convention?

For example, I need to check the correctness of git branch naming - it should contain ticket ID similarly to this:

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
end

But as I see, Rubocop processes only text nodes and checks only text lines. So, is it possible to define a rule that will be run only once to check one custom check not related to code but only to business logic?

Also I've created discussion here https://github.com/rubocop/rubocop/discussions/10470

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

As you point out, rubocop analyzes code, either as AST nodes, or lines of text / entire files. Therefore, the answer is: it might be possible, but don't do this.

I'd recommend keeping these things separate, e.g. having rake check that runs rubocop and also runs rake check_commits, and the later checks your git commits.

over 4 years ago · Santiago Trujillo Report

0

I've ended up adding a custom script for checking branch naming and adding it to the CI pipeline:

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:

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!