Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

Description

Fixes #3068

The TfsGitRepositoryInfo.CreateFromGit method crashes with IndexOutOfRangeException when processing Git external links that don't contain the expected three-part format (projectId%2frepoId%2fcommitId). The method was accessing array elements without bounds checking.

// Before: crashes on malformed links
string[] bits = Regex.Split(guidbits, "%2f", RegexOptions.IgnoreCase);
repoID = bits[1];  // IndexOutOfRangeException if bits.Length < 2
commitID = bits[2]; // IndexOutOfRangeException if bits.Length < 3

// After: validates before access
if (bits.Length < 3)
{
    Log.Warning("GitRepositoryInfo: Invalid Git external link format...");
    return null;
}
repoID = bits[1];
commitID = bits[2];

Changes:

  • Added bounds validation requiring minimum 3 array elements before accessing bits[1] and bits[2]
  • Returns null with warning log for malformed links instead of throwing
  • Removed unreachable else branch after validation
  • Added unit tests covering valid (3+ parts) and invalid (0-2 parts) link formats

Things to be aware of

  • Logging uses serilog - All Logging should be in the format "My message that contains {item} and {item2}", item, item2! Do not use $"My message that contains {item} and {item2}" to pass text into the log strings as this disables Serilog's ability to pass that data as telemetry to Application Insights and for log highlighting.

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)
  • This change requires a documentation update

How Has This Been Tested?

  • Unit tests added for valid 3-part links
  • Unit tests added for valid links with extended commit IDs (4+ parts)
  • Unit tests added for malformed links (1-2 parts)
  • CodeQL security analysis (0 vulnerabilities)
  • Full solution build verification

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
Original prompt

This section details on the original issue you should resolve

<issue_title>System.IndexOutOfRangeException when cretaing links with CreateFromGit</issue_title>
<issue_description>

Discussed in #3068

Originally posted by ptc-96 November 21, 2025
Hello,

I am performing a migration and I have encountered an error while migrating git links.

{"Timestamp":"2025-11-20T13:14:26.7544653+01:00","Level":"Error","MessageTemplate":"Sy**stem.IndexOutOfRangeException: Index was outside the bounds of the array.** \r\n   
at MigrationTools.Tools.TfsGitRepositoryInfo.CreateFromGit(ExternalLink gitExternalLink, IList 1 possibleRepos) in D:\\a\\azure-devops-migration-tools\\azure-devops-migration-tools\\src\\MigrationTools.Clients.TfsObjectModel\\Tools\\TfsGitRepositoryInfo.cs:line 97\r\n   
at MigrationTools.Tools.TfsGitRepositoryInfo.Create(ExternalLink gitExternalLink, IList 1 possibleRepos, TfsChangeSetMappingTool tfsChangeSetMappingTool, String workItemSourceProjectName) in D:\\a\\azure-devops-migration-tools\\azure-devops-migration-tools\\src\\MigrationTools.Clients.TfsObjectModel\\Tools\\TfsGitRepositoryInfo.cs:line 31\r\n   
at MigrationTools.Tools.TfsGitRepositoryTool.Enrich(TfsProcessor processor, WorkItemData sourceWorkItem, WorkItemData targetWorkItem) in D:\\a\\azure-devops-migration-tools\\azure-devops-migration-tools\\src\\MigrationTools.Clients.TfsObjectModel\\Tools\\TfsGitRepositoryTool.cs:line 109\r\n   
at MigrationTools.Processors.TfsWorkItemMigrationProcessor.ProcessWorkItemLinks(WorkItemData sourceWorkItem, WorkItemData targetWorkItem) in D:\\a\\azure-devops-migration-tools\\azure-devops-migration-tools\\src\\MigrationTools.Clients.TfsObjectModel\\Processors\\TfsWorkItemMigrationProcessor.cs:line 686\r\n   
at MigrationTools.Processors.TfsWorkItemMigrationProcessor.<ProcessWorkItemAsync>d__31.MoveNext() in D:\\a\\azure-devops-migration-tools\\azure-devops-migration-tools\\src\\MigrationTools.Clients.TfsObjectModel\\Processors\\TfsWorkItemMigrationProcessor.cs:line 581",

I've checked that some of the LinkedArtifactUri values of the Git links are not in the expected legacy format:
vstfs:///Git/Commit/_repoId_**%2f**_commitId_
but instead use the newer Azure DevOps format:
vstfs:///Git/Commit/_projectName_/_repoName_/_commitId_

This causes the tool to fail when trying to parse and construct the Git link.
<img width="1502" height="223" alt="image" src="https://github.com/user-attachments/assets/07489056-8b5b-43d2-b005-703ae0204580" />

Suggestion:
If supporting the new URI formats is not possible at this time, would it be feasible to at least catch this exception so the migration process does not stop entirely?

Thanks!

</issue_description>

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


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai
Copy link

coderabbitai bot commented Nov 21, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits November 21, 2025 22:52
- Added validation to check array length before accessing elements
- Returns null with warning log for invalid Git external link formats
- Added comprehensive unit tests for valid and invalid link scenarios

Co-authored-by: MrHinsh <[email protected]>
Copilot AI changed the title [WIP] Fix System.IndexOutOfRangeException in CreateFromGit Fix IndexOutOfRangeException in CreateFromGit when parsing malformed Git external links Nov 21, 2025
Copilot AI requested a review from MrHinsh November 21, 2025 23:00
Copilot finished work on behalf of MrHinsh November 21, 2025 23:00
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.

System.IndexOutOfRangeException when cretaing links with CreateFromGit

2 participants