ruby 如何在GitLab CI/CD上使用Danger?

hjqgdpho  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(185)

GitLab CI/CD已经设置为运行Danger,你必须将其添加到.gitlab-ci.yml文件中:

include:
  - project: 'gitlab-org/quality/pipeline-common'
    file: '/ci/danger-review.yml'

参考号/ci/danger-review.yml

我还通过以下方式覆盖了ruby镜像:

danger-review:
  image: ruby:3.2.2

我的Gemfile引用了danger-gitlab gem:

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.2.2'

# ...

gem 'danger-gitlab', '~> 8.0.0'

你应该可以添加一个Ruby风格的Dangerfile,它应该可以工作:

# frozen_string_literal: true

# ignore inline messages that are outside of the current diff
gitlab.dismiss_out_of_range_messages

message "Author @#{gitlab.mr_author}"

# Make it more obvious that a PR is a work in progress and shouldn't be merged yet
warn('This pull request is a Work in Progress and not ready to merge') if gitlab.mr_title.include? 'WIP'

# Large MR
warn('Big MR! Big changes, big things may happen! Check them.') if git.lines_of_code > 100

# Encourage contributors to write useful descriptions
warn('Please provide a Merge Request description') if gitlab.mr_body.length < 5

# If these are all empty something has gone wrong, better to raise it in a comment
if git.modified_files.empty? && git.added_files.empty? && git.deleted_files.empty?
  fail 'This PR has no changes at all, this is likely a developer issue.'
end

if git.deletions > git.insertions
  message '🗑 Code Cleanup!'
end

但是,在danger-review阶段,它无法解析gitlab引用:

bundler: failed to load command: danger (/usr/local/bundle/bin/danger)
/usr/local/bundle/gems/danger-9.3.0/lib/danger/danger_core/dangerfile.rb:73:in `method_missing':  (Danger::DSLError)
[!] Invalid `Dangerfile` file: undefined local variable or method `gitlab' for #<Danger::Dangerfile:0x00007f2243d7dcc0 ...

有什么想法吗

dtcbnfnu

dtcbnfnu1#

我是根据Orta Therox的建议来做这个的,我回顾了GitLab的repo是如何使用Danger的。
显然,他们的repo使用了自己的gitlab-dangerfiles gem,其中还打包了自己的默认插件和规则。(我相信我也应该能够添加我的自定义规则。)我确实使用它让它工作了。
危险文件:

# frozen_string_literal: true

require 'gitlab-dangerfiles'

Gitlab::Dangerfiles.for_project(self) do |dangerfiles|
  # Imports all plugins, rules and the default reviewer roulette
  dangerfiles.import_defaults
end

Gemfile:

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.2.2'

# ...

gem 'gitlab-dangerfiles', require: false

我还创建了一个具有Developer角色和api作用域的访问令牌。并将令牌添加到新的DANGER_GITLAB_API_TOKEN CI/CD变量。这允许消息在MR上显示为注解。

相关问题