ruby - 使用 issue_closing_pattern 变量关闭 gitlab 中的多个问题

标签 ruby regex gitlab

我希望能够通过使用默认模式引用多个问题来通过一次提交关闭多个问题 ^([Cc]loses|[Ff]ixes) +#\d+a。我知道这只会影响行首的fixes #number-patterns,这就是我想要的。
但我还不能让它工作。
我目前正在使用 Gitlab 6.1,根据 github 上的安装自述文件安装它,除了下面的代码片段之外没有做任何更改。
这是我尝试过的:

首先,我在 {gitlab-directory}/app/models/commit.rb 中进行了更改 the following (原代码被注释掉):

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

但此代码片段中使用的正则表达式不符合我的需要并且不是真的正确(例如:(fixs #123)(closees #123) 都可以。
测试此代码片段并确认此代码片段适用于与代码片段中使用的正则表达式相匹配的模式后,我尝试更改正则表达式。起初,我试图在第二行这样做:

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

这个没用。我在 log/unicorn.stderr.log 中没有发现任何错误消息,所以我尝试直接使用配置文件中的默认正则表达式而不使用变量:

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

但是这个也没有用。同样,log/unicorn.stderr.log 中没有错误消息。

如何在此代码段中使用配置文件中的变量 issue_closing_pattern 作为正则表达式模式?

最佳答案

如果您提供给 String#scan 的正则表达式方法包含捕获组,它返回一个数组数组,其中包含每个组匹配的模式:

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"]]

因为默认的正则表达式有一个仅针对“关闭/修复”位的捕获组,这就是循环所看到的所有内容,并且这些字符串不包含问题引用!要修复它,只需在整个模式周围添加一个捕获组:

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"]]

关于ruby - 使用 issue_closing_pattern 变量关闭 gitlab 中的多个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636667/

相关文章:

regex - 使用 top 和 awk 获取 iowait

javascript - JS - 检查 http ://In TextField Input

ruby-on-rails - RuboCop:线路太长 ← 如何忽略?

ruby-on-rails - 无法安装 Rails。错误 : Error installing rails: ERROR: Failed to build gem native extension

ruby-on-rails - 你可以将自己传递给 rails 中的 lambda 吗?

c# - 不匹配带有开头双引号的 YouTube 网址 - C# Regex

docker - 如何从 ci-pipeline 执行位于 runner 中的脚本?

gitlab - 如何禁止同一管道并发执行

git - 是否可以使用 SSH 协议(protocol)在 gitlab.com 上镜像私有(private)存储库?

ruby-on-rails - 测试字段在 mongoid 中键入一个模型