Jira Integration should consider branch name for card ID

Jira itself looks for the card details in several places, including comments, branch name, and PR title. Octopus however is limited to just parsing the comments.

Having to mention the card in every commit message is redundant, particularly if it’s already part of the branch name.

I am suggesting an improvement to the CommentParser in the integration to also check the Branch in the OctopusBuildInformation. I reached out on Slack and spoke to Mark Harrison, who has passed along this info to the dev team. I figured it would help to post here as well.

The change to the code could be as simple as this, although you may want to rename the CommentParser something like BuildInformationParser.

class CommentParser
{
    // Expression based on example found here https://confluence.atlassian.com/stashkb/integrating-with-custom-jira-issue-key-313460921.html?_ga=2.163394108.1696841245.1556699049-1954949426.1532303954 with modified negative lookbehind
    // with added '$' and '.' to exclude strings that look similar to Jira issues, e.g. `text that may cause confusion: $foo-1 or test.TST-01.com`
    static readonly Regex Expression = new("(?<![A-Z0-9\\$\\.]-?)(?>[A-Z][A-Z0-9]+-\\d+)(?![A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    public string[] ParseWorkItemIds(OctopusBuildInformation buildInformation)
    {            
        var commentResults = buildInformation.Commits.SelectMany(c => WorkItemIds(c.Comment))
            .Where(workItemId => !string.IsNullOrWhiteSpace(workItemId));
            
        var branchResult = WorkItemIds(buildInformation.Branch);
        
        return commentResults.Union(branchResult).ToArray();
    }

    string[] WorkItemIds(string input)
    {
        return Expression.Matches(input)
            .Select(m => m.Groups[0].Value)
            .ToArray();
    }
}

Thanks in advance!

Here is a link to the CommentParser.cs file on GitHub.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.