From 0b9b2077ebf8810375249ce0995f86260c5a9d81 Mon Sep 17 00:00:00 2001 From: Tian L <60599517+tian-lt@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:23:02 +0800 Subject: [PATCH] add a script to check nuget config for PRs (#2042) --- .../templates/build-single-architecture.yaml | 6 ++++ build/scripts/VerifyNugetConfig.ps1 | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 build/scripts/VerifyNugetConfig.ps1 diff --git a/build/pipelines/templates/build-single-architecture.yaml b/build/pipelines/templates/build-single-architecture.yaml index e65bbab2..71379f71 100644 --- a/build/pipelines/templates/build-single-architecture.yaml +++ b/build/pipelines/templates/build-single-architecture.yaml @@ -37,6 +37,12 @@ jobs: - checkout: self fetchDepth: 1 + - ${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + - powershell: | + $(Build.SourcesDirectory)\build\scripts\VerifyNugetConfig.ps1 -FilePath "$(Build.SourcesDirectory)\nuget.config" + displayName: Verify nuget config + failOnStderr: true + - ${{ if eq(parameters.isReleaseBuild, true) }}: - task: UniversalPackages@0 displayName: Download internals package diff --git a/build/scripts/VerifyNugetConfig.ps1 b/build/scripts/VerifyNugetConfig.ps1 new file mode 100644 index 00000000..374611c3 --- /dev/null +++ b/build/scripts/VerifyNugetConfig.ps1 @@ -0,0 +1,28 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.SYNOPSIS + Verify the specified nuget.config. Throw to fail the pipeline if the config is ill-formed. +.PARAMETER FilePath + The path pointing to the nuget.config file to be verified by this script. +.EXAMPLE + VerifyNugetConfig -FilePath .\nuget.config +#> + +param([Parameter(Mandatory)][string]$FilePath) + +$doc = [XML](Get-Content $FilePath) +$sources = (Select-Xml $doc -XPath "//configuration/packageSources").Node +$count = 0 +foreach($src in $sources.ChildNodes){ + if ($src.Name -eq "add"){ + ++$count + } + elseif ($src.Name -eq "clear"){ + $count = 0 + } +} +if ($count -gt 1){ + throw "Adding multiple package sources is not allowed in nuget.config." +}