Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use issue_closing_pattern variable to close multiple issues in gitlab

Tags:

regex

ruby

gitlab

I'd like to have the ability to close multiple issues with one commit by referencing multiple issues with the default pattern ^([Cc]loses|[Ff]ixes) +#\d+a. I know that this will only affect fixes #number-patterns at the beginning of lines and that's what I want.
But I wasn't yet able to get it to work.
I'm currently using Gitlab 6.1, installed it according to the installation readme on github and didn't change anything other then the codesnippet below.
Here's what I tried:

First I changed in {gitlab-directory}/app/models/commit.rb the following (original code commented out):

def closes_issues project
    md = safe_message.scan(/(?i)((\[)\s*(close|fix)(s|es|d|ed)*\s*#\d+\s*(\])|(\()\s*(close|fix)(s|es|d|ed)*\s*#\d+\s*(\)))/)
    #md = issue_closing_regex.match(safe_message)
    if md
      extractor = Gitlab::ReferenceExtractor.new
      md.each do |n|
       extractor.analyze(n[0])
      end
      extractor.issues_for(project)
      #extractor = Gitlab::ReferenceExtractor.new
      #extractor.analyze(md[0])
      #extractor.issues_for(project)
    else
      []
    end
  end

But the regex used in this code snippet doesn't fit my needs and isn't really correct (e.g.: (fixs #123) and (closees #123) would both work).
After testing this codesnippet and confirming that this one works with patterns that match the regex used in the snippet, I tried to change the regex. At first, I tried to do this in the second line:

md  safe_message.scan(/#{Gitlab.config.gitlab.issue_closing_pattern}/)

This one didn't work. I didn't found any error messages in log/unicorn.stderr.log so I tried to use the default regex from the config file directly without variable:

md safe_message.scan(/^([Cc]loses|[Ff]ixes) +#\d+a/)

But this one didn't work, too. Again, no error messages in log/unicorn.stderr.log.

How do I use the variable issue_closing_pattern from the config file as regex pattern in this code snippet?

like image 399
wullxz Avatar asked Oct 03 '22 14:10

wullxz


1 Answers

If the regex you provide to the String#scan method contains capture groups, it returns an array of arrays containing the patterns matched by each group:

irb(main):014:0> regex = "^([Cc]loses|[Ff]ixes) +#\\d+"
=> "^([Cc]loses|[Ff]ixes) +#\\d+"
irb(main):017:0> safe_message = "foo\ncloses #1\nfixes #2\nbar"
=> "foo\ncloses #1\nfixes #2\nbar"
irb(main):018:0> safe_message.scan(/#{regex}/)
=> [["closes"], ["fixes"]]

Because the default regex has a capture group for just the "closes/fixes" bit, that's all the loop is seeing, and those strings don't contain the issue references! To fix it, just add a capture group around the entire pattern:

irb(main):019:0> regex = "^(([Cc]loses|[Ff]ixes) +#\\d+)"
=> "^(([Cc]loses|[Ff]ixes) +#\\d+)"
irb(main):020:0> safe_message.scan(/#{regex}/)
=> [["closes #1", "closes"], ["fixes #2", "fixes"]]
like image 94
Ash Wilson Avatar answered Oct 20 '22 12:10

Ash Wilson