|
| 1 | +# Shared auto-release operations used by language repos to resolve the pull request and |
| 2 | +# changed-file set that drive post-merge auto-release. Generic GitHub API calls live in |
| 3 | +# Invoke-GitHubAPI.ps1; this file holds the auto-release policy and diff-shaping logic. |
| 4 | + |
| 5 | +. "${PSScriptRoot}\logging.ps1" |
| 6 | +. "${PSScriptRoot}\Invoke-GitHubAPI.ps1" |
| 7 | + |
| 8 | +# Resolves the auto-release pull request for a commit SHA. |
| 9 | +# |
| 10 | +# Applies the shared auto-release selection policy: |
| 11 | +# 1. Look up the pull requests associated with the commit. |
| 12 | +# 2. Keep only pull requests merged into the target branch. |
| 13 | +# 3. Select the most recently merged one. |
| 14 | +# 4. Re-fetch that pull request by number to read its authoritative merge state and labels. |
| 15 | +# 5. Require it to be merged into the target branch and to carry the auto-release label. |
| 16 | +# |
| 17 | +# Returns a result object: |
| 18 | +# PullRequest : the selected PR object, or $null |
| 19 | +# PullRequestNumber : the selected PR number, or $null |
| 20 | +# IsEligible : $true only when a merged, labeled PR was found |
| 21 | +# SkipReason : a human readable reason when IsEligible is $false |
| 22 | +function Get-GitHubAutoReleasePullRequestForCommit { |
| 23 | + param ( |
| 24 | + $RepoOwner, |
| 25 | + $RepoName, |
| 26 | + $RepoId = "$RepoOwner/$RepoName", |
| 27 | + [Parameter(Mandatory = $true)] |
| 28 | + [ValidateNotNullOrEmpty()] |
| 29 | + $CommitSha, |
| 30 | + $TargetBranch = "main", |
| 31 | + $RequiredLabel = "auto-release", |
| 32 | + [ValidateNotNullOrEmpty()] |
| 33 | + [Parameter(Mandatory = $true)] |
| 34 | + $AuthToken |
| 35 | + ) |
| 36 | + |
| 37 | + $result = [PSCustomObject]@{ |
| 38 | + PullRequest = $null |
| 39 | + PullRequestNumber = $null |
| 40 | + IsEligible = $false |
| 41 | + SkipReason = "" |
| 42 | + } |
| 43 | + |
| 44 | + $associatedPullRequests = @(Get-GitHubPullRequestsForCommit -RepoId $RepoId -CommitSha $CommitSha -AuthToken $AuthToken) |
| 45 | + |
| 46 | + $mergedToTarget = @( |
| 47 | + $associatedPullRequests | |
| 48 | + Where-Object { $_.merged_at -and $_.base.ref -eq $TargetBranch } |
| 49 | + ) |
| 50 | + |
| 51 | + if ($mergedToTarget.Count -eq 0) { |
| 52 | + $result.SkipReason = "No merged pull request targeting '$TargetBranch' was associated with commit '$CommitSha'." |
| 53 | + return $result |
| 54 | + } |
| 55 | + |
| 56 | + $selectedPullRequest = $mergedToTarget | |
| 57 | + Sort-Object { [datetime]$_.merged_at } -Descending | |
| 58 | + Select-Object -First 1 |
| 59 | + |
| 60 | + $result.PullRequestNumber = $selectedPullRequest.number |
| 61 | + |
| 62 | + # Re-fetch the pull request by number to read its authoritative state. The commit -> pulls payload is |
| 63 | + # a secondary representation whose labels and merge state can lag the canonical pull request, so |
| 64 | + # eligibility (merge target + required label) is decided against this authoritative payload. |
| 65 | + $pullRequest = Get-GitHubPullRequest -RepoId $RepoId -PullRequestNumber $selectedPullRequest.number -AuthToken $AuthToken |
| 66 | + $result.PullRequest = $pullRequest |
| 67 | + |
| 68 | + if (-not $pullRequest.merged_at -or $pullRequest.base.ref -ne $TargetBranch) { |
| 69 | + $result.SkipReason = "Pull request #$($pullRequest.number) is not merged into '$TargetBranch'." |
| 70 | + return $result |
| 71 | + } |
| 72 | + |
| 73 | + $labels = @($pullRequest.labels | ForEach-Object { $_.name }) |
| 74 | + if ($RequiredLabel -notin $labels) { |
| 75 | + $result.SkipReason = "Pull request #$($pullRequest.number) does not have the required label '$RequiredLabel'." |
| 76 | + return $result |
| 77 | + } |
| 78 | + |
| 79 | + $result.IsEligible = $true |
| 80 | + return $result |
| 81 | +} |
| 82 | + |
| 83 | +# Converts GitHub pull request file entries into the Azure SDK PR diff object shape |
| 84 | +# consumed by package-detection tooling (compatible with Generate-PR-Diff.ps1 output). |
| 85 | +# |
| 86 | +# Parameters: |
| 87 | +# PullRequestNumber : the PR number recorded in the diff object. |
| 88 | +# PullRequestFiles : the file entries returned by Get-GitHubPullRequestFiles. |
| 89 | +# ExcludePaths : optional paths to record on the diff object. |
| 90 | +# |
| 91 | +# Returns a PSCustomObject with ChangedFiles, ChangedServices, ExcludePaths, DeletedFiles, PRNumber. |
| 92 | +function New-GitHubPullRequestDiffObject { |
| 93 | + param ( |
| 94 | + [Parameter(Mandatory = $true)] |
| 95 | + $PullRequestNumber, |
| 96 | + [Parameter(Mandatory = $true)] |
| 97 | + [AllowEmptyCollection()] |
| 98 | + [array] $PullRequestFiles, |
| 99 | + [AllowEmptyCollection()] |
| 100 | + [array] $ExcludePaths = @() |
| 101 | + ) |
| 102 | + |
| 103 | + $changedFiles = @() |
| 104 | + $deletedFiles = @() |
| 105 | + |
| 106 | + foreach ($file in $PullRequestFiles) { |
| 107 | + $filename = "$($file.filename)" -replace '\\', '/' |
| 108 | + |
| 109 | + if ($file.status -eq 'removed') { |
| 110 | + $deletedFiles += $filename |
| 111 | + } |
| 112 | + else { |
| 113 | + $changedFiles += $filename |
| 114 | + } |
| 115 | + |
| 116 | + # For renames, include the previous path as deleted so package detection sees both sides of the move. |
| 117 | + if ($file.status -eq 'renamed' -and $file.previous_filename) { |
| 118 | + $deletedFiles += ("$($file.previous_filename)" -replace '\\', '/') |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + $changedFiles = @($changedFiles | Where-Object { $_ } | Sort-Object -Unique) |
| 123 | + $deletedFiles = @($deletedFiles | Where-Object { $_ } | Sort-Object -Unique) |
| 124 | + |
| 125 | + $changedServices = @( |
| 126 | + $changedFiles + $deletedFiles | |
| 127 | + ForEach-Object { if ($_ -match "^sdk/([^/]+)/") { $Matches[1] } } | |
| 128 | + Sort-Object -Unique |
| 129 | + ) |
| 130 | + |
| 131 | + if (-not $ExcludePaths) { |
| 132 | + $ExcludePaths = @() |
| 133 | + } |
| 134 | + |
| 135 | + return [PSCustomObject]@{ |
| 136 | + ChangedFiles = $changedFiles |
| 137 | + ChangedServices = $changedServices |
| 138 | + ExcludePaths = @($ExcludePaths) |
| 139 | + DeletedFiles = $deletedFiles |
| 140 | + PRNumber = "$PullRequestNumber" |
| 141 | + } |
| 142 | +} |
0 commit comments