diff --git a/.azure-pipelines/generation-templates/capture-openapi.yml b/.azure-pipelines/generation-templates/capture-openapi.yml index 0cd65816a..5343a7730 100644 --- a/.azure-pipelines/generation-templates/capture-openapi.yml +++ b/.azure-pipelines/generation-templates/capture-openapi.yml @@ -241,7 +241,7 @@ jobs: - pwsh: '$(scriptsDirectory)/create-pull-request.ps1' ${{ if eq(parameters.cloud, 'Prod') }}: - condition: eq('${{ parameters.skipOpenAPIPRGeneration }}', false) + condition: and(succeeded(), eq('${{ parameters.skipOpenAPIPRGeneration }}', false), eq(variables.publishChanges, true)) ${{ else }}: condition: and(succeeded(), eq('${{ parameters.skipOpenAPIPRGeneration }}', false), eq(variables.publishChanges, true), eq(variables.publicationHasChanges, true)) displayName: 'Create Pull Request for the generated OpenAPI files for msgraph-metadata' @@ -250,7 +250,8 @@ jobs: GeneratePullRequest: true GhAppId: $(microsoft-graph-devx-bot-appid) GhAppKey: $(microsoft-graph-devx-bot-privatekey) - OverrideSkipCI: false + # OpenAPI PRs have their own enable/skip controls, independent of the SDK manual-run override. + OverrideSkipCI: true RepoName: 'microsoftgraph/msgraph-metadata' ScriptsDirectory: $(scriptsDirectory) # Prod retains its existing title; Fairfax is labelled to distinguish its discovery PR. diff --git a/README.md b/README.md index 45d9024c8..beffaf6d7 100755 --- a/README.md +++ b/README.md @@ -89,10 +89,16 @@ The output C# code files will go in to the `output` directory. Fairfax cleaned CSDL is published under `clean_v10_fairfax_metadata`; OpenAPI descriptions are published under `openapi/fairfax/v1.0` with the service URL `https://graph.microsoft.us/v1.0`. Its pipeline artifacts are `clean_v10_fairfax_metadata` and `clean_v10_fairfax_openapi` (plus one OpenAPI artifact per profile). The OpenAPI stage consumes the current run's cleaned CSDL artifact even when publication is disabled. No Fairfax SDK generation stages are included. -The existing `skipMetadataCaptureAndClean`, `skipOpenApiCaptureAndClean`, `publishChanges`, and `skipOpenAPIPRGeneration` controls also apply to Fairfax. Skipping metadata capture requires previously published Fairfax cleaned CSDL in the metadata repository. Cleaned CSDL is pushed to metadata master; OpenAPI publication uses a separate `publish-open-api-files//Fairfax/v1.0` discovery PR branch. As with Prod, manual runs skip discovery PR creation. `publishChanges: false` produces artifacts without publishing Fairfax changes or creating its PR. +The existing `skipMetadataCaptureAndClean`, `skipOpenApiCaptureAndClean`, `publishChanges`, and `skipOpenAPIPRGeneration` controls also apply to Fairfax. Skipping metadata capture requires previously published Fairfax cleaned CSDL in the metadata repository. Cleaned CSDL is pushed to metadata master; OpenAPI publication uses a separate `publish-open-api-files//Fairfax/v1.0` discovery PR branch. `publishChanges: false` produces artifacts without publishing Fairfax changes or creating its PR. + +`skipOpenAPIPRGeneration` defaults to `false`, enabling OpenAPI discovery PRs into `microsoftgraph/msgraph-metadata` for both manual and scheduled runs, independently of the SDK `overrideSkipCI` parameter. Set it to `true` to disable OpenAPI PR publication. + +For a manual run that raises only OpenAPI PRs, leave `skipOpenAPIPRGeneration: false` and `overrideSkipCI: false` (both defaults). Keep OpenAPI generation enabled (`skipOpenApiCaptureAndClean: false`) and publication enabled (`publishChanges: true`). SDK PRs remain disabled for that manual run, although SDK generation stages may still run and push branches unless excluded using **Stages to run**. Generation and publication must succeed with changes to publish; Fairfax explicitly skips its PR when there are no changes. To also publish SDK PRs on a manual run, set `overrideSkipCI: true`. SDK behavior on scheduled runs is unchanged. Run the focused generation and publication regression checks with `pwsh -File test/scripts/FairfaxPipeline.Tests.ps1`. Hidi is mocked for these checks; publication uses temporary local Git repositories. +Run the manual-run PR gate regression checks with `pwsh -File test/scripts/OpenApiPullRequest.Tests.ps1`. + ## Use Typewriter to test your beta metadata We assume that the metadata you are using is based off the beta metadata file. diff --git a/test/scripts/OpenApiPullRequest.Tests.ps1 b/test/scripts/OpenApiPullRequest.Tests.ps1 new file mode 100644 index 000000000..9851c0a78 --- /dev/null +++ b/test/scripts/OpenApiPullRequest.Tests.ps1 @@ -0,0 +1,105 @@ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. +$ErrorActionPreference = 'Stop' +$repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent +$template = Get-Content (Join-Path $repoRoot '.azure-pipelines/generation-templates/capture-openapi.yml') -Raw +$pipeline = Get-Content (Join-Path $repoRoot '.azure-pipelines/generation-pipeline.yml') -Raw +$scriptPath = Join-Path $repoRoot 'scripts/create-pull-request.ps1' + +$openapiOverride = [regex]::Match($template, '(?m)^\s+OverrideSkipCI: (true)\s*$') +if (!$openapiOverride.Success -or $template.Contains('$(overrideSkipCI)')) { + throw 'The OpenAPI PR task must bypass the shared manual-run gate independently of the SDK override.' +} +if ($pipeline -notmatch '- name: skipOpenAPIPRGeneration\s+default: false\s+type: boolean') { + throw 'The existing OpenAPI skip parameter must default to false.' +} +$skipMapping = 'skipOpenAPIPRGeneration: \$\{\{ parameters.skipOpenAPIPRGeneration\s*\}\}' +if ([regex]::Matches($pipeline, $skipMapping).Count -ne 4) { + throw 'The shared variable and all three OpenAPI stages must use the existing skip parameter directly.' +} +foreach ($file in @('language-generation.yml', 'language-generation-kiota.yml')) { + $sdkTemplate = Get-Content (Join-Path $repoRoot ".azure-pipelines/generation-templates/$file") -Raw + if ($sdkTemplate -notmatch '(?m)^\s+OverrideSkipCI: \$\(overrideSkipCI\)\s*$') { + throw "SDK manual-run control changed in $file." + } +} +$prodPrCondition = "condition: and(succeeded(), eq('`${{ parameters.skipOpenAPIPRGeneration }}', false), eq(variables.publishChanges, true))" +if (!$template.Contains($prodPrCondition)) { + throw 'Prod OpenAPI PR creation must require successful enabled publication.' +} +$fairfaxPrCondition = "condition: and(succeeded(), eq('`${{ parameters.skipOpenAPIPRGeneration }}', false), eq(variables.publishChanges, true), eq(variables.publicationHasChanges, true))" +if (!$template.Contains($fairfaxPrCondition)) { + throw 'Fairfax OpenAPI PR creation must also require published changes.' +} + +$names = @('OverrideSkipCI', 'BUILD_REASON', 'GeneratePullRequest', 'RepoName', 'Version', + 'BaseBranch', 'BranchName', 'ScriptsDirectory', 'GhAppId', 'GhAppKey', 'GITHUB_TOKEN') +$originalEnvironment = @{} +foreach ($name in $names) { + $originalEnvironment[$name] = [Environment]::GetEnvironmentVariable($name) +} +$originalExitCode = $global:LASTEXITCODE +$ghState = @{ Calls = [System.Collections.Generic.List[object]]::new() } +$function:gh = { + $ghState.Calls.Add(@($args)) + $global:LASTEXITCODE = 0 +}.GetNewClosure() + +try { + $env:RepoName = 'microsoftgraph/msgraph-metadata' + $env:ScriptsDirectory = Join-Path $PSScriptRoot 'fixtures' + $env:BaseBranch = 'master' + $env:GhAppId = 'test-app' + $env:GhAppKey = 'test-key' + foreach ($version in @('v1.0', 'beta', 'Fairfax v1.0')) { + $env:Version = $version + $identifier = $version.Replace(' ', '/') + $env:BranchName = "publish-open-api-files/123/$identifier" + foreach ($case in @( + @{ Reason = 'Manual'; Enabled = 'True'; Expected = 1 }, + @{ Reason = 'Schedule'; Enabled = 'True'; Expected = 1 }, + @{ Reason = 'Manual'; Enabled = 'False'; Expected = 0 }, + @{ Reason = 'Schedule'; Enabled = 'False'; Expected = 0 } + )) { + $env:BUILD_REASON = $case.Reason + $env:OverrideSkipCI = $openapiOverride.Groups[1].Value + $env:GeneratePullRequest = $case.Enabled + $ghState.Calls.Clear() + & $scriptPath 6>$null + $creates = @($ghState.Calls | Where-Object { $_[0] -eq 'pr' -and $_[1] -eq 'create' }) + if ($creates.Count -ne $case.Expected) { + throw "Unexpected PR creation for $version, reason=$($case.Reason), enabled=$($case.Enabled)." + } + if ($case.Expected -eq 1) { + $arguments = $creates[0] + if ($arguments[[Array]::IndexOf($arguments, '-R') + 1] -ne $env:RepoName -or + $arguments[[Array]::IndexOf($arguments, '-B') + 1] -ne 'master' -or + $arguments[[Array]::IndexOf($arguments, '-H') + 1] -ne $env:BranchName) { + throw 'OpenAPI PR must target metadata master from the publication branch.' + } + } + } + } + $env:RepoName = 'microsoftgraph/msgraph-sdk-dotnet' + $env:GeneratePullRequest = 'True' + foreach ($case in @( + @{ Reason = 'Manual'; Override = 'False'; Expected = 0 }, + @{ Reason = 'Manual'; Override = 'True'; Expected = 1 }, + @{ Reason = 'Schedule'; Override = 'False'; Expected = 1 } + )) { + $env:BUILD_REASON = $case.Reason + $env:OverrideSkipCI = $case.Override + $ghState.Calls.Clear() + & $scriptPath 6>$null + $creates = @($ghState.Calls | Where-Object { $_[0] -eq 'pr' -and $_[1] -eq 'create' }) + if ($creates.Count -ne $case.Expected) { + throw 'SDK manual/scheduled PR behavior changed.' + } + } + Write-Host 'Passed: default-enabled independent OpenAPI PRs, explicit opt-out, publication guards, and unchanged SDK manual/scheduled behavior.' +} +finally { + foreach ($name in $names) { + [Environment]::SetEnvironmentVariable($name, $originalEnvironment[$name]) + } + $global:LASTEXITCODE = $originalExitCode +} diff --git a/test/scripts/fixtures/Generate-Github-Token.ps1 b/test/scripts/fixtures/Generate-Github-Token.ps1 new file mode 100644 index 000000000..2b889ccd6 --- /dev/null +++ b/test/scripts/fixtures/Generate-Github-Token.ps1 @@ -0,0 +1,4 @@ +param($AppClientId, $AppPrivateKeyContents, $Repository) + +# No network or credentials are used by the PR gate regression tests. +'test-token'