Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InjectIssueKeyFromBranch does not work as expected #230

Closed
cegento opened this issue Nov 4, 2023 · 10 comments
Closed

InjectIssueKeyFromBranch does not work as expected #230

cegento opened this issue Nov 4, 2023 · 10 comments
Assignees
Labels

Comments

@cegento
Copy link

cegento commented Nov 4, 2023

Hello

today i tried to implement the action \\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch via our captainhook.json file.
The config looks like this:

            {
                "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch",
                "options": {
                    "regex": "#([A-Z]+\\-[0-9]+)#i",
                    "into": "subject",
                    "mode": "append",
                    "prefix": "Issue ",
                    "force": true
                }
            }

After i run vendor/bin/captainhook install -f i commited something to test this feature out. Sadly the commit message has not changed.

After thinking a little bit whether what might have caused this, i just went into the files and added an echo in \\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch at line 101 with echo $newMsgData['subject'] . PHP_EOL . PHP_EOL . $newMsgData['body'];. The output was correct and the application was able to extract the issuekey.

I dont know why git does not recognize this change or whether there is a bug

@sebastianfeldmann
Copy link
Collaborator

I will have a look and see if I can reproduce the issue.

@sebastianfeldmann
Copy link
Collaborator

Ok I found the issue.
The default commit message templates includes a lot of comment lines.
Within those comment lines the branch name is present.

The action checks if the issue key is already in the prepared commit messages and if so it does nothing.
The problem being that I'm not making a difference between comment lins and actual message text.

In the new version I'll release in a bit (still thinking about some edge cases) this should be fixed.

sebastianfeldmann added a commit that referenced this issue Nov 5, 2023
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
@sebastianfeldmann
Copy link
Collaborator

Fixed and released with version 5.18.3

Thanks for reporting it :)

@cegento
Copy link
Author

cegento commented Nov 5, 2023

Thank you for the quick fix

@cegento
Copy link
Author

cegento commented Nov 5, 2023

I have looked it up again, either the error is now from my side or there are still issues.

For debug: i did this in the InjectIssueKEyFromranch.php file:

echo var_dump($this->createNewCommitMessage($options, $msg, $issueID));

and output was again correct but somehow again githu did not recognize

@sebastianfeldmann
Copy link
Collaborator

Are you committing via an external tool like PHPStorm or Kraken, or on the terminal?

@sebastianfeldmann
Copy link
Collaborator

sebastianfeldmann commented Nov 5, 2023

How it should work.

  • The captain prepare commit message hook is triggered.
  • In case you provide a message via -m or use for example PHPStorm to commit. The message you provided will be used.
  • In case no message is provided git will load a default commit message from a template.
  • A file with your commit message is created .git/COMMIT_EDITMSG
  • The Cap'n reads that file and appends the issue key and writes the new version back to disk
  • Only if you are on the terminal and you did not provide a -m option the .git/COMMIT_EDITMSG file is loaded into your default editor (vi vim nano...)
  • If you close the editor git uses the file to create the final commit message

If you use PHPStorm or the -m option you will never see the added issue number while committing. You should see it using git log though.
If you run git commit in your terminal you should see the added issueID in your editor.

@cegento
Copy link
Author

cegento commented Nov 6, 2023

I tested it via git commit now (without -m) and got exactly the same results as i have done with -m.

  • The echo var dump at line 80 i wrote printed the wanted commit message but github still somehow did not adapt (got git log)

@sebastianfeldmann
Copy link
Collaborator

It works on my machine :/
Here is my prepare-commit-msg config

    "prepare-commit-msg": {
        "enabled": true,
        "actions": [
            {
                "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\InjectIssueKeyFromBranch",
                "options": {
                    "regex": "#([A-Z]+\\-[0-9]+)#i",
                    "into": "body",
                    "mode": "append",
                    "prefix": "\nissue: ",
                    "force": false
                }
            }
        ]
    }

I'm on branch feature/captain-12345

git commit -m"My Commit Message"
pre-commit: 
 - \CaptainHook\App\Hook\PHP\Action\Linting                          : done
 - \CaptainHook\App\Hook\File\Action\DoesNotContainRegex             : done
 - \CaptainHook\App\Hook\File\Action\IsNotEmpty                      : done
 - Max size check                                                    : done
 - tools/phpunit --no-coverage --testsuite UnitTests                 : done
 - tools/phpcs --colors --standard=psr12 {$STAGED_FILES|of-type:p... : skipped
 - \CaptainHook\App\Hook\Composer\Action\CheckLockFile               : done
prepare-commit-msg: 
 - \CaptainHook\App\Hook\Message\Action\InjectIssueKeyFromBranch     : done
 - \CaptainHook\App\Hook\Message\Action\PrepareFromFile              : done
commit-msg: 
 - Verify commit message format                                      : done
 - Fake regex validation                                             : done
 - Store commit message on failure                                   : done
post-commit: 
 - hook is disabled
[feature/captain-12345 038612e] My Commit Message
 1 file changed, 2 insertions(+)

Then my git log looks like this

git log HEAD^..HEAD
commit 038612e1cce66898fd544f0da09667446a4ed900 (HEAD -> feature/captain-12345)
Author: Sebastian Feldmann <sebastianfeldmann@users.noreply.github.com>
Date:   Sat Nov 11 17:33:53 2023 +0100

    My Commit Message
    
    issue: captain-12345

How do you execute the Captain? Locally installed PHP or a Docker container?

@cegento
Copy link
Author

cegento commented Nov 11, 2023

I found the issue, it was actually my fault, i have put it under "commit-msg" instead of "prepare-commit-msg". Somehow the application did not throw any errors + even recognized it as a valid action under "commit-msg" and even composed the correct message :D

@cegento cegento closed this as completed Nov 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants