Skip to content

Commit ead171f

Browse files
Merge pull request #266 from sebastianlarisch/feature/add-pattern-option
add new pattern option
2 parents 03267e0 + b8dad67 commit ead171f

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

src/Hook/Message/Action/InjectIssueKeyFromBranch.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ private function createNewCommitMessage(Options $options, CommitMessage $msg, st
107107
$target = $options->get('into', 'body');
108108
$mode = $options->get('mode', 'append');
109109
$prefix = $options->get('prefix', ' ');
110+
$pattern = $options->get('pattern', '');
110111

111112
// overwrite either subject or body
112113
$newMsgData = ['subject' => $msg->getSubject(), 'body' => $msg->getBody()];
113-
$newMsgData[$target] = $this->injectIssueId($issueID, $newMsgData[$target], $mode, $prefix);
114+
$newMsgData[$target] = $this->injectIssueId($issueID, $newMsgData[$target], $mode, $prefix, $pattern);
114115

115116
$comments = '';
116117
foreach ($msg->getLines() as $line) {
@@ -132,10 +133,21 @@ private function createNewCommitMessage(Options $options, CommitMessage $msg, st
132133
* @param string $msg
133134
* @param string $mode
134135
* @param string $prefix
136+
* @param string $pattern
135137
* @return string
136138
*/
137-
private function injectIssueId(string $issueID, string $msg, string $mode, string $prefix): string
139+
private function injectIssueId(string $issueID, string $msg, string $mode, string $prefix, string $pattern): string
138140
{
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+
}
150+
139151
return ltrim($mode === 'prepend' ? $prefix . $issueID . ' ' . $msg : $msg . $prefix . $issueID);
140152
}
141153
}

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

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use CaptainHook\App\Exception\ActionFailed;
1818
use CaptainHook\App\Mockery as CHMockery;
1919
use CaptainHook\App\RepoMock;
20-
use SebastianFeldmann\Git\CommitMessage;
2120
use PHPUnit\Framework\TestCase;
21+
use SebastianFeldmann\Git\CommitMessage;
2222

2323
class InjectIssueKeyFromBranchTest extends TestCase
2424
{
@@ -44,7 +44,7 @@ public function testConstraint(): void
4444
public function testPrependSubject(): void
4545
{
4646
$repo = new RepoMock();
47-
$info = $this->createGitInfoOperator('5.0.0', 'freature/ABCD-12345-foo-bar-baz');
47+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
4848

4949
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
5050
$repo->setInfoOperator($info);
@@ -71,7 +71,7 @@ public function testPrependSubject(): void
7171
public function testAppendSubject(): void
7272
{
7373
$repo = new RepoMock();
74-
$info = $this->createGitInfoOperator('5.0.0', 'freature/ABCD-12345-foo-bar-baz');
74+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
7575

7676
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
7777
$repo->setInfoOperator($info);
@@ -98,7 +98,7 @@ public function testAppendSubject(): void
9898
public function testAppendBodyWithPrefix(): void
9999
{
100100
$repo = new RepoMock();
101-
$info = $this->createGitInfoOperator('5.0.0', 'freature/ABCD-12345-foo-bar-baz');
101+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
102102

103103
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
104104
$repo->setInfoOperator($info);
@@ -130,7 +130,7 @@ public function testAppendBodyWithPrefix(): void
130130
public function testAppendBodyWithPrefixWithComments(): void
131131
{
132132
$repo = new RepoMock();
133-
$info = $this->createGitInfoOperator('5.0.0', 'freature/ABCD-12345-foo-bar-baz');
133+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
134134

135135
$repo->setCommitMsg(
136136
new CommitMessage(
@@ -221,7 +221,7 @@ public function testFailIssueKeyNotFound(): void
221221
public function testIssueKeyAlreadyInMSG(): void
222222
{
223223
$repo = new RepoMock();
224-
$info = $this->createGitInfoOperator('5.0.0', 'freature/ABCD-12345-foo-bar-baz');
224+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
225225

226226
$repo->setCommitMsg(new CommitMessage('ABCD-12345 foo' . PHP_EOL . PHP_EOL . 'bar'));
227227
$repo->setInfoOperator($info);
@@ -238,4 +238,60 @@ public function testIssueKeyAlreadyInMSG(): void
238238

239239
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
240240
}
241+
242+
/**
243+
* Tests InjectIssueKeyFromBranch::execute
244+
*
245+
* @throws \Exception
246+
*/
247+
public function testSubjectWithPattern(): void
248+
{
249+
$repo = new RepoMock();
250+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
251+
252+
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
253+
$repo->setInfoOperator($info);
254+
255+
$io = $this->createIOMock();
256+
$config = $this->createConfigMock();
257+
$action = $this->createActionConfigMock();
258+
$action->method('getOptions')->willReturn(new Options([
259+
'into' => 'subject',
260+
'pattern' => '$1:',
261+
'mode' => 'prepend',
262+
]));
263+
264+
$hook = new InjectIssueKeyFromBranch();
265+
$hook->execute($config, $io, $repo, $action);
266+
267+
$this->assertEquals('ABCD-12345: foo', $repo->getCommitMsg()->getSubject());
268+
}
269+
270+
/**
271+
* Tests InjectIssueKeyFromBranch::execute
272+
*
273+
* @throws \Exception
274+
*/
275+
public function testSubjectWithEmptyPattern(): void
276+
{
277+
$repo = new RepoMock();
278+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
279+
280+
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
281+
$repo->setInfoOperator($info);
282+
283+
$io = $this->createIOMock();
284+
$config = $this->createConfigMock();
285+
$action = $this->createActionConfigMock();
286+
$action->method('getOptions')->willReturn(new Options([
287+
'into' => 'subject',
288+
'pattern' => '',
289+
'mode' => 'prepend',
290+
]));
291+
292+
$hook = new InjectIssueKeyFromBranch();
293+
$hook->execute($config, $io, $repo, $action);
294+
295+
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
296+
}
241297
}

0 commit comments

Comments
 (0)