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 2eb1ca65992b..30722e8df7fb 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 c4a9fa5bb11e..3c7c0596dfcd 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 000000000000..1af5bea443c9 --- /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