|
1 | | -name: Build validation (shared) |
2 | | - |
3 | | -permissions: |
4 | | - pull-requests: write |
5 | | - statuses: write |
6 | | - |
7 | | -on: |
8 | | - workflow_call: |
9 | | - inputs: |
10 | | - PayloadJson: |
11 | | - required: true |
12 | | - type: string |
13 | | - secrets: |
14 | | - AccessToken: |
15 | | - required: true |
16 | | - |
17 | | -jobs: |
18 | | - build: |
19 | | - name: Run Script |
20 | | - if: github.repository_owner == 'MicrosoftDocs' |
21 | | - runs-on: ubuntu-latest |
22 | | - steps: |
23 | | - - name: Script |
24 | | - shell: pwsh |
25 | | - env: |
26 | | - PayloadJson: ${{ inputs.PayloadJson }} |
27 | | - AccessToken: ${{ secrets.AccessToken }} |
28 | | - BuildUser: "learn-build-service-prod[bot]" |
29 | | - |
30 | | - run: | |
31 | | -
|
32 | | - # Get GitHub data and event |
33 | | - $GitHubData = $env:PayloadJson | ConvertFrom-Json -Depth 50 |
34 | | - $GitRequestEvent = $GitHubData.event_name |
35 | | - $BuildUser = $env:BuildUser |
36 | | - |
37 | | - $AccessToken = $env:AccessToken |
38 | | -
|
39 | | - $DefaultBranch = $GitHubData.event.repository.default_branch |
40 | | - $GitHubState = $GitHubData.event.issue.state |
41 | | - $GitHubAction = $GitHubData.event.action |
42 | | - $GitHubSender = $GitHubData.event.sender.login |
43 | | - $GitHubRepoName = $GitHubData.event.repository.name |
44 | | - $CommentUser = $GitHubData.event.comment.user.login |
45 | | - $PrIssueNumber = $GitHubData.event.issue.number |
46 | | - $PrUrl = $GitHubData.event.issue.pull_request.url |
47 | | -
|
48 | | - $GitHubHeaders = @{} |
49 | | - $GitHubHeaders.Add("Authorization","token $($AccessToken)") |
50 | | - $GitHubHeaders.Add("User-Agent", "OfficeDocs") |
51 | | -
|
52 | | - $StatusUrl = "https://review.learn.microsoft.com/en-us/office-authoring-guide/pr-criteria?branch=main" |
53 | | - $StatusCheckName = "PR has no warnings or errors" |
54 | | - $Status = @{} |
55 | | - $Status.Add("context", $StatusCheckName) |
56 | | - $Status.Add("target_url", $StatusUrl) |
57 | | -
|
58 | | - $ValidationRegex = "(?m)\s*(Validation status: )([Pp]assed|[Ss]uggestion[s]?|[Ww]arning[s]?|[Ee]rror[s]?)\s*$" |
59 | | -
|
60 | | - Write-Host "Repo: $GitHubRepoName" |
61 | | - Write-Host "Sender: $GitHubSender" |
62 | | - Write-Host "Request event: $GitRequestEvent" |
63 | | - Write-Host "GitHub action: $GitHubAction" |
64 | | - Write-Host "GitHub state: $GitHubState" |
65 | | - Write-Host "Default branch: $DefaultBranch" |
66 | | - Write-Host "PR number: $PrIssueNumber" |
67 | | -
|
68 | | - # Make the job summary section show up so the job always looks consistent. |
69 | | - echo "" >> $env:GITHUB_STEP_SUMMARY |
70 | | -
|
71 | | - If (($GitRequestEvent -eq "issue_comment") -and (($GitHubAction -eq "created"))) { |
72 | | -
|
73 | | - Write-Host "Comment added on PR." |
74 | | -
|
75 | | - If ($CommentUser -eq $BuildUser) { |
76 | | -
|
77 | | - Write-Host "Comment from $BuildUser. Processing." |
78 | | -
|
79 | | - # Get the contents of the comment that was added to the PR |
80 | | - $CommentBody = $GitHubData.event.comment.body |
81 | | -
|
82 | | - # Check to see if comment contains validation status |
83 | | - $StatusFound = $CommentBody -match $ValidationRegex |
84 | | -
|
85 | | - If ($StatusFound) { |
86 | | -
|
87 | | - Write-Host "Regex result: $StatusFound." |
88 | | -
|
89 | | - $ValidationResult = $Matches[2] |
90 | | -
|
91 | | - Write-Host "Validation status: $ValidationResult" |
92 | | -
|
93 | | - $PrData = Invoke-RestMethod -Method GET -ContentType "application/json" -Headers $GitHubHeaders -Uri $PrUrl -ErrorAction Stop |
94 | | - $StatusUrl = $PrData.statuses_url |
95 | | - $PrHtmlUrl = $PrData.html_url |
96 | | -
|
97 | | - Write-Host "PR status url: $StatusUrl" |
98 | | -
|
99 | | - If (($ValidationResult -like "*error*") -or ($ValidationResult -like "*warning*")) { |
100 | | - |
101 | | - # Populates the job summary if an PR validation has an error or warning. |
102 | | - echo "# Pull request validation error" >> $env:GITHUB_STEP_SUMMARY |
103 | | - echo "" >> $env:GITHUB_STEP_SUMMARY |
104 | | - echo "Build validation completed with a status of `"$ValidationResult`" in PR: $PrHtmlUrl. Builds must be free of errors and warnings before PRs can be merged." >> $env:GITHUB_STEP_SUMMARY |
105 | | -
|
106 | | - # Capitalize first letter of result. |
107 | | - $CapValidationResult = $ValidationResult.Substring(0,1).ToUpper() + $ValidationResult.Substring(1) |
108 | | -
|
109 | | - $Status.state = "failure" |
110 | | - $Status.description = "Blocking merge. $CapValidationResult must be resolved before merge." |
111 | | - |
112 | | - } ElseIf ($ValidationResult -like "*suggestion*") { |
113 | | - |
114 | | - write-host "Build validation completed with a status of `"$ValidationResult`". Allowing merge." |
115 | | -
|
116 | | - $Status.state = "success" |
117 | | - $Status.description = "Allowing merge. Please resolve $ValidationResult." |
118 | | -
|
119 | | - } Else { |
120 | | - |
121 | | - write-host "Build validation completed with a status of `"$ValidationResult`". Allowing merge." |
122 | | -
|
123 | | - $Status.state = "success" |
124 | | - $Status.description = "Allowing merge." |
125 | | - |
126 | | - } |
127 | | -
|
128 | | - $StatusJson = $Status | ConvertTo-Json |
129 | | - $SuccessfulPost = $False |
130 | | - $RetryCount = 0 |
131 | | - |
132 | | - Do { |
133 | | - |
134 | | - Try { |
135 | | - |
136 | | - # Send POST request to GitHub |
137 | | - Invoke-RestMethod -Headers $GitHubHeaders -Uri $StatusUrl -Method POST -Body $StatusJson -ErrorAction Stop |
138 | | - $SuccessfulPost = $True |
139 | | - |
140 | | - } Catch { |
141 | | - |
142 | | - # If the request fails for any reason, retry it after a delay, up to six times. |
143 | | - $RetryCount++ |
144 | | - Start-Sleep 1 |
145 | | - |
146 | | - } |
147 | | - |
148 | | - } Until (($SuccessfulPost) -or ($RetryCount -gt 5)) |
149 | | -
|
150 | | - |
151 | | - If (($ValidationResult -like "*error*") -or ($ValidationResult -like "*warning*")) { |
152 | | -
|
153 | | - # Force the workflow to fail so the validation failure can be tracked in Actions. |
154 | | -
|
155 | | - Throw "Build validation completed with a status of `"$ValidationResult`" in PR: $PrHtmlUrl. Builds must be free of errors and warnings before PRs can be merged." |
156 | | -
|
157 | | - } |
158 | | -
|
159 | | - } Else { |
160 | | - |
161 | | - Write-Host "Comment was from $BuildUser but no status was found." |
162 | | - |
163 | | - } |
164 | | -
|
165 | | - } Else { |
166 | | - |
167 | | - Write-Host "Comment not from $BuildUser. Exiting." |
168 | | -
|
169 | | - } |
170 | | -
|
171 | | - } Else { |
172 | | - |
173 | | - Write-Host "Not an added comment on PR." |
174 | | - |
175 | | - } # PR event and action check |
| 1 | +name: Shared-BuildValidation (deprecated) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + PayloadJson: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + secrets: |
| 10 | + AccessToken: |
| 11 | + required: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + noop: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - run: echo "This workflow is no longer in use. It has been moved to MicrosoftDocs/tcp-workflows." |
0 commit comments