ruby 如何将危险文件拆分为多个较小的文件

j2cgzkjk  于 2023-01-30  发布在  Ruby
关注(0)|答案(1)|浏览(169)

我尝试将Dangerfile拆分为多个文件,并从主Dangerfile调用它们。
我尝试过这种方法,在父Dangerfile

puts "Branch  #{github.branch_for_base} #{git.lines_of_code}"
require_relative "scripts/large_diff.rb"
warn 'Some warning'

scripts/large_diff.rb

puts '---->>>>'
puts github.pr_json.inspect
puts '<<<<---->>>>'
puts github.inspect
puts '<<<<----'

但这会产生误差

You used `puts` in your Dangerfile. To print out text to GitHub use `message` instead
Branch  release/abc/xyz 304
---->>>>
bundler: failed to load command: danger (/Users/runner/work/my-proj/my-proj/vendor/bundle/ruby/3.0.0/bin/danger)
/Users/runner/work/my-proj/my-proj/scripts/large_diff.rb:2:in `<top (required)>': \e[31m (Danger::DSLError)
[!] Invalid `Dangerfile` file: undefined local variable or method `github' for main:Object. Updating the Danger gem might fix the issue. Your Danger version: 8.5.0, latest Danger version: 8.6.1
\e[0m
 #  from Dangerfile:3
 #  -------------------------------------------
 #  puts "Branch  #{github.branch_for_base} #{git.lines_of_code}"
 >  require_relative "scripts/large_diff.rb"
 #  warn 'Big PR, split it into smaller ones'
 #  -------------------------------------------
    from Dangerfile:3:in `require_relative'
    from Dangerfile:3:in `eval_file'
    from /Users/runner/work/bitrise-comment/bitrise-comment/vendor/bundle/ruby/3.0.0/gems/danger-8.5.0/lib/danger/danger_core/dangerfile.rb:311:in `eval'
    from /Users/runner/work/bitrise-comment/bitrise-comment/vendor/bundle/ruby/3.0.0/gems/danger-8.5.0/lib/danger/danger_core/dangerfile.rb:311:in `eval_file'
    ...
    ...
    ...

我知道这是因为github对象在子危险文件中不可用。我如何使用父Dangerfile中可用的变量和函数(对象github函数warnfail等)以及子危险文件?
PS:我是Ruby的新手:)

hpxqektj

hpxqektj1#

你所要做的并不是Ruby特有的,而是一种处理Danger的方式。将文件拆分成几个较小的文件的方法之一是为这些较小的危险文件指定一个文件夹。下面是这样一个结构的示例

.
├── Dangerfile
└── checks
    ├── large_diff
    │   └── Dangerfile
    └── other_check
        └── Dangerfile

然后在主Dangerfile中,您可以像这样引用其他文件

danger.import_dangerfile(path: 'checks/large_diff')
danger.import_dangerfile(path: 'checks/other_check')

如何命名目录并不重要,但是Danger会期望在那个目录中找到一个名为Dangerfile的文件。就个人而言,我更喜欢这种方法,但是也有其他方法,比如从gem导入一个Danger文件,或者创建一个Danger插件并通过danger.import_plugin(...)调用它。更多信息请参见参考-https://danger.systems/reference.html

相关问题