0

I am trying to split Dangerfile into multiple files and call them from the main Dangerfile.

I tried this way, In the parent Dangerfile

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

And in the scripts/large_diff.rb

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

But this gives this error

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'
    ...
    ...
    ...

I know this is because the github object is not avaialble in the child danger file. How can I use variables and functions (the object github functions warn, fail etc) available in Parent Dangerfile also in the child danger file?

PS: I am new to ruby :)

Johnykutty
  • 12,091
  • 13
  • 59
  • 100

1 Answers1

0

What you're trying to do is not so much Ruby-specific but rather a way of how things are done with Danger. One of the ways to split the file into several smaller ones would be to dedicate a folder for these smaller danger files. Here's an example of such a structure

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

Then in the main Dangerfile, you can just reference the other ones like so

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

It doesn't really matter how you name the directories, but Danger will expect to find a file named Dangerfile in that directory. Personally, I prefer this approach, but there are other methods like importing a Dangerfile from a gem or creating a Danger plugin and calling it through danger.import_plugin(...) See the reference for more info - https://danger.systems/reference.html

Andrius
  • 423
  • 4
  • 11