Passed
Push — main ( ead171...a2167f )
by Sebastian
03:33
created

InjectIssueKeyFromBranch::extractIssueId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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
declare(strict_types=1);
13
14
namespace CaptainHook\App\Hook\Message\Action;
15
16
use CaptainHook\App\Config;
17
use CaptainHook\App\Config\Options;
18
use CaptainHook\App\Console\IO;
19
use CaptainHook\App\Exception\ActionFailed;
20
use CaptainHook\App\Hook\Action;
21
use CaptainHook\App\Hook\Constrained;
22
use CaptainHook\App\Hook\Restriction;
23
use CaptainHook\App\Hooks;
24
use SebastianFeldmann\Git\CommitMessage;
25
use SebastianFeldmann\Git\Repository;
26
27
/**
28
 * Class PrepareFromFile
29
 *
30
 * Example configuration:
31
 * {
32
 *   "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch",
33
 *   "options": {
34
 *     "regex": "#([A-Z]+\\-[0-9]+)#i",
35
 *     "into": "body",
36
 *     "mode": "append",
37
 *     "prefix": "\nissue: ",
38
 *     "force": true
39
 *   }
40
 * }
41
 *
42
 * The regex option needs group $1 (...) to be the issue key
43
 *
44
 * @package CaptainHook
45
 * @author  Sebastian Feldmann <[email protected]>
46
 * @link    https://github.com/captainhookphp/captainhook
47
 * @since   Class available since Release 5.16.0
48
 */
49
class InjectIssueKeyFromBranch implements Action, Constrained
50
{
51
    /**
52
     * Mode constants
53
     */
54
    private const MODE_APPEND  = 'append';
55
    private const MODE_PREPEND = 'prepend';
56
57
    /**
58
     * Target constants
59
     */
60
    private const TARGET_SUBJECT = 'subject';
61
    private const TARGET_BODY    = 'body';
62
63
    /**
64
     * Returns a list of applicable hooks
65
     *
66
     * @return \CaptainHook\App\Hook\Restriction
67
     */
68 1
    public static function getRestriction(): Restriction
69
    {
70 1
        return Restriction::fromArray([Hooks::PREPARE_COMMIT_MSG]);
71
    }
72
73
    /**
74
     * Execute the configured action
75
     *
76
     * @param  \CaptainHook\App\Config           $config
77
     * @param  \CaptainHook\App\Console\IO       $io
78
     * @param  \SebastianFeldmann\Git\Repository $repository
79
     * @param  \CaptainHook\App\Config\Action    $action
80
     * @return void
81
     * @throws \Exception
82
     */
83 9
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
84
    {
85 9
        $options = $action->getOptions();
86 9
        $branch  = $repository->getInfoOperator()->getCurrentBranch();
87 9
        $pattern = $options->get('regex', '#([A-Z]+\-[0-9]+)#i');
88 9
        $issueID = $this->extractIssueId($branch, $pattern);
89
90
        // did we actually find an issue id?
91 9
        if (empty($issueID)) {
92 2
            if ($options->get('force', false)) {
93 1
                throw new ActionFailed('No issue key found in branch name');
94
            }
95
        }
96
97 8
        $msg = $repository->getCommitMsg();
98
99
        // make sure the issue key is not already in the commit message
100 8
        if (stripos($msg->getSubject() . $msg->getContent(), $issueID) !== false) {
101 2
            return;
102
        }
103
104 6
        $repository->setCommitMsg($this->createNewCommitMessage($options, $msg, $issueID));
105
    }
106
107
    /**
108
     * Extract issue id from branch name
109
     *
110
     * @param  string $branch
111
     * @param  string $pattern
112
     * @return string
113
     */
114 9
    private function extractIssueId(string $branch, string $pattern): string
115
    {
116 9
        $match = [];
117
        // can we actually find an issue id?
118 9
        if (!preg_match($pattern, $branch, $match)) {
119 2
            return '';
120
        }
121 7
        return $match[1] ?? '';
122
    }
123
124
    /**
125
     * Will create the new commit message with the injected issue key
126
     *
127
     * @param  \CaptainHook\App\Config\Options      $options
128
     * @param  \SebastianFeldmann\Git\CommitMessage $msg
129
     * @param  string                               $issueID
130
     * @return \SebastianFeldmann\Git\CommitMessage
131
     */
132 6
    private function createNewCommitMessage(Options $options, CommitMessage $msg, string $issueID): CommitMessage
133
    {
134
        // let's figure out where to put the issueID
135 6
        $target = $options->get('into', self::TARGET_BODY);
136 6
        $mode   = $options->get('mode', self::MODE_APPEND);
137
138
        // overwrite either subject or body
139 6
        $pattern          = $this->handlePrefixAndSuffix($mode, $options);
140 6
        $msgData          = [self::TARGET_SUBJECT => $msg->getSubject(), self::TARGET_BODY => $msg->getBody()];
141 6
        $msgData[$target] = $this->injectIssueId($issueID, $msgData[$target], $mode, $pattern);
142
143
        // combine all the parts to create a new commit message
144 6
        $msgText = $msgData[self::TARGET_SUBJECT] . PHP_EOL
145 6
                 . PHP_EOL
146 6
                 . $msgData[self::TARGET_BODY] . PHP_EOL
147 6
                 . $msg->getComments();
148
149 6
        return new CommitMessage($msgText, $msg->getCommentCharacter());
150
    }
151
152
    /**
153
     * Appends or prepends the issue id to the given message part
154
     *
155
     * @param  string $issueID
156
     * @param  string $msg
157
     * @param  string $mode
158
     * @param  string $pattern
159
     * @return string
160
     */
161 6
    private function injectIssueId(string $issueID, string $msg, string $mode, string $pattern): string
162
    {
163 6
        $issueID = preg_replace_callback(
164 6
            '/\$(\d+)/',
165 6
            function ($matches) use ($issueID) {
166 6
                return $matches[1] === '1' ? $issueID : '';
167 6
            },
168 6
            $pattern
169 6
        );
170
171 6
        return ltrim($mode === self::MODE_PREPEND ? $issueID . $msg : $msg . $issueID);
172
    }
173
174
    /**
175
     * Make sure the prefix and suffix options still works even if they should not be used anymore
176
     *
177
     * @param  string                          $mode
178
     * @param  \CaptainHook\App\Config\Options $options
179
     * @return string
180
     */
181 6
    private function handlePrefixAndSuffix(string $mode, Options $options): string
182
    {
183 6
        $space   = '';
184 6
        $pattern = $options->get('pattern', '');
185 6
        if (empty($pattern)) {
186 5
            $space   = ' ';
187 5
            $pattern = '$1';
188
        }
189
        // depending on the mode use a whitespace as prefix or suffix
190 6
        $prefix = $options->get('prefix', $mode == 'append' ? $space : '');
191 6
        $suffix = $options->get('suffix', $mode == 'prepend' ? $space : '');
192 6
        return $prefix . $pattern . $suffix;
193
    }
194
}
195