Passed
Push — main ( cebfe5...3df9c7 )
by Sebastian
04:33
created

IntegrateBeforePush::getRestriction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Notify\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception\ActionFailed;
17
use CaptainHook\App\Hook\Action;
18
use CaptainHook\App\Hook\Constrained;
19
use CaptainHook\App\Hook\Restriction;
20
use CaptainHook\App\Git\Rev\Util as RevUtil;
21
use CaptainHook\App\Hooks;
22
use SebastianFeldmann\Git\Repository;
23
24
/**
25
 * Class IntegrateBeforePush
26
 *
27
 * @package CaptainHook
28
 * @author  Sebastian Feldmann <[email protected]>
29
 * @link    https://github.com/captainhookphp/captainhook
30
 * @since   Class available since Release 5.19.1
31
 */
32
class IntegrateBeforePush implements Action, Constrained
33
{
34
    /**
35
     * Returns a list of applicable hooks
36
     *
37
     * @return \CaptainHook\App\Hook\Restriction
38
     */
39 1
    public static function getRestriction(): Restriction
40
    {
41 1
        return Restriction::fromArray([Hooks::PRE_PUSH]);
42
    }
43
44
    /**
45
     * Executes the action
46
     *
47
     * @param  \CaptainHook\App\Config           $config
48
     * @param  \CaptainHook\App\Console\IO       $io
49
     * @param  \SebastianFeldmann\Git\Repository $repository
50
     * @param  \CaptainHook\App\Config\Action    $action
51
     * @return void
52
     * @throws \Exception
53
     */
54 2
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
55
    {
56 2
        $trigger       = $action->getOptions()->get('trigger', '[merge]');
57 2
        $branchToWatch = $action->getOptions()->get('branch', 'origin/main');
58 2
        $branchInfo    = RevUtil::extractBranchInfo($branchToWatch);
59
60 2
        $repository->getRemoteOperator()->fetchBranch($branchInfo['remote'], $branchInfo['branch']);
61
62 2
        foreach ($repository->getLogOperator()->getCommitsBetween('HEAD', $branchToWatch) as $commit) {
63 2
            $message = $commit->getSubject() . PHP_EOL . $commit->getBody();
64 2
            if (str_contains($message, $trigger)) {
65 1
                throw new ActionFailed('integrate ' . $branchInfo['branch'] . ' before you push!');
66
            }
67
        }
68
    }
69
}
70