From 76ad9783131b8cf37edc59139d8e915a33d0c71a Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Wed, 22 Jan 2025 15:26:16 -0800 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 9656 (#3006) * Added label handle sdk-gen pipeline template Added common script to delete label from a PR * Update eng/common/scripts/Invoke-GitHubAPI.ps1 Co-authored-by: Ben Broderick Phillips --------- Co-authored-by: ray chen Co-authored-by: Ben Broderick Phillips --- .../templates/jobs/archetype-spec-gen-sdk.yml | 28 ++++++++++++++++ eng/common/scripts/Invoke-GitHubAPI.ps1 | 33 +++++++++++++++++++ eng/common/scripts/Remove-IssueLabel.ps1 | 32 ++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 eng/common/scripts/Remove-IssueLabel.ps1 diff --git a/eng/common/pipelines/templates/jobs/archetype-spec-gen-sdk.yml b/eng/common/pipelines/templates/jobs/archetype-spec-gen-sdk.yml index 2eb1ca6599..30722e8df7 100644 --- a/eng/common/pipelines/templates/jobs/archetype-spec-gen-sdk.yml +++ b/eng/common/pipelines/templates/jobs/archetype-spec-gen-sdk.yml @@ -168,6 +168,34 @@ jobs: artifactName: $(sdkArtifactName) targetPath: "$(System.DefaultWorkingDirectory)/generatedSdkArtifacts" + - task: PowerShell@2 + displayName: Add label to the spec PR + condition: and(eq(variables['Build.Reason'], 'PullRequest'), ne(variables['BreakingChangeLabel'], ''), eq(variables['BreakingChangeLabelAction'], 'add')) + inputs: + pwsh: true + workingDirectory: $(SdkRepoDirectory) + filePath: $(SdkRepoDirectory)/eng/common/scripts/Add-IssueLabels.ps1 + arguments: > + -RepoOwner $(SpecRepoOwner) + -RepoName $(SpecRepoName) + -IssueNumber "$(System.PullRequest.PullRequestNumber)" + -Labels $(BreakingChangeLabel) + -AuthToken "$(azuresdk-github-pat)" + + - task: PowerShell@2 + displayName: Remove label from the spec PR + condition: and(eq(variables['Build.Reason'], 'PullRequest'), ne(variables['BreakingChangeLabel'], ''), eq(variables['BreakingChangeLabelAction'], 'remove')) + inputs: + pwsh: true + workingDirectory: $(SdkRepoDirectory) + filePath: $(SdkRepoDirectory)/eng/common/scripts/Remove-IssueLabel.ps1 + arguments: > + -RepoOwner $(SpecRepoOwner) + -RepoName $(SpecRepoName) + -IssueNumber "$(System.PullRequest.PullRequestNumber)" + -LabelName $(BreakingChangeLabel) + -AuthToken "$(azuresdk-github-pat)" + - ${{ if eq(parameters.SkipPullRequestCreation, false) }}: - template: /eng/common/pipelines/templates/steps/git-push-changes.yml parameters: diff --git a/eng/common/scripts/Invoke-GitHubAPI.ps1 b/eng/common/scripts/Invoke-GitHubAPI.ps1 index c4a9fa5bb1..3c7c0596df 100644 --- a/eng/common/scripts/Invoke-GitHubAPI.ps1 +++ b/eng/common/scripts/Invoke-GitHubAPI.ps1 @@ -258,6 +258,39 @@ function Add-GitHubIssueComment { -MaximumRetryCount 3 } +# Will delete label from the issue if it exists +function Remove-GitHubIssueLabel { + param ( + [Parameter(Mandatory = $true)] + $RepoOwner, + [Parameter(Mandatory = $true)] + $RepoName, + [Parameter(Mandatory = $true)] + $IssueNumber, + [ValidateNotNullOrEmpty()] + [Parameter(Mandatory = $true)] + $LabelName, + [ValidateNotNullOrEmpty()] + [Parameter(Mandatory = $true)] + $AuthToken + ) + + if ($LabelName.Trim().Length -eq 0) + { + throw " The 'LabelName' parameter should not be empty or whitespace." + } + # Encode the label name + $encodedLabelName = [System.Web.HttpUtility]::UrlEncode($LabelName) + + $uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/labels/$encodedLabelName" + + return Invoke-RestMethod ` + -Method DELETE ` + -Uri $uri ` + -Headers (Get-GitHubApiHeaders -token $AuthToken) ` + -MaximumRetryCount 3 +} + # Will add labels to existing labels on the issue function Add-GitHubIssueLabels { param ( diff --git a/eng/common/scripts/Remove-IssueLabel.ps1 b/eng/common/scripts/Remove-IssueLabel.ps1 new file mode 100644 index 0000000000..1af5bea443 --- /dev/null +++ b/eng/common/scripts/Remove-IssueLabel.ps1 @@ -0,0 +1,32 @@ +[CmdletBinding(SupportsShouldProcess = $true)] +param( + [Parameter(Mandatory = $true)] + [string]$RepoOwner, + + [Parameter(Mandatory = $true)] + [string]$RepoName, + + [Parameter(Mandatory = $true)] + [string]$IssueNumber, + + [Parameter(Mandatory = $true)] + [string]$LabelName, + + [Parameter(Mandatory = $true)] + [string]$AuthToken +) + +. (Join-Path $PSScriptRoot common.ps1) + +try { + Remove-GitHubIssueLabel -RepoOwner $RepoOwner -RepoName $RepoName ` + -IssueNumber $IssueNumber -LabelName $LabelName -AuthToken $AuthToken +} +catch { + if ($_.Exception.Response.StatusCode -eq 404) { + LogWarning "Label $LabelName not found on issue" + exit 0 + } + LogError "Remove-GithubIssueLabel failed with exception:`n$_" + exit 1 +} \ No newline at end of file