Skip to content

Commit

Permalink
Fix inject issue key action
Browse files Browse the repository at this point in the history
The action will now re-add the previous defined comment lines as well.
An issue key present in a comment part of the message will no longer
prevent the addition of the found issue key.

issue: #230
  • Loading branch information
sebastianfeldmann committed Nov 5, 2023
1 parent a83b66e commit 89047b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Hook/Message/Action/InjectIssueKeyFromBranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
*
* Example configuration:
* {
* "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch"
* "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch",
* "options": {
* "regex": "#([A-Z]+\\-[0-9]+)#i",
* "into": "body"
* "into": "body",
* "mode": "append",
* "prefix": "\\nissue: ",
* "prefix": "\nissue: ",
* "force": true
* }
* }
Expand Down Expand Up @@ -74,7 +74,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
$msg = $repository->getCommitMsg();

// make sure the issue key is not already in our commit message
if (stripos($msg->getRawContent(), $issueID) !== false) {
if (stripos($msg->getSubject() . $msg->getContent(), $issueID) !== false) {
return;
}
$repository->setCommitMsg($this->createNewCommitMessage($options, $msg, $issueID));
Expand All @@ -99,8 +99,15 @@ private function createNewCommitMessage(Options $options, CommitMessage $msg, st
$newMsgData = ['subject' => $msg->getSubject(), 'body' => $msg->getBody()];
$newMsgData[$target] = $this->injectIssueId($issueID, $newMsgData[$target], $mode, $prefix);

$comments = '';
foreach ($msg->getLines() as $line) {
if (strpos(trim($line), $msg->getCommentCharacter()) === 0) {
$comments .= $line . PHP_EOL;
}
}

return new CommitMessage(
$newMsgData['subject'] . PHP_EOL . PHP_EOL . $newMsgData['body'],
$newMsgData['subject'] . PHP_EOL . PHP_EOL . $newMsgData['body'] . PHP_EOL . $comments,
$msg->getCommentCharacter()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public function testAppendBodyWithPrefix(): void
$hook = new InjectIssueKeyFromBranch();
$hook->execute($config, $io, $repo, $action);

$this->assertEquals('bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345', $repo->getCommitMsg()->getBody());
$this->assertEquals(
'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345' . PHP_EOL,
$repo->getCommitMsg()->getBody()
);
}

/**
Expand Down

0 comments on commit 89047b6

Please sign in to comment.