Skip to content

Fix key collision when multiple PRs share the same merge commit SHA#114

Merged
Malcolmnixon merged 2 commits intomainfrom
copilot/fix-key-collision-issue
Mar 23, 2026
Merged

Fix key collision when multiple PRs share the same merge commit SHA#114
Malcolmnixon merged 2 commits intomainfrom
copilot/fix-key-collision-issue

Conversation

Copy link
Contributor

Copilot AI commented Mar 23, 2026

Pull Request

Description

BuildLookupData crashed with ArgumentException: An item with the same key has already been added when two or more pull requests shared the same merge commit SHA — a condition possible with certain GitHub repo configurations (e.g., squash/re-merge workflows).

Root cause: .ToDictionary() on commitHashToPr throws on duplicate keys.

Fix: Use .GroupBy().ToDictionary() to deduplicate, keeping the first PR in collection order per SHA:

// Before — throws on duplicate merge commit SHAs
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);

// After — handles duplicates gracefully
var commitHashToPr = data.PullRequests
    .Where(p => (p.Merged && p.MergeCommitSha != null) || (!p.Merged && p.HeadSha != null))
    .GroupBy(p => p.Merged ? p.MergeCommitSha! : p.HeadSha!)
    .ToDictionary(g => g.Key, g => g.First());

A regression test was added using the exact SHA from the bug report (c85989fd08aee2b768557f6b90011ec325b3bdea).

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code quality improvement

Related Issues

Pre-Submission Checklist

Before submitting this pull request, ensure you have completed the following:

Build and Test

  • Code builds successfully: dotnet build --configuration Release
  • All tests pass: dotnet test --configuration Release
  • Self-validation tests pass:
    dotnet run --project src/DemaConsulting.BuildMark --configuration Release --framework net10.0
    --no-build -- --validate
  • Code produces zero warnings

Code Quality

  • Code formatting is correct: dotnet format --verify-no-changes
  • New code has appropriate XML documentation comments
  • Static analyzer warnings have been addressed

Quality Checks

Please run the following checks before submitting:

  • Spell checker passes: cspell "**/*.{md,cs}"
  • Markdown linter passes: markdownlint "**/*.md"
  • YAML linter passes: yamllint '**/*.{yml,yaml}'

Testing

  • Added unit tests for new functionality
  • Updated existing tests if behavior changed
  • All tests follow the AAA (Arrange, Act, Assert) pattern
  • Test coverage is maintained or improved

Documentation

  • Updated README.md (if applicable)
  • Updated ARCHITECTURE.md (if applicable)
  • Added code examples for new features (if applicable)
  • Updated requirements.yaml (if applicable)

Additional Notes

All 175 tests pass. CodeQL reports 0 alerts.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net10.0/DemaConsulting.BuildMark.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net10.0/DemaConsulting.BuildMark.Tests.deps.json /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net10.0/testhost.dll --port 43301 --endpoint 127.0.0.1:043301 --role client --parentprocessid 4277 --telemetryoptedin false (http block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net9.0/DemaConsulting.BuildMark.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net9.0/DemaConsulting.BuildMark.Tests.deps.json /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net9.0/testhost.dll --port 33733 --endpoint 127.0.0.1:033733 --role client --parentprocessid 4275 --telemetryoptedin false (http block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net8.0/DemaConsulting.BuildMark.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net8.0/DemaConsulting.BuildMark.Tests.deps.json /home/REDACTED/work/BuildMark/BuildMark/test/DemaConsulting.BuildMark.Tests/bin/Release/net8.0/testhost.dll --port 44369 --endpoint 127.0.0.1:044369 --role client --parentprocessid 4276 --telemetryoptedin false (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Key Collision</issue_title>
<issue_description>### Description

When running BuildMark in demaconsulting/TemplateDotNetLibrary#55 the following error occured:

Generating build information...
Error: An item with the same key has already been added. Key: c85989fd08aee2b768557f6b90011ec325b3bdea
Error: Process completed with exit code 1.

Steps to Reproduce

Unknown - possibly caused by repository commits

Expected Behavior

Should build correct build information

Actual Behavior

Tool errors out.

Code Sample

Tool Version

0.4.0

.NET Version

.NET 10

Operating System

Microsoft Windows Server 2025

Additional Context

No response

Checklist

  • I have searched existing issues to ensure this is not a duplicate
  • I have provided a clear description of the problem
  • I have included steps to reproduce the issue</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Fix key collision error in BuildMark Fix key collision when multiple PRs share the same merge commit SHA Mar 23, 2026
Copilot AI requested a review from Malcolmnixon March 23, 2026 20:02
@Malcolmnixon Malcolmnixon marked this pull request as ready for review March 23, 2026 20:03
Copilot AI review requested due to automatic review settings March 23, 2026 20:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash in GitHubRepoConnector lookup construction when multiple pull requests share the same merge commit SHA, which can occur in certain GitHub workflows.

Changes:

  • Deduplicates the commit-SHA-to-PR mapping in BuildLookupData to avoid ToDictionary() duplicate-key exceptions.
  • Adds a regression test covering duplicate merge commit SHA behavior using the reported SHA.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/DemaConsulting.BuildMark/RepoConnectors/GitHubRepoConnector.cs Updates BuildLookupData to safely handle duplicate commit SHA keys when building the PR lookup map.
test/DemaConsulting.BuildMark.Tests/GitHubRepoConnectorTests.cs Adds a regression test ensuring GetBuildInformationAsync does not throw when duplicate merge commit SHAs are present.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Malcolmnixon Malcolmnixon merged commit e72089f into main Mar 23, 2026
19 checks passed
@Malcolmnixon Malcolmnixon deleted the copilot/fix-key-collision-issue branch March 23, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Key Collision

3 participants