Skip to content

Commit c68c677

Browse files
Add modify option
1 parent 2b247df commit c68c677

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/Hook/Message/Action/InjectIssueKeyFromBranch.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* "mode": "append|prepend",
3939
* "prefix": "",
4040
* "suffix": "",
41+
* "modify": "lowercase|uppercase",
4142
* "force": true
4243
* }
4344
* }
@@ -139,16 +140,18 @@ private function createNewCommitMessage(Options $options, CommitMessage $msg, st
139140
$target = $options->get('into', self::TARGET_BODY);
140141
$mode = $options->get('mode', self::MODE_APPEND);
141142

143+
$issueID = $this->modify($issueID, (string) $options->get('modify', ''));
144+
142145
// overwrite either subject or body
143146
$pattern = $this->handlePrefixAndSuffix($mode, $options);
144147
$msgData = [self::TARGET_SUBJECT => $msg->getSubject(), self::TARGET_BODY => $msg->getBody()];
145148
$msgData[$target] = $this->injectIssueId($issueID, $msgData[$target], $mode, $pattern);
146149

147150
// combine all the parts to create a new commit message
148151
$msgText = $msgData[self::TARGET_SUBJECT] . PHP_EOL
149-
. PHP_EOL
150-
. $msgData[self::TARGET_BODY] . PHP_EOL
151-
. $msg->getComments();
152+
. PHP_EOL
153+
. $msgData[self::TARGET_BODY] . PHP_EOL
154+
. $msg->getComments();
152155

153156
return new CommitMessage($msgText, $msg->getCommentCharacter());
154157
}
@@ -195,4 +198,28 @@ private function handlePrefixAndSuffix(string $mode, Options $options): string
195198
$suffix = $options->get('suffix', $mode == 'prepend' ? $space : '');
196199
return $prefix . $pattern . $suffix;
197200
}
201+
202+
/**
203+
* Run modifier
204+
*
205+
* @param string $issueID
206+
* @param string $modify
207+
* @return string
208+
*/
209+
private function modify(string $issueID, string $modify): string
210+
{
211+
if (empty($modify)) {
212+
return $issueID;
213+
}
214+
215+
$availableModifiers = [
216+
'lowercase' => fn (string $str): string => strtolower($str),
217+
'uppercase' => fn (string $str): string => strtoupper($str),
218+
];
219+
220+
if (array_key_exists($modify, $availableModifiers)) {
221+
$issueID = $availableModifiers[$modify]($issueID);
222+
}
223+
return $issueID;
224+
}
198225
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ public function testPrependSubject(): void
5454
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
5555
}
5656

57+
public function testModify(): void
58+
{
59+
$repo = new RepoMock();
60+
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABC-12345-foo-bar-baz');
61+
62+
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
63+
$repo->setInfoOperator($info);
64+
65+
$io = $this->createIOMock();
66+
$config = $this->createConfigMock();
67+
$action = $this->createActionConfigMock();
68+
$action->method('getOptions')->willReturn(new Options([
69+
'into' => 'subject',
70+
'mode' => 'prepend',
71+
'modify' => 'lowercase'
72+
]));
73+
74+
$hook = new InjectIssueKeyFromBranch();
75+
$hook->execute($config, $io, $repo, $action);
76+
77+
$this->assertEquals('abc-12345 foo', $repo->getCommitMsg()->getSubject());
78+
}
79+
5780
public function testAppendSubject(): void
5881
{
5982
$repo = new RepoMock();

0 commit comments

Comments
 (0)