-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathstage-determine-changes.yml
75 lines (65 loc) · 3.14 KB
/
stage-determine-changes.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
jobs:
- job: evaluate_changes
displayName: 'Check for Doc Only Changes'
pool:
vmImage: 'ubuntu-latest'
steps:
- powershell: |
# Determine the context of the build (PR or push) and set the target branch accordingly
$isPR = "$(Build.Reason)" -eq "PullRequest"
# Normalize the target branch name for PR builds or default to 'master' for push builds
$targetBranchName = $isPR ? "$(System.PullRequest.TargetBranch)" -replace 'refs/heads/', '' : "master"
Write-Host "Build context determined: $(if ($isPR) { 'Pull Request targeting ' + $targetBranchName } else { 'Push' })"
# Fetch the target or default base branch and determine the merge base
git fetch origin $targetBranchName
$mergeBase = git merge-base HEAD "origin/$targetBranchName"
Write-Host "Merge base with '$targetBranchName' identified at $mergeBase"
Write-Host "Comparing changes from $mergeBase..."
$gitDiffCommand = "git diff $mergeBase --name-only"
$changedFiles = Invoke-Expression $gitDiffCommand
$docsOnly = $false
$nonDocsOnly = $false
$mixedChanges = $false
$docFiles = 0
$nonDocFiles = 0
if ($changedFiles) {
Write-Host "Changed files:"
Write-Host $changedFiles
} else {
Write-Host "No files have changed."
}
foreach ($file in $changedFiles -split "`n") {
# Identifying changes as documentation if they occur:
# Within any 'doc' folder in the repo (at any level), or
# Are Markdown files at the root level or within subdirectories
$isDoc = $file -match "/doc/" -or # Path contains '/doc/' indicating it's within a doc folder at any level
$file -match "^doc/" -or # Path starts with 'doc/' indicating it's in a root-level doc folder
$file -match "\.md$" # File ends with .md indicating it's a Markdown file
if ($isDoc) {
$docFiles++
} else {
$nonDocFiles++
}
}
Write-Host "Documentation files changed: $docFiles"
Write-Host "Non-documentation files changed: $nonDocFiles"
if ($docFiles -gt 0 -and $nonDocFiles -eq 0) {
$docsOnly = $true
Write-Host "All changes are documentation-only."
} elseif ($docFiles -gt 0 -and $nonDocFiles -gt 0) {
$mixedChanges = $true
Write-Host "Mixed changes detected: Both documentation and non-documentation files have been modified."
} elseif ($nonDocFiles -gt 0) {
$nonDocsOnly = $true
Write-Host "Non-documentation changes detected."
}
# Explicitly write the final values for clarity
Write-Host "Final values:"
Write-Host "docsOnly: $docsOnly"
Write-Host "nonDocsOnly: $nonDocsOnly"
Write-Host "mixedChanges: $mixedChanges"
# Output the results as pipeline variables
Write-Host "##vso[task.setvariable variable=docsOnly;isOutput=true]$docsOnly"
Write-Host "##vso[task.setvariable variable=nonDocsOnly;isOutput=true]$nonDocsOnly"
Write-Host "##vso[task.setvariable variable=mixedChanges;isOutput=true]$mixedChanges"
name: DetermineChanges