Skip to content

Commit 0b1edca

Browse files
weshaggardjnlycklama
authored andcommitted
Add Prettier as its own pipeline check (#25718)
- Move Prettier check from unified pipeline to own devops pipeline - Refactor scripts and yml templates for sharing with TS Validation
1 parent 4ebc8b0 commit 0b1edca

File tree

8 files changed

+157
-85
lines changed

8 files changed

+157
-85
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trigger: none
2+
3+
jobs:
4+
- job:
5+
pool:
6+
name: azsdk-pool-mms-ubuntu-2204-general
7+
vmImage: ubuntu-22.04
8+
9+
steps:
10+
- template: /eng/pipelines/templates/steps/npm-install.yml
11+
12+
- pwsh: |
13+
$(Build.SourcesDirectory)/eng/scripts/Swagger-Prettier-Check.ps1 -Verbose
14+
displayName: Swagger Prettier Check
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
steps:
2+
- script: npm --version --loglevel info
3+
displayName: npm --version
4+
5+
- script: npm ci
6+
displayName: npm ci
7+
8+
- script: npm ls -a
9+
displayName: npm ls -a
10+
condition: succeededOrFailed()

eng/pipelines/typespec-validation-all.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,9 @@ jobs:
3636
vmImage: $(OSVmImage)
3737

3838
steps:
39-
- script: npm --version --loglevel info
40-
displayName: npm --version
41-
42-
- script: npm ci
43-
displayName: npm ci
44-
45-
- script: npm ls -a
46-
displayName: npm ls -a
47-
condition: succeededOrFailed()
39+
- template: /eng/pipelines/templates/steps/npm-install.yml
4840

4941
- pwsh: |
50-
$(Build.SourcesDirectory)/eng/scripts/Validate-TypeSpec.ps1 $(Build.SourcesDirectory) -GitClean
42+
$(Build.SourcesDirectory)/eng/scripts/Validate-TypeSpec.ps1 -CheckAll -GitClean -Verbose
5143
displayName: Validate All Specs
5244
condition: succeededOrFailed()
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
trigger: none
22

3-
pr:
4-
branches:
5-
include:
6-
- '*'
7-
83
jobs:
94
- job: Validate_Impacted_Specs
105
pool:
116
name: azsdk-pool-mms-ubuntu-2204-general
127
vmImage: ubuntu-22.04
138

149
steps:
15-
- script: npm --version --loglevel info
16-
displayName: npm --version
17-
18-
- script: npm ci
19-
displayName: npm ci
20-
21-
- script: npm ls -a
22-
displayName: npm ls -a
23-
condition: succeededOrFailed()
10+
- template: /eng/pipelines/templates/steps/npm-install.yml
2411

2512
- pwsh: |
26-
$(Build.SourcesDirectory)/eng/scripts/Validate-TypeSpec.ps1 `
27-
$(Build.SourcesDirectory) `
28-
"origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" `
29-
"${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}" `
30-
-GitClean
13+
$(Build.SourcesDirectory)/eng/scripts/Validate-TypeSpec.ps1 -GitClean -Verbose
3114
displayName: Validate Impacted Specs
3215
condition: succeededOrFailed()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function Get-ChangedFiles($baseCommitish="HEAD^", $targetCommitish="HEAD", $diffFilter="d")
2+
{
3+
# diff-filter=d is to exclude any deleted files as we don't generally
4+
# want to do work against files that no longer exist.
5+
# https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
6+
7+
# core.quotepath=off is to help avoid quoting in the returned paths
8+
# https://git-scm.com/docs/git-config/2.22.0#Documentation/git-config.txt-corequotePath
9+
10+
# Get all the files that have changed between HEAD and the commit before head
11+
# For PR's that last commit is always a merge commit so HEAD^ will get the parent
12+
# commit of the base branch and as such will diff HEAD against HEAD^
13+
$changedFiles = git -c core.quotepath=off diff --name-only --diff-filter=$diffFilter $baseCommitish $targetCommitish
14+
15+
Write-Verbose "Changed files:"
16+
$changedFiles | ForEach-Object { Write-Verbose "$_" }
17+
18+
return $changedFiles
19+
}
20+
21+
function Get-ChangedSwaggerFiles()
22+
{
23+
$changedFiles = Get-ChangedFilesUnderSpecification
24+
25+
$changedSwaggerFiles = $changedFiles | Where-Object {
26+
$_.EndsWith(".json")
27+
}
28+
29+
return $changedSwaggerFiles
30+
}
31+
32+
function Get-ChangedFilesUnderSpecification($changedFiles = (Get-ChangedFiles))
33+
{
34+
$changedFilesUnderSpecification = $changedFiles | Where-Object {
35+
$_.StartsWith("specification")
36+
}
37+
38+
return $changedFilesUnderSpecification
39+
}
40+
41+
function Get-ChangedCoreFiles($changedFiles = (Get-ChangedFiles))
42+
{
43+
$rootFiles = @(
44+
".gitattributes",
45+
".prettierrc.json",
46+
"package-lock.json",
47+
"package.json",
48+
"tsconfig.json"
49+
)
50+
51+
$coreFiles = $changedFiles | Where-Object {
52+
$_.StartsWith("eng/") -or
53+
$_.StartsWith("specification/common-types/") -or
54+
$_ -in $rootFiles
55+
}
56+
57+
return $coreFiles
58+
}
Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,50 @@
11
[CmdletBinding()]
22
param (
3-
[Parameter(Position = 0, Mandatory = $true)]
4-
[string] $SpecsRepoRootDirectory,
5-
[Parameter(Position = 1, Mandatory = $false)]
6-
[string]$TargetBranch,
7-
[Parameter(Position = 2, Mandatory = $false)]
8-
[string]$SourceBranch
3+
[switch]$CheckAll = $false
94
)
5+
Set-StrictMode -Version 3
106

11-
$changedFiles = @()
12-
$allChangedFiles = (Get-ChildItem -path ./specification tspconfig.* -Recurse).Directory.FullName | ForEach-Object {[IO.Path]::GetRelativePath($($pwd.path), $_)}
13-
$allChangedFiles = $allChangedFiles -replace '\\', '/'
7+
. $PSScriptRoot/ChangedFiles-Functions.ps1
148

15-
if ([string]::IsNullOrEmpty($TargetBranch) -or [string]::IsNullOrEmpty($SourceBranch)) {
16-
if ($TargetBranch -or $SourceBranch) {
17-
throw "Please provide both target branch and source branch."
18-
}
19-
$changedFiles = $allChangedFiles
9+
$repoPath = Resolve-Path "$PSScriptRoot/../.."
10+
$checkAllPath = "specification"
11+
12+
if ($CheckAll) {
13+
$changedFiles = $checkAllPath
2014
}
2115
else {
22-
Write-Host "git -c core.quotepath=off -c i18n.logoutputencoding=utf-8 diff --name-only `"$TargetBranch...$SourceBranch`" --"
23-
$changedFiles = git -c core.quotepath=off -c i18n.logoutputencoding=utf-8 diff --name-only `"$TargetBranch...$SourceBranch`" --
24-
$changedFiles = $changedFiles -replace '\\', '/' | Sort-Object
25-
26-
Write-Host "changedFiles:"
27-
foreach ($changedFile in $changedFiles) {
28-
Write-Host " $changedFile"
29-
}
30-
Write-Host
16+
$changedFiles = @(Get-ChangedFiles -diffFilter "")
17+
$coreChangedFiles = Get-ChangedCoreFiles $changedFiles
3118

32-
$engFiles = $changedFiles | Where-Object {if ($_) { $_.StartsWith('eng') }}
33-
34-
$commonTypesFiles = $changedFiles | Where-Object {if ($_) { $_.StartsWith('specification/common-types') }}
35-
36-
$rootFilesImpactingTypeSpec = @(
37-
".gitattributes",
38-
".prettierrc.json",
39-
"package-lock.json",
40-
"package.json",
41-
"tsconfig.json"
42-
)
43-
$repoRootFiles = $changedFiles | Where-Object {$_ -in $rootFilesImpactingTypeSpec}
44-
45-
if (($Env:BUILD_REPOSITORY_NAME -eq 'azure/azure-rest-api-specs') -and ($engFiles -or $commonTypesFiles -or $repoRootFiles)) {
46-
$changedFiles = $allChangedFiles
19+
if ($Env:BUILD_REPOSITORY_NAME -eq 'azure/azure-rest-api-specs' -and $coreChangedFiles) {
20+
Write-Verbose "Found changes to core eng or root files so checking all specs."
21+
$changedFiles = $checkAllPath
4722
}
4823
else {
49-
$changedFiles = $changedFiles | Where-Object {if ($_) { $_.StartsWith('specification') }}
24+
$changedFiles = Get-ChangedFilesUnderSpecification $changedFiles
5025
}
5126
}
5227

5328
$typespecFolders = @()
5429
$skippedTypespecFolders = @()
5530
foreach ($file in $changedFiles) {
56-
if ($file -match 'specification\/[^\/]*\/') {
57-
if (Test-Path $matches[0]) {
58-
$typespecFolder = (Get-ChildItem -path $matches[0] tspconfig.* -Recurse).Directory.FullName | ForEach-Object {if ($_) { [IO.Path]::GetRelativePath($($pwd.path), $_) }}
59-
$typespecFolders += $typespecFolder -replace '\\', '/'
31+
if ($file -match 'specification(\/[^\/]*\/)*') {
32+
$path = "$repoPath/$($matches[0])"
33+
if (Test-Path $path) {
34+
Write-Verbose "Checking for tspconfig files under $path"
35+
$typespecFolder = Get-ChildItem -path $path tspconfig.* -Recurse
36+
if ($typespecFolder) {
37+
$typespecFolders += $typespecFolder.Directory.FullName
38+
}
6039
} else {
61-
$skippedTypespecFolders += $matches[0]
62-
}
40+
$skippedTypespecFolders += $path
41+
}
6342
}
6443
}
65-
6644
foreach ($skippedTypespecFolder in $skippedTypespecFolders | Select-Object -Unique) {
6745
Write-Host "Cannot find directory $skippedTypespecFolder"
6846
}
6947

70-
$typespecFolders = $typespecFolders | Select-Object -Unique | Sort-Object
48+
$typespecFolders = $typespecFolders | ForEach-Object { [IO.Path]::GetRelativePath($repoPath, $_) -replace '\\', '/' } | Sort-Object -Unique
7149

7250
return $typespecFolders
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[CmdletBinding()]
2+
param (
3+
[switch]$CheckAll = $false
4+
)
5+
Set-StrictMode -Version 3
6+
7+
. $PSScriptRoot/ChangedFiles-Functions.ps1
8+
9+
$exitCode = 0
10+
11+
$repoPath = Resolve-Path "$PSScriptRoot/../.."
12+
13+
if ($CheckAll) {
14+
Write-Host "npx --no -- prettier --check $repoPath/specification/**/*.json --log-level warn"
15+
npx --no -- prettier --check $repoPath/specification/**/*.json --log-level warn
16+
if ($LASTEXITCODE) {
17+
Write-Host "> npx prettier --write $repoPath/specification/**/*.json"
18+
$exitCode = 1
19+
}
20+
}
21+
else
22+
{
23+
$filesToCheck = @(Get-ChangedSwaggerFiles)
24+
if (!$filesToCheck) {
25+
Write-Host "No specification files found to check."
26+
}
27+
else {
28+
foreach ($file in $filesToCheck) {
29+
Write-Host "npx --no -- prettier --check $repoPath/$file --log-level warn"
30+
npx --no -- prettier --check $repoPath/$file --log-level warn
31+
if ($LASTEXITCODE) {
32+
Write-Host "> npx prettier --write $repoPath/$file"
33+
$exitCode = 1
34+
}
35+
}
36+
}
37+
}
38+
39+
if ($exitCode) {
40+
Write-Host "Code style issues found in the above file(s), please run prettier to update. For more detailed docs see https://aka.ms/azsdk/specs/prettier."
41+
}
42+
43+
exit $exitCode

eng/scripts/Validate-TypeSpec.ps1

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
[CmdletBinding()]
22
param (
3-
[Parameter(Position = 0, Mandatory = $true)]
4-
[string] $SpecsRepoRootDirectory,
5-
[Parameter(Position = 1, Mandatory = $false)]
6-
[string]$TargetBranch,
7-
[Parameter(Position = 2, Mandatory = $false)]
8-
[string]$SourceBranch,
9-
[Parameter(Mandatory = $false)]
10-
[switch]$GitClean
3+
[switch]$CheckAll = $false,
4+
[switch]$GitClean = $false
115
)
126

137
$exitCode = 0
148

15-
$typespecFolders = &"$PSScriptRoot/Get-TypeSpec-Folders.ps1" "$SpecsRepoRootDirectory" "$TargetBranch" "$SourceBranch"
9+
$typespecFolders = &"$PSScriptRoot/Get-TypeSpec-Folders.ps1" -CheckAll:$CheckAll
1610

1711
Write-Host "typespecFolders:"
1812
foreach ($typespecFolder in $typespecFolders) {

0 commit comments

Comments
 (0)