Skip to content

Commit 152d1fa

Browse files
authored
Add check for PubOps/non-PubOps repos
1 parent 9e6e1cc commit 152d1fa

File tree

1 file changed

+115
-46
lines changed

1 file changed

+115
-46
lines changed

.github/workflows/Shared-AutoPublish.yml

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
EnableAutoPublish: ${{ inputs.EnableAutoPublish }}
3737
PrivateKey: ${{ secrets.PrivateKey }}
3838
ClientId: ${{ secrets.ClientId }}
39+
IsPubOpsRepo: ${{ contains(github.event.repository.topics, 'pubops') }}
3940

4041
run: |
4142
@@ -44,6 +45,7 @@ jobs:
4445
$AccessToken = $env:AccessToken
4546
$PrivateKey = $env:PrivateKey
4647
$ClientId = $env:ClientId
48+
$IsPubOpsRepo = [System.Convert]::ToBoolean($env:IsPubOpsRepo)
4749
4850
# Date/Time calculations
4951
$UtcNow = Get-Date -AsUTC
@@ -70,7 +72,8 @@ jobs:
7072
7173
# PR variables
7274
$PrTitle = "[AutoPublish] $DefaultBranch to $TargetBranch"
73-
$PrDescription = "@MicrosoftDocs/marveldocs-pubops`n`nThis is an automated pull request to publish changes from the $DefaultBranch branch to the $TargetBranch branch. Merging this PR will publish the changes to the live learn.microsoft.com site.`n`nBefore merging this PR, complete the following checks:`n`n- There are no more than 100 files in the PR. If there are more than 100 files, review the changes for any obvious mistakes such as mass-deleted files. If the changes appear normal (bulk changes, general updates to many files, etc), merge the PR. If you're not sure, do not merge the PR and investigate further.`n- There are no build warnings. If there are warnings, resolve them before merging the PR.`n- Regular PR criteria checks pass.`n`nIf you have questions, email marveldocs-admins."
75+
$PrDescriptionPubOps = "@MicrosoftDocs/marveldocs-pubops`n`nThis is an automated pull request to publish changes from the $DefaultBranch branch to the $TargetBranch branch. Merging this PR will publish the changes to the live learn.microsoft.com site.`n`nBefore merging this PR, complete the following checks:`n`n- There are no more than 100 files in the PR. If there are more than 100 files, review the changes for any obvious mistakes such as mass-deleted files. If the changes appear normal (bulk changes, general updates to many files, etc), merge the PR. If you're not sure, do not merge the PR and investigate further.`n- There are no build warnings. If there are warnings, resolve them before merging the PR.`n- Regular PR criteria checks pass.`n`nIf you have questions, email marveldocs-admins."
76+
$PrDescriptionNonPubOps = "This is an automated pull request to publish changes from the $DefaultBranch branch to the $TargetBranch branch.`n`nThis is repo isn't managed by PubOps.`n`n**A contributor with write access to this repo needs to merge this PR for changes in it to go live.**`n`nBefore you merge this PR:`n`n- Check the **Files changed** tab to ensure only changes you intend to publish are included in the PR.`n- View the changes on https://review.learn.microsoft.com to confirm the changes look correct on the site`n`nIf you have questions, email marveldocs-admins."
7477
7578
# Label variables
7679
$AutoPublishLabelColor = "5319E7"
@@ -79,6 +82,9 @@ jobs:
7982
$SignOffLabelColor = "46ce1c"
8083
$SignOffLabelDescription = "The pull request is ready to be reviewed and merged by PubOps."
8184
$SignOffLabel = "Sign off"
85+
$ManualMergeLabelColor = "FF6600"
86+
$ManualMergeLabelDescription = "An AutoPublish pull request requires a human to merge it."
87+
$ManualMergeLabel = "Manual merge required"
8288
8389
#####################
8490
#####################
@@ -229,7 +235,7 @@ jobs:
229235

230236
Write-Host "Checking to see if label $Name exists in repo URL $LabelUri."
231237

232-
$LabelResults = Invoke-WebRequest -UseBasicParsing -Uri $LabelUri -Headers $AppGitHubAccessHeaders -ErrorAction Stop
238+
$LabelResults = Invoke-WebRequest -UseBasicParsing -Uri $LabelUri -Headers $StandardGitHubHeaders -ErrorAction Stop
233239
$LabelFound = $True
234240

235241
} Catch {
@@ -276,7 +282,7 @@ jobs:
276282

277283
Write-Host "Creating label $Name with color $Color on repo $RepoUri."
278284

279-
$Result = Invoke-RestMethod -Uri $RepoUri -Headers $AppGitHubAccessHeaders -Body $Body -Method POST
285+
$Result = Invoke-RestMethod -Uri $RepoUri -Headers $StandardGitHubHeaders -Body $Body -Method POST
280286

281287
} Catch {
282288

@@ -309,7 +315,7 @@ jobs:
309315

310316
Write-Host "Getting labels on issue $IssueLabelUrl."
311317

312-
$LabelResults = Invoke-RestMethod -Uri $IssueLabelUrl -Headers $AppGitHubAccessHeaders -ErrorAction Stop
318+
$LabelResults = Invoke-RestMethod -Uri $IssueLabelUrl -Headers $StandardGitHubHeaders -ErrorAction Stop
313319

314320
} Catch {
315321

@@ -371,7 +377,7 @@ jobs:
371377

372378
Write-Host "Setting label $LabelName on URL $IssueLabelUrl."
373379

374-
$Result = Invoke-RestMethod -Uri $IssueLabelUrl -Body $Body -Headers $AppGitHubAccessHeaders -Method POST
380+
$Result = Invoke-RestMethod -Uri $IssueLabelUrl -Body $Body -Headers $StandardGitHubHeaders -Method POST
375381

376382

377383
} Catch {
@@ -482,47 +488,42 @@ jobs:
482488

483489
#####################
484490
#####################
485-
# New-PullRequest
491+
# Set-Labels
486492

487-
Function New-PullRequest {
493+
Function Set-Labels {
494+
495+
param(
488496

489-
$PrResponse = $Null
497+
[Parameter(Mandatory = $True)]
498+
[string]$IssueUrl
499+
500+
)
490501

491-
Write-Host "Creating a new PR from $DefaultBranch to $TargetBranch."
502+
# Check if labels exist on the repo.
503+
$AutoLabelExists = Test-RepoLabel -RepoUri $RepoLabelUrl -Name $AutoPublishLabel
492504

493-
# Create github HTTP authentication header using GitHub app installation token
494-
$AppGitHubAccessToken = Get-GitHubAppInstallationToken -ClientId $ClientId -PrivateKey $PrivateKey -Organization $Organization -TokenTTLMinutes 10
495-
$AppGitHubAccessHeaders = @{}
496-
$AppGitHubAccessHeaders.Add("Authorization","token $($AppGitHubAccessToken)")
497-
$AppGitHubAccessHeaders.Add("User-Agent", "OfficeDocs")
498-
499-
$PrTitle = "$PrTitle - $PacificStamp $(Get-TzAbbrev $PacificTz $PacificNow) | $IndiaStamp $(Get-TzAbbrev $IndiaTz $IndiaNow)"
505+
If (!$AutoLabelExists) {
500506

501-
$PrBody = @{
502-
title = $PrTitle
503-
head = $DefaultBranch
504-
base = "$TargetBranch"
505-
body = $PrDescription
506-
} | ConvertTo-Json
507+
# Create label on the repo if it doesn't exist.
508+
New-RepoLabel -RepoUri $RepoLabelUrl -Name $AutoPublishLabel -Color $AutoPublishLabelColor -Description $AutoPublishLabelDescription
507509

508-
Try {
510+
}
509511

510-
$PrResponse = Invoke-RestMethod -Uri "https://api.github.com/repos/$Organization/$Repository/pulls" -Method POST -Headers $AppGitHubAccessHeaders -Body $PrBody -ErrorAction Stop
512+
# Only add the AutoPublishLabel label if it doesn't already exist on the PR
513+
If (!$LabelResultsArray.$AutoPublishLabel) {
511514

512-
Write-Host "Created pull request $($PrResponse.html_url)"
515+
Write-Host "Label $AutoPublishLabel doesn't exist on $IssueUrl. Adding label."
513516

514-
$IssueUrl = $PrResponse.issue_url
517+
# Add the label to the PR
518+
Set-PrLabel -IssueUrl $IssueUrl -LabelName $AutoPublishLabel
515519

516-
# Check if labels exist on the repo.
517-
$AutoLabelExists = Test-RepoLabel -RepoUri $RepoLabelUrl -Name $AutoPublishLabel
518-
$SignOffLabelExists = Test-RepoLabel -RepoUri $RepoLabelUrl -Name $SignOffLabel
520+
}
519521

520-
If (!$AutoLabelExists) {
522+
If ($IsPubOpsRepo) {
521523

522-
# Create label on the repo if it doesn't exist.
523-
New-RepoLabel -RepoUri $RepoLabelUrl -Name $AutoPublishLabel -Color $AutoPublishLabelColor -Description $AutoPublishLabelDescription
524+
Write-Host "PubOps repo. Checking and setting $SignOffLabel label."
524525

525-
}
526+
$SignOffLabelExists = Test-RepoLabel -RepoUri $RepoLabelUrl -Name $SignOffLabel
526527

527528
If (!$SignOffLabelExists) {
528529

@@ -531,40 +532,102 @@ jobs:
531532

532533
}
533534

534-
# Check to see if the labels we're interested in are already added to the PR.
535-
$LabelResultsArray = Test-Prlabel -LabelArray $AutoPublishLabel,$SignOffLabel -IssueUrl $IssueUrl
536535

537-
# Only add the AutoPublishLabel label if it doesn't already exist on the PR
538-
If (!$LabelResultsArray.$AutoPublishLabel) {
536+
# Only add the SignOffLabel label if it doesn't already exist on the PR
539537

540-
Write-Host "Label $AutoPublishLabel doesn't exist on $IssueUrl. Adding label."
538+
If (!$LabelResultsArray.$SignOffLabel) {
539+
540+
Write-Host "Label $SignOffLabel doesn't exist on $IssueUrl. Adding label."
541541

542542
# Add the label to the PR
543-
Set-PrLabel -IssueUrl $IssueUrl -LabelName $AutoPublishLabel
543+
Set-PrLabel -IssueUrl $IssueUrl -LabelName $SignOffLabel
544544

545545
}
546546

547-
# Only add the SignOffLabel label if it doesn't already exist on the PR
548547

549-
If (!$LabelResultsArray.$SignOffLabel) {
550548

551-
Write-Host "Label $SignOffLabel doesn't exist on $IssueUrl. Adding label."
549+
} Else {
550+
551+
Write-Host "Non-PubOps repo. Checking and setting $ManualMergeLabel label."
552+
553+
$ManualMergeLabelExists = Test-RepoLabel -RepoUri $RepoLabelUrl -Name $ManualMergeLabel
554+
555+
If (!$ManualMergeLabelExists) {
556+
557+
# Create label on the repo if it doesn't exist.
558+
New-RepoLabel -RepoUri $RepoLabelUrl -Name $ManualMergeLabel -Color $ManualMergeLabelColor -Description $ManualMergeLabelDescription
559+
560+
}
561+
562+
# Only add the ManualMergeLabel label if it doesn't already exist on the PR
563+
564+
If (!$LabelResultsArray.$ManualMergeLabel) {
565+
566+
Write-Host "Label $ManualMergeLabel doesn't exist on $IssueUrl. Adding label."
552567

553568
# Add the label to the PR
554-
Set-PrLabel -IssueUrl $IssueUrl -LabelName $SignOffLabel
569+
Set-PrLabel -IssueUrl $IssueUrl -LabelName $ManualMergeLabel
555570

556571
}
557572

573+
}
574+
575+
}
576+
577+
#####################
578+
#####################
579+
# New-PullRequest
580+
581+
Function New-PullRequest {
582+
583+
$PrResponse = $Null
584+
585+
Write-Host "Creating a new PR from $DefaultBranch to $TargetBranch."
586+
587+
# Create github HTTP authentication header using GitHub app installation token
588+
$AppGitHubAccessToken = Get-GitHubAppInstallationToken -ClientId $ClientId -PrivateKey $PrivateKey -Organization $Organization -TokenTTLMinutes 10
589+
$AppGitHubAccessHeaders = @{}
590+
$AppGitHubAccessHeaders.Add("Authorization","token $($AppGitHubAccessToken)")
591+
$AppGitHubAccessHeaders.Add("User-Agent", "OfficeDocs")
592+
593+
$PrTitle = "$PrTitle - $PacificStamp $(Get-TzAbbrev $PacificTz $PacificNow) | $IndiaStamp $(Get-TzAbbrev $IndiaTz $IndiaNow)"
594+
595+
If ($IsPubOpsRepo) {
596+
597+
$PrDescription = $PrDescriptionPubOps
598+
599+
} Else {
600+
601+
$PrDescription = $PrDescriptionNonPubOps
602+
603+
}
604+
605+
$PrBody = @{
606+
title = $PrTitle
607+
head = $DefaultBranch
608+
base = "$TargetBranch"
609+
body = $PrDescription
610+
} | ConvertTo-Json
611+
612+
Try {
613+
614+
$PrResponse = Invoke-RestMethod -Uri "https://api.github.com/repos/$Organization/$Repository/pulls" -Method POST -Headers $AppGitHubAccessHeaders -Body $PrBody -ErrorAction Stop
615+
616+
Write-Host "Created pull request $($PrResponse.html_url)"
617+
558618
} Catch {
559619

560620
$PrResponse = $Null
621+
622+
Write-Host "ERROR occurred during PR creation. Error: $_"
561623

562624
}
563625

564626
Return $PrResponse
565627

566628
}
567629

630+
568631
#####################
569632
#####################
570633
# Main
@@ -575,8 +638,6 @@ jobs:
575638

576639
$PrData = Get-PublishPullRequest
577640

578-
write-host $($PrData | ConvertTo-Json -Depth 50)
579-
580641
# Check to see if $PrData contains data. If yes, there's a PR. If not, no PR.
581642
If ($PrData) {
582643

@@ -597,9 +658,17 @@ jobs:
597658
If ($BranchDiff.ahead_by -gt 0) {
598659

599660
Write-Host "$DefaultBranch has changes ahead of $TargetBranch"
661+
Write-Host "PubOps repo: $IsPubOpsRepo"
600662

601663
$NewPrResponse = New-PullRequest
602-
664+
$IssueUrl = $NewPrResponse.issue_url
665+
666+
If ($IssueUrl) {
667+
668+
Set-Labels -IssueUrl $IssueUrl
669+
670+
}
671+
603672
} Else {
604673

605674
Write-Host "$DefaultBranch has no changes ahead of $TargetBranch. Not creating PR."

0 commit comments

Comments
 (0)