Skip to content

Commit

Permalink
GitAuto: [FEATURE] Implement JIRA Ticket Number Extraction from Branc…
Browse files Browse the repository at this point in the history
…h Name (#64)

* Update Src/AiCommitMessage/Utilities/JiraTicketExtractor.cs.

* Update Tests/AiCommitMessage.Tests/Utilities/JiraTicketExtractorTests.cs.

* type - add Jira ticket extraction functionality and tests.

* refactor - Improve code comments in BranchNameUtility.cs.

* type - refactor code for improved string case handling.

---------

Co-authored-by: gitauto-ai[bot] <161652217+gitauto-ai[bot]@users.noreply.github.com>
Co-authored-by: gstraccini[bot] <150967461+gstraccini[bot]@users.noreply.github.com>
Co-authored-by: Guilherme Branco Stracini <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent c551ad8 commit 7b245b9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Src/AiCommitMessage/Services/GenerateCommitMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ public string GenerateCommitMessage(GenerateCommitMessageOptions options)
if (provider == GitProvider.GitHub)
{
var issueNumber = BranchNameUtility.ExtractIssueNumber(options.Branch);
if (!string.IsNullOrEmpty(issueNumber))
if (!string.IsNullOrWhiteSpace(issueNumber))
{
text = $"#{issueNumber} {text}";
}
}
else
{
var jiraTicketNumber = BranchNameUtility.ExtractJiraTicket(options.Branch);
if (!string.IsNullOrWhiteSpace(jiraTicketNumber))
{
text = $"[{jiraTicketNumber}] {text}";
}
}

if (!options.Debug)
{
Expand Down
39 changes: 37 additions & 2 deletions Src/AiCommitMessage/Utility/BranchNameUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

namespace AiCommitMessage.Utility;

/// <summary>
/// Class BranchNameUtility.
/// </summary>
public static class BranchNameUtility
{
private const string Pattern = @"(?:issue)?[-/]?(\d+)";
/// <summary>
/// The GitHub issue pattern.
/// </summary>
private const string GitHubIssuePattern = @"(?:issue)?[-/]?(\d+)";

/// <summary>
/// The Jira ticket pattern.
/// </summary>
private const string JiraTicketPattern = @"(?i)([A-Z]+)-?(\d+)";

/// <summary>
/// Extracts the GitHub issue number from a branch name.
Expand All @@ -15,11 +26,35 @@ public static string ExtractIssueNumber(string branchName)
{
var match = Regex.Match(
branchName,
Pattern,
GitHubIssuePattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase,
TimeSpan.FromSeconds(5)
);

return match.Success ? match.Groups[1].Value : string.Empty;
}

/// <summary>
/// Extracts the JIRA ticket number from a given branch name.
/// </summary>
/// <param name="branchName">The branch name to extract the JIRA ticket from.</param>
/// <returns>The extracted JIRA ticket number in uppercase, or an empty string if not found.</returns>
public static string ExtractJiraTicket(string branchName)
{
var match = Regex.Match(
branchName,
JiraTicketPattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase,
TimeSpan.FromSeconds(5)
);

if (!match.Success)
{
return string.Empty;
}

var projectKey = match.Groups[1].Value.ToUpperInvariant();
var issueNumber = match.Groups[2].Value;
return $"{projectKey}-{issueNumber}";
}
}
34 changes: 34 additions & 0 deletions Tests/AiCommitMessage.Tests/Utility/BranchNameUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,38 @@ public void ExtractIssueNumber_ShouldReturnEmptyString(string branchName)
// Assert
result.Should().BeEmpty();
}

[Theory]
[InlineData("feature/XPTO-1234-some-branch-name_with_description", "XPTO-1234")]
[InlineData("feature/XPTO1234-some-branch-name_with_description", "XPTO-1234")]
[InlineData("XPTO1234-some-branch-name_with_description", "XPTO-1234")]
[InlineData("XPTO-1234-some-branch-name_with_description", "XPTO-1234")]
[InlineData("bugfix/XPTO-1234--some-branch-name_with_description", "XPTO-1234")]
[InlineData("hotfix/xpto-1234-some-branch-name", "XPTO-1234")]
[InlineData("release/XPTO1234", "XPTO-1234")]
[InlineData("XPTO-1234", "XPTO-1234")]
[InlineData("xpto1234", "XPTO-1234")]
public void ExtractJiraTicket_ShouldReturnExpectedResult(
string branchName,
string expectedTicket
)
{
// Act
var result = BranchNameUtility.ExtractJiraTicket(branchName);

// Assert
result.Should().NotBeEmpty();
result.Should().Be(expectedTicket);
}

[Theory]
[InlineData("chore/no-ticket-branch")]
public void ExtractJiraTicket_ShouldReturnEmptyString(string branchName)
{
// Act
var result = BranchNameUtility.ExtractJiraTicket(branchName);

// Assert
result.Should().BeEmpty();
}
}

0 comments on commit 7b245b9

Please sign in to comment.