Skip to content

Commit 8b95c87

Browse files
azure-sdksima-zhu
andauthored
Sync eng/common directory with azure-sdk-tools for PR 2861 (Azure#23507)
* Use common script for git diff changes * remove extra files * suppress spell check * suppress false positive cspelling * Address comments * make changes to cspell * Suppress to right values * More fix Co-authored-by: sima-zhu <[email protected]>
1 parent a6ffcbb commit 8b95c87

File tree

9 files changed

+110
-103
lines changed

9 files changed

+110
-103
lines changed

eng/common/pipelines/templates/steps/credscan.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# cSpell:ignore changedfiles
2+
# cSpell:ignore credscan
3+
# cSpell:ignore securedevelopmentteam
4+
# cSpell:ignore postanalysis
15
parameters:
26
SuppressionFilePath: 'eng/CredScanSuppression.json'
37
BaselineFilePath: ''
@@ -7,12 +11,7 @@ parameters:
711
steps:
812
- pwsh: |
913
if ("$(Build.Reason)" -eq 'PullRequest') {
10-
$targetBranch = "origin/$(System.PullRequest.TargetBranch)" -replace "refs/heads/"
11-
12-
# Add config to disable the quote and encoding on file name.
13-
# Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-quoted-file-names
14-
# Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-commit-message-transcoding
15-
$changedFiles = git -c core.quotepath=off -c i18n.logoutputencoding=utf-8 diff $targetBranch HEAD --name-only --diff-filter=d
14+
$changedFiles = & "eng/common/scripts/get-changedfiles.ps1"
1615
$changedFiles | ForEach-Object { Add-Content -Path "${{ parameters.SourceDirectory }}/credscan.tsv" -Value "${{ parameters.SourceDirectory }}/$_"}
1716
}
1817
else {

eng/common/pipelines/templates/steps/eng-common-workflow-enforcer.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
# cSpell:ignore changedfiles
2+
# cSpell:ignore Committish
3+
# cSpell:ignore LASTEXITCODE
4+
15
steps:
6+
- template: /eng/common/pipelines/templates/steps/set-default-branch.yml
27
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
38
- pwsh: |
4-
Set-PsDebug -Trace 1
5-
# Find the default branch of the repo
6-
$defaultBranch = (git remote show origin | Out-String) -replace "(?ms).*HEAD branch: (\w+).*", '$1'
9+
# Find the default branch of the repo. The variable value sets in build step.
10+
Write-Host "Default Branch: $(DefaultBranch)"
711
$targetBranch = "$(System.PullRequest.TargetBranch)" -replace "refs/heads/"
812
9-
Write-Host "Default Branch: ${defaultBranch}"
10-
11-
if ((!"$(System.PullRequest.SourceBranch)".StartsWith("sync-eng/common")) -and $targetBranch -eq $defaultBranch)
13+
if ((!"$(System.PullRequest.SourceBranch)".StartsWith("sync-eng/common")) -and $targetBranch -eq "$(DefaultBranch)")
1214
{
13-
14-
$filesInCommonDir = git diff "origin/${targetBranch}" HEAD --name-only -- 'eng/common/*'
15-
15+
$filesInCommonDir = & "eng/common/scripts/get-changedfiles.ps1" -TargetCommittish $targetBranch -DiffPath 'eng/common/*'
1616
if (($LASTEXITCODE -eq 0) -and ($filesInCommonDir.Count -gt 0))
1717
{
1818
Write-Host "##vso[task.LogIssue type=error;]Changes to files under 'eng/common' directory should not be made in this Repo`n${filesInCommonDir}"

eng/common/scripts/Detect-Api-Changes.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# cSpell:ignore PULLREQUEST
2+
# cSpell:ignore TARGETBRANCH
13
[CmdletBinding()]
24
Param (
35
[Parameter(Mandatory=$True)]
@@ -16,6 +18,8 @@ Param (
1618
[string] $TargetBranch = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/")
1719
)
1820

21+
. (Join-Path $PSScriptRoot common.ps1)
22+
1923
# Submit API review request and return status whether current revision is approved or pending or failed to create review
2024
function Submit-Request($filePath, $packageName)
2125
{
@@ -61,8 +65,7 @@ function Should-Process-Package($pkgPath, $packageName)
6165
# Get package info from json file created before updating version to daily dev
6266
$pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json
6367
$packagePath = $pkgInfo.DirectoryPath
64-
$modifiedFiles = git diff --name-only --relative $TargetBranch HEAD
65-
$modifiedFiles = $modifiedFiles.Where({$_.startswith($packagePath)})
68+
$modifiedFiles = Get-ChangedFiles -DiffPath "$packagePath/*" -DiffFilterType ''
6669
$filteredFileCount = $modifiedFiles.Count
6770
Write-Host "Number of modified files for package: $filteredFileCount"
6871
return ($filteredFileCount -gt 0 -and $pkgInfo.IsNewSdk)
@@ -80,7 +83,6 @@ function Log-Input-Params()
8083
Write-Host "Package Name: $($PackageName)"
8184
}
8285

83-
. (Join-Path $PSScriptRoot common.ps1)
8486
Log-Input-Params
8587

8688
if (!($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn")))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# cSpell:ignore Committish
2+
# cSpell:ignore PULLREQUEST
3+
# cSpell:ignore TARGETBRANCH
4+
# cSpell:ignore SOURCECOMMITID
5+
function Get-ChangedFiles {
6+
param (
7+
[string]$SourceCommittish= "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}",
8+
[string]$TargetCommittish = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/"),
9+
[string]$DiffPath,
10+
[string]$DiffFilterType = "d"
11+
)
12+
# If ${env:SYSTEM_PULLREQUEST_TARGETBRANCH} is empty, then return empty.
13+
if ($TargetCommittish -eq "origin/") {
14+
Write-Host "There is no target branch passed in. "
15+
return ""
16+
}
17+
18+
# Add config to disable the quote and encoding on file name.
19+
# Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-quoted-file-names
20+
# Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-commit-message-transcoding
21+
# Git PR diff: https://docs.github.com/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-comparing-branches-in-pull-requests#three-dot-and-two-dot-git-diff-comparisons
22+
$command = "git -c core.quotepath=off -c i18n.logoutputencoding=utf-8 diff `"$TargetCommittish...$SourceCommittish`" --name-only --diff-filter=$DiffFilterType"
23+
if ($DiffPath) {
24+
$command = $command + " -- `'$DiffPath`'"
25+
}
26+
Write-Host $command
27+
$changedFiles = Invoke-Expression -Command $command
28+
if(!$changedFiles) {
29+
Write-Host "No changed files in git diff between $TargetCommittish and $SourceCommittish"
30+
}
31+
else {
32+
Write-Host "Here are the diff files:"
33+
foreach ($file in $changedFiles) {
34+
Write-Host " $file"
35+
}
36+
}
37+
return $changedFiles
38+
}

eng/common/scripts/check-spelling-in-changed-files.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# cSpell:ignore LASTEXITCODE
2+
# cSpell:ignore errrrrorrrrr
3+
# cSpell:ignore sepleing
14
<#
25
.SYNOPSIS
36
Uses cspell (from NPM) to check spelling of recently changed files
@@ -286,13 +289,7 @@ if (!(Test-Path $CspellConfigPath)) {
286289
# Lists names of files that were in some way changed between the
287290
# current $SourceBranch and $TargetBranch. Excludes files that were deleted to
288291
# prevent errors in Resolve-Path
289-
Write-Host "git diff --diff-filter=d --name-only --relative $TargetBranch $SourceBranch"
290-
$changedFilesList = git diff `
291-
--diff-filter=d `
292-
--name-only `
293-
--relative `
294-
$TargetBranch `
295-
$SourceBranch
292+
$changedFilesList = Get-ChangedFiles
296293

297294
$changedFiles = @()
298295
foreach ($file in $changedFilesList) {

eng/common/scripts/common.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# cSpell:ignore Apireview
2+
# cSpell:ignore Onboarded
13
$RepoRoot = Resolve-Path "${PSScriptRoot}..\..\..\.."
24
$EngDir = Join-Path $RepoRoot "eng"
35
$EngCommonDir = Join-Path $EngDir "common"
@@ -12,6 +14,7 @@ $EngScriptsDir = Join-Path $EngDir "scripts"
1214
. (Join-Path $EngCommonScriptsDir Invoke-GitHubAPI.ps1)
1315
. (Join-Path $EngCommonScriptsDir Invoke-DevOpsAPI.ps1)
1416
. (Join-Path $EngCommonScriptsDir artifact-metadata-parsing.ps1)
17+
. (Join-Path $EngCommonScriptsDir "Helpers" git-helpers.ps1)
1518

1619
# Setting expected from common languages settings
1720
$Language = "Unknown"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# cSpell:ignore Committish
3+
# cSpell:ignore committish
4+
# cSpell:ignore PULLREQUEST
5+
# cSpell:ignore TARGETBRANCH
6+
# cSpell:ignore SOURCECOMMITID
7+
# cSpell:ignore elete
8+
# cSpell:ignore ename
9+
<#
10+
.SYNOPSIS
11+
Returns git diff changes in pull request.
12+
.DESCRIPTION
13+
The script is to return diff changes in pull request.
14+
.PARAMETER SourceCommittish
15+
The branch committish PR merges from.
16+
Definition of committish: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefcommit-ishacommit-ishalsocommittish
17+
.PARAMETER TargetCommittish
18+
The branch committish PR targets to merge into.
19+
.PARAMETER DiffPath
20+
The files which git diff to scan against. Support regex match. E.g. "eng/common/*", "*.md"
21+
.PARAMETER DiffFilterType
22+
The filter type A(a)dd, D(d)elete, R(r)ename, U(u)pate.
23+
E.g. 'ad' means filter out the newly added file and deleted file
24+
E.g. '' means no filter on file mode.
25+
#>
26+
[CmdletBinding()]
27+
param (
28+
[string] $SourceCommittish = "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}",
29+
[string] $TargetCommittish = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/"),
30+
[string] $DiffPath = "",
31+
[string] $DiffFilterType = 'd'
32+
)
33+
34+
Set-StrictMode -Version 3
35+
. (Join-Path $PSScriptRoot common.ps1)
36+
37+
return Get-ChangedFiles -SourceCommittish $SourceCommittish `
38+
-TargetCommittish $TargetCommittish `
39+
-DiffPath $DiffPath `
40+
-DiffFilterType $DiffFilterType
Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,13 @@
1+
# cSpell:ignore Committish
2+
# cSpell:ignore PULLREQUEST
3+
# cSpell:ignore TARGETBRANCH
14
param (
2-
# The root repo we scaned with.
5+
# The root repo we scanned with.
36
[string] $RootRepo = '$PSScriptRoot/../../..',
47
# The target branch to compare with.
58
[string] $targetBranch = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "/refs/heads/")
69
)
7-
$deletedFiles = (git diff $targetBranch HEAD --name-only --diff-filter=D)
8-
$renamedFiles = (git diff $targetBranch HEAD --diff-filter=R)
9-
$changedMarkdowns = (git diff $targetBranch HEAD --name-only -- '*.md')
1010

11-
$beforeRenameFiles = @()
12-
# Retrieve the 'renamed from' files. Git command only returns back the files after rename.
13-
# In order to have the files path before rename, it has to do some regex checking.
14-
# It is better to be replaced by more reliable commands if any.
15-
foreach ($file in $renamedFiles) {
16-
if ($file -match "^rename from (.*)$") {
17-
$beforeRenameFiles += $file -replace "^rename from (.*)$", '$1'
18-
}
19-
}
20-
# A combined list of deleted and renamed files.
21-
$relativePathLinks = ($deletedFiles + $beforeRenameFiles)
22-
# Removed the deleted markdowns.
23-
$changedMarkdowns = $changedMarkdowns | Where-Object { $deletedFiles -notcontains $_ }
24-
# Scan all markdowns and find if it contains the deleted or renamed files.
25-
$markdownContainLinks = @()
26-
$allMarkdownFiles = Get-ChildItem -Path $RootRepo -Recurse -Include *.md
27-
foreach ($f in $allMarkdownFiles) {
28-
$filePath = $f.FullName
29-
$content = Get-Content -Path $filePath -Raw
30-
foreach ($l in $relativePathLinks) {
31-
if ($content -match $l) {
32-
$markdownContainLinks += $filePath
33-
break
34-
}
35-
}
36-
}
11+
. (Join-Path $PSScriptRoot common.ps1)
3712

38-
# Convert markdowns path of the PR to absolute path.
39-
$adjustedReadmes = $changedMarkdowns | Foreach-Object { Resolve-Path $_ }
40-
$markdownContainLinks += $adjustedReadmes
41-
42-
# Get rid of any duplicated ones.
43-
$allMarkdowns = [string[]]($markdownContainLinks | Sort-Object | Get-Unique)
44-
45-
Write-Host "Here are all markdown files we need to check based on the changed files:"
46-
foreach ($file in $allMarkdowns) {
47-
Write-Host " $file"
48-
}
49-
return $allMarkdowns
13+
return Get-ChangedFiles -TargetCommittish $targetBranch -DiffPath '*.md'

eng/common/scripts/git-diff-changes.ps1

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)