Skip to content

Add config file class for C# nuget `*.csproj`

Leaminn Ma requested to merge add-c-sharp-nuget-config-file-class into master

What does this MR do and why?

Background

The Repository X-Ray is a tool that searches for dependency manager configuration or lock files and then extracts a list of libraries from the content.

In #474306 (comment 2025085630), we decided to migrate the Repository X-Ray functionality into the GitLab Rails monolith. This gives us two main benefits: (i) it will eventually allow us to run the service outside of the CI pipeline, and (ii) we can maintain the parsing logic centrally so that other domains can utilize it.

Base MR

In !162313 (merged), we introduced the ConfigFiles::Base class where the intention is for each dependency manager config file type to be represented by a child class. The child class contains the parsing logic to extract a list of libraries and their versions from the file content.

This MR

In this MR, we introduce the CsharpNuget config file class that parses *.csproj for the language C#.

For references to the existing parsing logic in the Repository X-Ray repo, see:

Resolves part of #476177.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

  1. Enable the feature flag: Feature.enable(:ai_enable_internal_repository_xray_service)

  2. Create a new blank project and add the following file to the repo:

subdir/MyProj.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
    <PackageReference Include="MessagePack" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Sentry" Version="5.3.1" PrivateAssets="All" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..local/.Date.csproj" />
  </ItemGroup>

  <ItemGroup>
    <None Include="artifacts-icon.png" Pack="true" />
    <None Include="README.md" Pack="true" />
  </ItemGroup>
</Project>
  1. Observe that the X-ray report for the config file is recorded:
project = Project.last # Should be the project you created earlier
project.reload.xray_reports

Screenshot_2024-09-17_at_3.28.51_PM

Related to #476177

Edited by Leaminn Ma

Merge request reports