Skip to content

Commit a2167f9

Browse files
Cleanup magic spaces
1 parent 651d49f commit a2167f9

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

src/Hook/Message/Action/InjectIssueKeyFromBranch.php

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848
*/
4949
class InjectIssueKeyFromBranch implements Action, Constrained
5050
{
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+
5163
/**
5264
* Returns a list of applicable hooks
5365
*
@@ -70,29 +82,45 @@ public static function getRestriction(): Restriction
7082
*/
7183
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
7284
{
73-
$branch = $repository->getInfoOperator()->getCurrentBranch();
7485
$options = $action->getOptions();
75-
$match = [];
86+
$branch = $repository->getInfoOperator()->getCurrentBranch();
7687
$pattern = $options->get('regex', '#([A-Z]+\-[0-9]+)#i');
88+
$issueID = $this->extractIssueId($branch, $pattern);
7789

78-
// can we actually find an issue id?
79-
if (!preg_match($pattern, $branch, $match)) {
90+
// did we actually find an issue id?
91+
if (empty($issueID)) {
8092
if ($options->get('force', false)) {
8193
throw new ActionFailed('No issue key found in branch name');
8294
}
83-
return;
8495
}
8596

86-
$issueID = $match[1] ?? '';
87-
$msg = $repository->getCommitMsg();
97+
$msg = $repository->getCommitMsg();
8898

89-
// make sure the issue key is not already in our commit message
99+
// make sure the issue key is not already in the commit message
90100
if (stripos($msg->getSubject() . $msg->getContent(), $issueID) !== false) {
91101
return;
92102
}
103+
93104
$repository->setCommitMsg($this->createNewCommitMessage($options, $msg, $issueID));
94105
}
95106

107+
/**
108+
* Extract issue id from branch name
109+
*
110+
* @param string $branch
111+
* @param string $pattern
112+
* @return string
113+
*/
114+
private function extractIssueId(string $branch, string $pattern): string
115+
{
116+
$match = [];
117+
// can we actually find an issue id?
118+
if (!preg_match($pattern, $branch, $match)) {
119+
return '';
120+
}
121+
return $match[1] ?? '';
122+
}
123+
96124
/**
97125
* Will create the new commit message with the injected issue key
98126
*
@@ -104,26 +132,21 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
104132
private function createNewCommitMessage(Options $options, CommitMessage $msg, string $issueID): CommitMessage
105133
{
106134
// let's figure out where to put the issueID
107-
$target = $options->get('into', 'body');
108-
$mode = $options->get('mode', 'append');
109-
$prefix = $options->get('prefix', ' ');
110-
$pattern = $options->get('pattern', '');
135+
$target = $options->get('into', self::TARGET_BODY);
136+
$mode = $options->get('mode', self::MODE_APPEND);
111137

112138
// overwrite either subject or body
113-
$newMsgData = ['subject' => $msg->getSubject(), 'body' => $msg->getBody()];
114-
$newMsgData[$target] = $this->injectIssueId($issueID, $newMsgData[$target], $mode, $prefix, $pattern);
139+
$pattern = $this->handlePrefixAndSuffix($mode, $options);
140+
$msgData = [self::TARGET_SUBJECT => $msg->getSubject(), self::TARGET_BODY => $msg->getBody()];
141+
$msgData[$target] = $this->injectIssueId($issueID, $msgData[$target], $mode, $pattern);
115142

116-
$comments = '';
117-
foreach ($msg->getLines() as $line) {
118-
if (strpos(trim($line), $msg->getCommentCharacter()) === 0) {
119-
$comments .= $line . PHP_EOL;
120-
}
121-
}
143+
// combine all the parts to create a new commit message
144+
$msgText = $msgData[self::TARGET_SUBJECT] . PHP_EOL
145+
. PHP_EOL
146+
. $msgData[self::TARGET_BODY] . PHP_EOL
147+
. $msg->getComments();
122148

123-
return new CommitMessage(
124-
$newMsgData['subject'] . PHP_EOL . PHP_EOL . $newMsgData['body'] . PHP_EOL . $comments,
125-
$msg->getCommentCharacter()
126-
);
149+
return new CommitMessage($msgText, $msg->getCommentCharacter());
127150
}
128151

129152
/**
@@ -132,22 +155,40 @@ private function createNewCommitMessage(Options $options, CommitMessage $msg, st
132155
* @param string $issueID
133156
* @param string $msg
134157
* @param string $mode
135-
* @param string $prefix
136158
* @param string $pattern
137159
* @return string
138160
*/
139-
private function injectIssueId(string $issueID, string $msg, string $mode, string $prefix, string $pattern): string
161+
private function injectIssueId(string $issueID, string $msg, string $mode, string $pattern): string
140162
{
141-
if (!empty($pattern)) {
142-
$issueID = preg_replace_callback(
143-
'/\$(\d+)/',
144-
function ($matches) use ($issueID) {
145-
return $matches[1] === '1' ? $issueID : '';
146-
},
147-
$pattern
148-
);
149-
}
163+
$issueID = preg_replace_callback(
164+
'/\$(\d+)/',
165+
function ($matches) use ($issueID) {
166+
return $matches[1] === '1' ? $issueID : '';
167+
},
168+
$pattern
169+
);
150170

151-
return ltrim($mode === 'prepend' ? $prefix . $issueID . ' ' . $msg : $msg . $prefix . $issueID);
171+
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+
private function handlePrefixAndSuffix(string $mode, Options $options): string
182+
{
183+
$space = '';
184+
$pattern = $options->get('pattern', '');
185+
if (empty($pattern)) {
186+
$space = ' ';
187+
$pattern = '$1';
188+
}
189+
// depending on the mode use a whitespace as prefix or suffix
190+
$prefix = $options->get('prefix', $mode == 'append' ? $space : '');
191+
$suffix = $options->get('suffix', $mode == 'prepend' ? $space : '');
192+
return $prefix . $pattern . $suffix;
152193
}
153194
}

tests/unit/Hook/Message/Action/InjectIssueKeyFromBranchTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testAppendBodyWithPrefixWithComments(): void
148148
'prefix' => PHP_EOL . PHP_EOL . 'issue: '
149149
]));
150150

151-
$expected = 'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345' . PHP_EOL;
151+
$expected = 'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345';
152152
$hook = new InjectIssueKeyFromBranch();
153153
$hook->execute($config, $io, $repo, $action);
154154

@@ -256,9 +256,9 @@ public function testSubjectWithPattern(): void
256256
$config = $this->createConfigMock();
257257
$action = $this->createActionConfigMock();
258258
$action->method('getOptions')->willReturn(new Options([
259-
'into' => 'subject',
260-
'pattern' => '$1:',
261-
'mode' => 'prepend',
259+
'into' => 'subject',
260+
'pattern' => '$1: ',
261+
'mode' => 'prepend',
262262
]));
263263

264264
$hook = new InjectIssueKeyFromBranch();
@@ -284,9 +284,9 @@ public function testSubjectWithEmptyPattern(): void
284284
$config = $this->createConfigMock();
285285
$action = $this->createActionConfigMock();
286286
$action->method('getOptions')->willReturn(new Options([
287-
'into' => 'subject',
287+
'into' => 'subject',
288288
'pattern' => '',
289-
'mode' => 'prepend',
289+
'mode' => 'prepend',
290290
]));
291291

292292
$hook = new InjectIssueKeyFromBranch();

0 commit comments

Comments
 (0)