-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathVerifyPRChanges.ps1
83 lines (73 loc) · 3.01 KB
/
VerifyPRChanges.ps1
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
76
77
78
79
80
81
82
83
Param(
[Parameter(HelpMessage = "The token to use for the GitHub API", Mandatory = $false)]
[string] $token,
[Parameter(HelpMessage = "The name of the repository the PR is going to", Mandatory = $false)]
[string] $prBaseRepository,
[Parameter(HelpMessage = "The id of the pull request", Mandatory = $false)]
[string] $pullRequestId
)
function ValidateFiles
(
[Object[]] $Files
) {
$disallowedExtensions = @('.ps1', '.psm1', '.yml', '.yaml')
$disallowedFiles = @('CODEOWNERS')
$Files | ForEach-Object {
$filename = $_.filename
$status = $_.status
Write-Host "- $filename $status"
$extension = [System.IO.Path]::GetExtension($filename)
$name = [System.IO.Path]::GetFileName($filename)
if (($extension -in $disallowedExtensions) -or ($name -in $disallowedFiles) -or $filename.StartsWith(".github/")) {
throw "Pull Request containing changes to scripts, workflows or CODEOWNERS are not allowed from forks."
}
}
}
function ValidatePullRequest
(
[string[]] $PullRequestRepository,
[string[]] $PullRequestId,
[hashtable] $Headers,
[int] $MaxAllowedChangedFiles = 3000
) {
$url = "https://api.github.com/repos/$($prBaseRepository)/pulls/$pullRequestId"
$pullRequestDetails = (Invoke-WebRequest -UseBasicParsing -Headers $Headers -Uri $url).Content | ConvertFrom-Json
# List Pull Request files has a max of 3000 files. https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests-files
if ($pullRequestDetails.changed_files -gt $MaxAllowedChangedFiles) {
throw "Pull request contains changes to $($pullRequestDetails.changed_files) files. You cannot change more than $MaxAllowedChangedFiles files from a fork."
}
}
function ValidatePullRequestFiles
(
[string[]] $PullRequestRepository,
[string[]] $PullRequestId,
[hashtable] $Headers
) {
$pageNumber = 1
$resultsPerPage = 100
$hasMoreData = $true
Write-Host "Files Changed:"
while ($hasMoreData) {
$url = "https://api.github.com/repos/$($prBaseRepository)/pulls/$pullRequestId/files?per_page=$resultsPerPage&page=$pageNumber"
$changedFiles = (Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri $url).Content | ConvertFrom-Json
# Finish check if there are no more files to be validated
if (-not $changedFiles) {
break
}
ValidateFiles -Files $changedFiles
if ($changedFiles -and ($changedFiles.Count -eq $resultsPerPage)) {
$pageNumber += 1
}
else {
$hasMoreData = $false
}
}
Write-Host "Verification completed successfully."
}
$headers = @{
"Authorization" = "token $token"
"X-GitHub-Api-Version" = "2022-11-28"
"Accept" = "application/vnd.github+json"
}
ValidatePullRequest -PullRequestRepository $prBaseRepository -PullRequestId $pullRequestId -Headers $headers
ValidatePullRequestFiles -PullRequestRepository $prBaseRepository -PullRequestId $pullRequestId -Headers $headers