|
| 1 | +# eng/ci/scripts/classify-changes.ps1 |
| 2 | + |
| 3 | +# Read from environment variables provided by Azure DevOps |
| 4 | +$buildReason = $env:BUILD_REASON |
| 5 | +$targetBranch = $env:SYSTEM_PULLREQUEST_TARGETBRANCH |
| 6 | +$sourceBranch = $env:SYSTEM_PULLREQUEST_SOURCEBRANCH |
| 7 | + |
| 8 | +Write-Host "BuildReason: $buildReason" |
| 9 | +Write-Host "TargetBranch: $targetBranch" |
| 10 | +Write-Host "SourceBranch: $sourceBranch" |
| 11 | + |
| 12 | +# Default: not docs-only (for non-PR builds, schedules, etc.) |
| 13 | +if ($buildReason -ne "PullRequest") { |
| 14 | + Write-Host "Non-PR build, forcing DocsOnly = false" |
| 15 | + Write-Host "##vso[task.setvariable variable=DocsOnly;isOutput=true]false" |
| 16 | + exit 0 |
| 17 | +} |
| 18 | + |
| 19 | +if (-not $targetBranch) { |
| 20 | + Write-Host "No target branch provided, defaulting DocsOnly = false" |
| 21 | + Write-Host "##vso[task.setvariable variable=DocsOnly;isOutput=true]false" |
| 22 | + exit 0 |
| 23 | +} |
| 24 | + |
| 25 | +# Normalize target branch (AzDO usually gives refs/heads/main) |
| 26 | +$normalizedTarget = $targetBranch |
| 27 | +if ($normalizedTarget.StartsWith("refs/heads/")) { |
| 28 | + $normalizedTarget = $normalizedTarget.Substring("refs/heads/".Length) |
| 29 | +} |
| 30 | +Write-Host "Normalized target branch: $normalizedTarget" |
| 31 | + |
| 32 | +# Fetch that branch |
| 33 | +Write-Host "Fetching target branch: origin/$normalizedTarget" |
| 34 | +git fetch origin $normalizedTarget --depth=1 |
| 35 | + |
| 36 | +# Get changed files between origin/<target> and HEAD |
| 37 | +Write-Host "Computing changed files vs origin/$normalizedTarget..." |
| 38 | +$changedFiles = git diff --name-only "origin/$normalizedTarget" "HEAD" |
| 39 | + |
| 40 | +if (-not $changedFiles -or $changedFiles.Count -eq 0) { |
| 41 | + Write-Host "No changed files detected, treat as non-docs-only" |
| 42 | + Write-Host "##vso[task.setvariable variable=DocsOnly;isOutput=true]false" |
| 43 | + exit 0 |
| 44 | +} |
| 45 | + |
| 46 | +Write-Host "Changed files:" |
| 47 | +$changedFiles | ForEach-Object { Write-Host " - $_" } |
| 48 | + |
| 49 | +$docsOnly = $true |
| 50 | + |
| 51 | +foreach ($file in $changedFiles) { |
| 52 | + $leaf = [System.IO.Path]::GetFileName($file) |
| 53 | + $lowerFile = $file.ToLowerInvariant() |
| 54 | + |
| 55 | + # Docs / meta rules: |
| 56 | + $isDocsPath = $file.StartsWith("docs/") |
| 57 | + $isMarkdown = $lowerFile.EndsWith(".md") |
| 58 | + $isVsCodePath = $file.StartsWith(".vscode/") |
| 59 | + $isGitHubPath = $file.StartsWith(".github/") |
| 60 | + $isLicenseLike = $leaf -in @('LICENSE', 'CODEOWNERS') |
| 61 | + |
| 62 | + $isDocsOrMeta = $isDocsPath -or $isMarkdown -or $isVsCodePath -or $isGitHubPath -or $isLicenseLike |
| 63 | + |
| 64 | + if (-not $isDocsOrMeta) { |
| 65 | + Write-Host "Found non-docs/meta file: $file" |
| 66 | + $docsOnly = $false |
| 67 | + break |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +$docsOnlyString = if ($docsOnly) { "true" } else { "false" } |
| 72 | + |
| 73 | +Write-Host "DocsOnly = $docsOnlyString" |
| 74 | +Write-Host "##vso[task.setvariable variable=DocsOnly;isOutput=true]$docsOnlyString" |
0 commit comments