|
15 | 15 | $AuthToken |
16 | 16 | ) |
17 | 17 | Set-StrictMode -version 3 |
18 | | - |
19 | 18 | . (Join-Path $PSScriptRoot common.ps1) |
20 | 19 |
|
21 | 20 | function Get-AllBranchesAndPullRequestInfo($owner, $repo) { |
@@ -66,67 +65,124 @@ if ($AuthToken) { |
66 | 65 | } |
67 | 66 |
|
68 | 67 | $owner, $repo = $RepoId -split "/" |
69 | | -$branches = Get-AllBranchesAndPullRequestInfo $owner $repo |
70 | 68 |
|
71 | | -foreach ($branch in $branches) |
72 | | -{ |
73 | | - $branchName = $branch.Name |
74 | | - if ($branchName -notmatch $BranchRegex) { |
75 | | - continue |
| 69 | +# These will always be output at the end of the script. Their only purpose is for information gathering |
| 70 | +# Total number returned from query |
| 71 | +$totalBranchesFromQuery = 0 |
| 72 | +# reasons why a branch was skipped |
| 73 | +$skippedBranchNotMatchRegex = 0 |
| 74 | +$skippedForCommitDate = 0 |
| 75 | +$skippedForOpenPRs = 0 |
| 76 | +$skippedForPRNotInBranch = 0 |
| 77 | +$skippedForPRNotInRepo = 0 |
| 78 | +# gh call counters |
| 79 | +$ghPRViewCalls = 0 |
| 80 | +$ghBranchDeleteCalls = 0 |
| 81 | + |
| 82 | +try { |
| 83 | + # Output the core rate limit at the start of processing. There's no real need |
| 84 | + # to output this at the end because the GH call counts are being output |
| 85 | + $coreRateLimit = Get-RateLimit core |
| 86 | + Write-RateLimit $coreRateLimit |
| 87 | + # Output the GraphQL rate limit before and after the call |
| 88 | + $graphqlRateLimit = Get-RateLimit graphql |
| 89 | + Write-RateLimit $graphqlRateLimit "Before GraphQL Call" |
| 90 | + $branches = Get-AllBranchesAndPullRequestInfo $owner $repo |
| 91 | + $graphqlRateLimit = Get-RateLimit graphql |
| 92 | + Write-RateLimit $graphqlRateLimit "After GraphQL Call" |
| 93 | + |
| 94 | + if ($branches) { |
| 95 | + $totalBranchesFromQuery = $branches.Count |
76 | 96 | } |
77 | | - $openPullRequests = @($branch.pullRequests | Where-Object { !$_.Closed }) |
78 | 97 |
|
79 | | - # If we have a central PR that created this branch still open don't delete the branch |
80 | | - if ($CentralRepoId) |
| 98 | + foreach ($branch in $branches) |
81 | 99 | { |
82 | | - $pullRequestNumber = $matches["PrNumber"] |
83 | | - # If central PR number is not found, then skip |
84 | | - if (!$pullRequestNumber) { |
85 | | - LogError "No PR number found in the branch name. Please check the branch name '$branchName'. Skipping..." |
| 100 | + $branchName = $branch.Name |
| 101 | + if ($branchName -notmatch $BranchRegex) { |
| 102 | + $skippedBranchNotMatchRegex++ |
86 | 103 | continue |
87 | 104 | } |
| 105 | + $openPullRequests = @($branch.pullRequests | Where-Object { !$_.Closed }) |
88 | 106 |
|
89 | | - $centralPR = gh pr view --json 'url,closed' --repo $CentralRepoId $pullRequestNumber | ConvertFrom-Json |
90 | | - if ($LASTEXITCODE) { |
91 | | - LogError "PR '$pullRequestNumber' not found in repo '$CentralRepoId'. Skipping..." |
92 | | - continue; |
93 | | - } else { |
94 | | - LogDebug "Found central PR $($centralPR.url) and Closed=$($centralPR.closed)" |
95 | | - if (!$centralPR.Closed) { |
96 | | - # Skipping if there is an open central PR open for the branch. |
97 | | - LogDebug "Central PR is still open so skipping the deletion of branch '$branchName'. Skipping..." |
98 | | - continue; |
| 107 | + # If we have a central PR that created this branch still open don't delete the branch |
| 108 | + if ($CentralRepoId) |
| 109 | + { |
| 110 | + $pullRequestNumber = $matches["PrNumber"] |
| 111 | + # If central PR number is not found, then skip |
| 112 | + if (!$pullRequestNumber) { |
| 113 | + LogError "No PR number found in the branch name. Please check the branch name '$branchName'. Skipping..." |
| 114 | + $skippedForPRNotInBranch++ |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + $ghPRViewCalls++ |
| 119 | + $centralPR = gh pr view --json 'url,closed' --repo $CentralRepoId $pullRequestNumber | ConvertFrom-Json |
| 120 | + if ($LASTEXITCODE) { |
| 121 | + LogError "PR '$pullRequestNumber' not found in repo '$CentralRepoId'. Skipping..." |
| 122 | + $skippedForPRNotInRepo++ |
| 123 | + continue |
| 124 | + } else { |
| 125 | + LogDebug "Found central PR $($centralPR.url) and Closed=$($centralPR.closed)" |
| 126 | + if (!$centralPR.Closed) { |
| 127 | + $skippedForOpenPRs++ |
| 128 | + # Skipping if there is an open central PR open for the branch. |
| 129 | + LogDebug "Central PR is still open so skipping the deletion of branch '$branchName'. Skipping..." |
| 130 | + continue |
| 131 | + } |
99 | 132 | } |
100 | 133 | } |
101 | | - } |
102 | | - else { |
103 | | - # Not CentralRepoId - not associated with a central repo PR |
104 | | - if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) { |
105 | | - LogDebug "Found open PRs associate with branch '$branchName'. Skipping..." |
106 | | - continue |
| 134 | + else { |
| 135 | + # Not CentralRepoId - not associated with a central repo PR |
| 136 | + if ($openPullRequests.Count -gt 0 -and !$DeleteBranchesEvenIfThereIsOpenPR) { |
| 137 | + $skippedForOpenPRs++ |
| 138 | + LogDebug "Found open PRs associate with branch '$branchName'. Skipping..." |
| 139 | + continue |
| 140 | + } |
107 | 141 | } |
108 | | - } |
109 | 142 |
|
110 | | - # If there is date filter, then check if branch last commit is older than the date. |
111 | | - if ($LastCommitOlderThan) |
112 | | - { |
113 | | - $commitDate = $branch.committedDate |
114 | | - if ($commitDate -gt $LastCommitOlderThan) { |
115 | | - LogDebug "The branch $branch last commit date '$commitDate' is newer than the date '$LastCommitOlderThan'. Skipping..." |
116 | | - continue |
| 143 | + # If there is date filter, then check if branch last commit is older than the date. |
| 144 | + if ($LastCommitOlderThan) |
| 145 | + { |
| 146 | + $commitDate = $branch.committedDate |
| 147 | + if ($commitDate -gt $LastCommitOlderThan) { |
| 148 | + $skippedForCommitDate++ |
| 149 | + LogDebug "The branch $branch last commit date '$commitDate' is newer than the date '$LastCommitOlderThan'. Skipping..." |
| 150 | + continue |
| 151 | + } |
117 | 152 | } |
118 | | - } |
119 | 153 |
|
120 | | - foreach ($openPullRequest in $openPullRequests) { |
121 | | - Write-Host "Note: Open pull Request '$($openPullRequest.url)' will be closed after branch deletion, given the central PR is closed." |
122 | | - } |
| 154 | + foreach ($openPullRequest in $openPullRequests) { |
| 155 | + LogDebug "Note: Open pull Request '$($openPullRequest.url)' will be closed after branch deletion, given the central PR is closed." |
| 156 | + } |
123 | 157 |
|
124 | | - $commitUrl = $branch.commitUrl |
125 | | - if ($PSCmdlet.ShouldProcess("'$branchName' in '$RepoId'", "Deleting branch on cleanup script")) { |
126 | | - gh api "repos/${RepoId}/git/refs/heads/${branchName}" -X DELETE |
127 | | - if ($LASTEXITCODE) { |
128 | | - LogError "Deletion of branch '$branchName` failed" |
| 158 | + $commitUrl = $branch.commitUrl |
| 159 | + if ($PSCmdlet.ShouldProcess("'$branchName' in '$RepoId'", "Deleting branch on cleanup script")) { |
| 160 | + $ghBranchDeleteCalls++ |
| 161 | + gh api "repos/${RepoId}/git/refs/heads/${branchName}" -X DELETE |
| 162 | + if ($LASTEXITCODE) { |
| 163 | + LogError "Deletion of branch '$branchName` failed, see command output above" |
| 164 | + exit $LASTEXITCODE |
| 165 | + } |
| 166 | + LogDebug "The branch '$branchName' at commit '$commitUrl' in '$RepoId' has been deleted." |
129 | 167 | } |
130 | | - Write-Host "The branch '$branchName' at commit '$commitUrl' in '$RepoId' has been deleted." |
| 168 | + } |
| 169 | +} |
| 170 | +finally { |
| 171 | + |
| 172 | + |
| 173 | + Write-Host "Number of branches returned from graphql query: $totalBranchesFromQuery" |
| 174 | + # The $BranchRegex seems to be always set |
| 175 | + if ($BranchRegex) { |
| 176 | + Write-Host "Number of branches that didn't match the BranchRegex: $skippedBranchNotMatchRegex" |
| 177 | + } |
| 178 | + Write-Host "Number of branches skipped for newer last commit date: $skippedForCommitDate" |
| 179 | + Write-Host "Number of branches skipped for open PRs: $skippedForOpenPRs" |
| 180 | + Write-Host "Number of gh api calls to delete branches: $ghBranchDeleteCalls" |
| 181 | + # The following are only applicable when $CentralRepoId is passed in |
| 182 | + if ($CentralRepoId) { |
| 183 | + Write-Host "The following are applicable because CentralRepoId was passed in:" |
| 184 | + Write-Host " Number of gh pr view calls: $ghPRViewCalls" |
| 185 | + Write-Host " Number of branches skipped due to PR not in the repository: $skippedForPRNotInRepo " |
| 186 | + Write-Host " Number of branches skipped due to PR not in the branch name: $skippedForPRNotInBranch" |
131 | 187 | } |
132 | 188 | } |
0 commit comments