captainhook-go / captainhook

CaptainHook is a very flexible git hook manager for software developers that makes sharing git hooks with your team a breeze.
http://captainhook.info
MIT License
22 stars 1 forks source link

Potential bug in CaptainHook::Message.InjectIssueKeyFromBranch #1

Closed tommy-muehle closed 11 months ago

tommy-muehle commented 11 months ago

Hi @sebastianfeldmann 👋

not seen or heard for a long time! But I hope you're doing well. 🙂 I'm currently playing around with Captain Hook for Go. Therefore I added the following configuration to my repository:

"prepare-commit-msg": {
  "actions": [
     {
    "run": "CaptainHook::Message.InjectIssueKeyFromBranch",
    "options": {
      "regex": "[A-Z]+-[0-9]+",
          "into": "subject",
      "force": true
    }
     }
  ]
}

My branch name is just PRODUCT-2453 and it's catched successfully!

But I get a panic panic: runtime error: index out of range [1] with length 1.

If I looked correct and it's not too late it's because you're looking into match[1] instead of match[0] here.

Proof of concept:

package main

func main() {
  log.Println(regexp.MustCompile("[A-Z]+-[0-9]+").FindStringSubmatch("PRODUCT-2053")[0])
}

Best regards, Tommy

sebastianfeldmann commented 11 months ago

Yes, I have to make sure there is an index one. I will patch this and will give a helpful warning. The regex should contain a group like this "([A-Z]+-[0-9]+)" then it should work.

I will go and fix the panic ;)

tommy-muehle commented 11 months ago

@sebastianfeldmann Can confirm, a group also fixes the issue!

sebastianfeldmann commented 11 months ago

Just released 1.0.2 it checks the match slice before using match[1] If it is too short you get a message that you should put your ISSUE-KEY in a regex group.

Thanks for the help :)