Skip to content

Commit

Permalink
Added label handle sdk-gen pipeline template (#9656)
Browse files Browse the repository at this point in the history
* 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 <bebroder@microsoft.com>

---------

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>
  • Loading branch information
raych1 and benbp authored Jan 22, 2025
1 parent 46934b1 commit 81f76ad
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
28 changes: 28 additions & 0 deletions eng/common/pipelines/templates/jobs/archetype-spec-gen-sdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
32 changes: 32 additions & 0 deletions eng/common/scripts/Remove-IssueLabel.ps1
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 81f76ad

Please sign in to comment.