@@ -2,7 +2,7 @@ name: Tier management
22
33permissions :
44 pull-requests : write
5- contents : read
5+ contents : write
66
77on :
88 workflow_call :
2121 required : true
2222
2323jobs :
24- build :
25- name : Run Script
26- if : github.repository_owner == 'MicrosoftDocs'
24+ tier-management :
25+ name : Tier management
26+ if : github.repository_owner == 'MicrosoftDocs' && github.event_name == 'issue_comment'
2727 runs-on : ubuntu-latest
2828 steps :
2929 - name : Script
@@ -447,4 +447,130 @@ jobs:
447447
448448 } # PR event and action check
449449
450-
450+ set-draft :
451+ name : Set PR as draft
452+ if : github.repository_owner == 'MicrosoftDocs' && github.event_name == 'pull_request_target'
453+ runs-on : ubuntu-latest
454+ steps :
455+ - name : Script
456+ shell : pwsh
457+ env :
458+ PayloadJson : ${{ inputs.PayloadJson }}
459+ AccessToken : ${{ secrets.AccessToken }}
460+
461+ run : |
462+
463+ # Get GitHub data and event
464+ $GitHubData = $env:PayloadJson | ConvertFrom-Json -Depth 50
465+ $GitRequestEvent = $GitHubData.event_name
466+
467+ $AccessToken = $env:AccessToken
468+
469+ $DefaultBranch = $GitHubData.event.repository.default_branch
470+ $GitHubSender = $GitHubData.event.sender.login
471+ $PrUrl = $GitHubData.event.pull_request.url
472+ $CommentsUrl = $GitHubData.event.pull_request.comments_url
473+ $UserPermissionUrl = $GitHubData.event.repository.collaborators_url.Replace("{/collaborator}", "/$GitHubSender/permission" )
474+
475+ $DraftMessage = "<h1>Pull request set to Draft</h1><p>Hi @{0}. <p>To avoid accidentally publishing the changes in this pull request prematurely, its state has been changed to <b>Draft</b>.<p>When you're ready for the changes in this pull request to be published live, select the <b>Ready for review</b> button at the bottom of the page.<p>If you have questions, please post a message to <a href=`"https://aka.ms/askanadmin`">https://aka.ms/askanadmin</a>."
476+
477+ # Create github HTTP authentication header
478+ $GitHubHeaders = @{}
479+ $GitHubHeaders.Add("Authorization","token $($AccessToken)")
480+ $GitHubHeaders.Add("User-Agent", "OfficeDocs")
481+
482+ #####################
483+ #####################
484+ # Set-PrMessage
485+
486+ Function Set-PrMessage {
487+
488+ [cmdletbinding()]
489+ Param(
490+ [Parameter(Mandatory=$True)]
491+ $Message
492+ )
493+
494+ $BodyHash = @{}
495+ $BodyHash.body = $Message
496+ $BodyJson = $BodyHash | ConvertTo-Json
497+ $BodyJson
498+
499+ Try {
500+
501+ $Result = Invoke-WebRequest -UseBasicParsing -Uri $CommentsUrl -Body $BodyJson -Headers $GitHubHeaders -Method POST -ErrorAction Stop
502+
503+ $PostCommentSuccess = $True
504+
505+ } Catch {
506+
507+ $PostCommentSuccess = $False
508+
509+ }
510+
511+ Return $PostCommentSuccess
512+
513+ }
514+
515+
516+ #####################
517+ #####################
518+ # Main
519+
520+
521+ # Get permission level of user who created the comment. Need to use .role_name instead of .permission because .permission provides only legacy values.
522+ # .role_name provides legacy plus triage, maintain, and custom roles like write-elevated.
523+ $UserPermission = $(Invoke-RestMethod -Method GET -Headers $GitHubHeaders -Uri $UserPermissionUrl).role_name
524+
525+ Write-Host "User $GitHubSender permission level: $UserPermission."
526+
527+ # If user has triage or above, do nothing, otherwise switch PR to draft.
528+ If (($UserPermission -like "write*") -or ($UserPermission -eq "maintain") -or ($UserPermission -eq "triage") -or ($UserPermission -eq "admin")) {
529+
530+ Write-Host "User has $UserPermission access. Not switching PR to draft."
531+
532+ } Else {
533+
534+ Write-Host "PR URL: $PrUrl"
535+
536+ # REST: get PR node_id
537+ $PrData = Invoke-RestMethod -Method Get -Headers $GitHubHeaders -Uri $PrUrl
538+ $PrNodeId = $PrData.node_id
539+
540+ Write-Host "Setting PR $($PrData.number) to draft. Node ID: $PrNodeId."
541+
542+ $Body = @{
543+ query = @'
544+ mutation($id: ID!){
545+ convertPullRequestToDraft(input:{pullRequestId:$id}) {
546+ pullRequest { number url isDraft }
547+ }
548+ }
549+ '@
550+ variables = @{ id = $PrNodeId }
551+ } | ConvertTo-Json -Depth 5
552+
553+
554+ Try {
555+
556+ $Resp = Invoke-RestMethod -Method Post -Uri "https://api.github.com/graphql" -Headers $GitHubHeaders -ContentType 'application/json' -Body $Body
557+
558+ # Show errors if any, otherwise show the PR
559+ If ($Resp.errors) {
560+ Write-Error ("GraphQL error(s): " + ($Resp.errors | ConvertTo-Json -Depth 10))
561+ } Else {
562+ $Resp.data.convertPullRequestToDraft.pullRequest | ConvertTo-Json -Depth 5
563+ }
564+
565+ $DraftMessage = $DraftMessage -f $GitHubSender
566+
567+ Set-PrMessage -Message $DraftMessage
568+
569+ } Catch {
570+
571+ Write-Host "ERROR: Failed to set PR to draft. Error: $_"
572+
573+ }
574+
575+
576+ }
0 commit comments