git-changelog-lib icon indicating copy to clipboard operation
git-changelog-lib copied to clipboard

Use group 1 for Issue matching

Open AndreasScharfCPB opened this issue 2 years ago • 2 comments
trafficstars

Hi, I am currently trying to parse our issues from the commit messages and that works. But because our format is [ABC-1234] we get [ABC-1234] as a result because the issue matcher returns group() (=group(0)). In regex otherwise we could define non-capturing groups ( "(?:pattern)" ) which would result in this matching pattern NOT returned by the group. The only difference for the default regex would be that it always have to be in one group aka surrounded by brackets. So \b[a-zA-Z]([a-zA-Z]+)-([0-9]+)\b would be (\b[a-zA-Z]([a-zA-Z]+)-([0-9]+)\b) and group(1) for ABC-1234 would return ABC-1234 and in our case we could use (?:\[)(([a-zA-Z](?:[a-zA-Z]+)-(?:[0-9]+)))(?:\]) and group(1) for [ABC-1234] would return ABC-1234

This way there would be more flexibility in the regex. The other, more complicated approach would be to also add a parameter for which group should be returned or return all groups or something but in my understanding group(1) should do the trick for the default and most other cases as you could just filter out everything else.

Code change would be here: https://github.com/tomasbjerre/git-changelog-lib/blob/e37028a81a2988ca1f0a4dde9b9cbf670ed84663/src/main/java/se/bjurr/gitchangelog/internal/issues/IssueParser.java#L72

AndreasScharfCPB avatar Jun 07 '23 09:06 AndreasScharfCPB

Sorry, I dont understand what you want with this issue.

tomasbjerre avatar Jun 09 '23 16:06 tomasbjerre

If you wrap the issue ID in special characters, like we do (e.g. [ABC-1234]). because we have several issue tracking systems with different formats, than \b(regex)\b does not work as the issue ID is not a word but \b[(regex)]\b does would work. But then the brackets are part of the issue id which is wrong and does not work down the road with loading Jira issue titles etc. Just (regex) would also work but than of course it could match some other part of the commit message as well unintentionally. issueMatcher.group() just returns the whole match. issueMatcher.group(1) returns the first group of the matching String. So as I stated above you could define a regex that matches the String [(regex)] but groups only the real issueID and uses non-capturing groups to cut of the parts of the block that are not part of the issue ID.

AndreasScharfCPB avatar Jun 12 '23 07:06 AndreasScharfCPB