Before you can use Danger-PHP, you need to provide a ruleset. The default configuration can be generated using:
# Composer global installed or Docker
$ danger init
# Phar
$ php danger.phar initThis command generates the default .danger.php in the current directory.
The .danger.php file returns a Danger\Config object. This object accepts lot of configuration.
The useRule method can be used to add a Rule which should be executed in this Danger.
See builtin rules for all included rules.
This method also accepts a function consider you own changes. You will get the current Danger context as first parameter.
Here is an example:
<?php declare(strict_types=1);
use Danger\Config;
use Danger\Context;
return (new Config())
->useRule(function (Context $context) {
// if !$context->xxxx
$context->failure('Conditions not matched');
})
;To see what the Danger\Context see here
The after is similar to useRule but are intended to be executed as last steps.
One example usage case could be to add labels when Danger is failed.
<?php declare(strict_types=1);
use Danger\Config;
use Danger\Context;
return (new Config())
->after(function (Context $context) {
if ($context->hasFailures()) {
$context->platform->addLabels('Incomplete');
}
})
;With this option you can control that the message should be updated or replaced
<?php declare(strict_types=1);
use Danger\Config;
return (new Config())
->useCommentMode(Config::UPDATE_COMMENT_MODE_REPLACE) // Replace the old comment
->useCommentMode(Config::UPDATE_COMMENT_MODE_UPDATE) // Update the old comment
;Currently only supported on GitLab
This option allows using a thread instead of a comment in the Pull Request.
<?php declare(strict_types=1);
use Danger\Config;
return (new Config())
->useThreadOnFails()
;Currently only supported on Github
With this option enabled Danger uses a Comment Proxy instead using the given Token. This will be necessary as Github Actions doesn't have permission to comment when the Pull Request comes from a fork See Danger-Comment-Proxy as example implementation
<?php declare(strict_types=1);
use Danger\Config;
return (new Config())
->useGithubCommentProxy('https://my-host.com')
;