Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,11 @@ private static LookupData BuildLookupData(GitHubData data)
// Build a mapping from commit SHA to pull request.
// This is used to associate commits with their pull requests for change tracking.
// For merged PRs, use MergeCommitSha; for open PRs, use head SHA.
// Duplicate commit SHAs are handled gracefully by keeping the first PR in collection order per SHA.
var commitHashToPr = data.PullRequests
.Where(p => (p.Merged && p.MergeCommitSha != null) || (!p.Merged && p.HeadSha != null))
.ToDictionary(p => p.Merged ? p.MergeCommitSha! : p.HeadSha!, p => p);
.GroupBy(p => p.Merged ? p.MergeCommitSha! : p.HeadSha!)
.ToDictionary(g => g.Key, g => g.First());

// Build a set of commit SHAs in the current branch.
// This is used for efficient filtering of branch-related tags.
Expand Down
55 changes: 55 additions & 0 deletions test/DemaConsulting.BuildMark.Tests/GitHubRepoConnectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,59 @@ public async Task GitHubRepoConnector_GetBuildInformationAsync_PreReleaseAllPrev
// Should have null baseline since all previous versions are on the same hash
Assert.IsNull(buildInfo.BaselineVersionTag);
}

/// <summary>
/// Test that GetBuildInformationAsync does not throw when two merged pull requests share
/// the same merge commit SHA. This is a regression test for the key collision bug where
/// <c>ToDictionary</c> would throw <see cref="ArgumentException"/> on duplicate keys.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetBuildInformationAsync_WithDuplicateMergeCommitSha_DoesNotThrow()
{
// Arrange - Create mock responses where two merged PRs share the same merge commit SHA.
// The SHA below is the exact key from the bug report (demaconsulting/BuildMark#45).
const string sharedMergeCommitSha = "c85989fd08aee2b768557f6b90011ec325b3bdea";
using var mockHandler = new MockGitHubGraphQLHttpMessageHandler()
.AddCommitsResponse(sharedMergeCommitSha)
.AddReleasesResponse(new MockRelease("v1.0.0", "2024-01-01T00:00:00Z"))
.AddPullRequestsResponse(
new MockPullRequest(
Number: 1,
Title: "First PR with shared merge commit",
Url: "https://github.com/test/repo/pull/1",
Merged: true,
MergeCommitSha: sharedMergeCommitSha,
HeadRefOid: "head-sha-1",
Labels: []),
new MockPullRequest(
Number: 2,
Title: "Second PR with same merge commit SHA",
Url: "https://github.com/test/repo/pull/2",
Merged: true,
MergeCommitSha: sharedMergeCommitSha,
HeadRefOid: "head-sha-2",
Labels: []))
.AddIssuesResponse()
.AddTagsResponse(new MockTag("v1.0.0", sharedMergeCommitSha));

using var mockHttpClient = new HttpClient(mockHandler);
var connector = new MockableGitHubRepoConnector(mockHttpClient);

// Set up mock command responses
connector.SetCommandResponse("git remote get-url origin", "https://github.com/test/repo.git");
connector.SetCommandResponse("git rev-parse --abbrev-ref HEAD", "main");
connector.SetCommandResponse("git rev-parse HEAD", sharedMergeCommitSha);
connector.SetCommandResponse("gh auth token", "test-token");

// Act - This must not throw ArgumentException due to duplicate dictionary key
var buildInfo = await connector.GetBuildInformationAsync(Version.Create("v1.0.0"));

// Assert - Build info should be valid and not null
Assert.IsNotNull(buildInfo);
Assert.AreEqual("1.0.0", buildInfo.CurrentVersionTag.VersionInfo.FullVersion);
Assert.AreEqual(sharedMergeCommitSha, buildInfo.CurrentVersionTag.CommitHash);
Assert.IsNotNull(buildInfo.Changes);
Assert.IsNotNull(buildInfo.Bugs);
Assert.IsNotNull(buildInfo.KnownIssues);
}
}
Loading