|
| 1 | +# Copyright 2023-2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# NOTE: This file is automatically generated from: |
| 16 | +# https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/main/infra/terraform/modules/workflow_files/periodic-reporter.yaml |
| 17 | + |
| 18 | +name: 'reporter' |
| 19 | + |
| 20 | +on: |
| 21 | + schedule: |
| 22 | + # 2 hours after scheduled periodic and once again in the evening |
| 23 | + - cron: '0 5,17 * * *' |
| 24 | + workflow_dispatch: |
| 25 | + |
| 26 | +jobs: |
| 27 | + report: |
| 28 | + if: github.repository_owner == 'GoogleCloudPlatform' || github.repository_owner == 'terraform-google-modules' |
| 29 | + |
| 30 | + permissions: |
| 31 | + issues: 'write' |
| 32 | + |
| 33 | + runs-on: 'ubuntu-latest' |
| 34 | + |
| 35 | + steps: |
| 36 | + - uses: 'actions/github-script@v7' |
| 37 | + with: |
| 38 | + script: |- |
| 39 | + // label for all issues opened by reporter |
| 40 | + const periodicLabel = 'periodic-failure'; |
| 41 | +
|
| 42 | + // check if any reporter opened any issues previously |
| 43 | + const prevIssues = await github.paginate(github.rest.issues.listForRepo, { |
| 44 | + ...context.repo, |
| 45 | + state: 'open', |
| 46 | + creator: 'github-actions[bot]', |
| 47 | + labels: [periodicLabel] |
| 48 | + }); |
| 49 | + // createOrCommentIssue creates a new issue or comments on an existing issue. |
| 50 | + const createOrCommentIssue = async function (title, txt) { |
| 51 | + if (prevIssues.length < 1) { |
| 52 | + console.log('no previous issues found, creating one'); |
| 53 | + await github.rest.issues.create({ |
| 54 | + ...context.repo, |
| 55 | + title: title, |
| 56 | + body: txt, |
| 57 | + labels: [periodicLabel] |
| 58 | + }); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (prevIssues.length > 1) { |
| 62 | + console.warn( |
| 63 | + `found ${prevIssues.length} issues but only adding comment to ${prevIssues[0].html_url}` |
| 64 | + ); |
| 65 | + } |
| 66 | + console.log( |
| 67 | + `found previous issue ${prevIssues[0].html_url}, adding comment` |
| 68 | + ); |
| 69 | + await github.rest.issues.createComment({ |
| 70 | + ...context.repo, |
| 71 | + issue_number: prevIssues[0].number, |
| 72 | + body: txt |
| 73 | + }); |
| 74 | + }; |
| 75 | +
|
| 76 | + // updateAndCloseIssues comments on any existing issues and closes them. No-op if no issue exists. |
| 77 | + const updateAndCloseIssues = async function (txt) { |
| 78 | + if (prevIssues.length < 1) { |
| 79 | + console.log('no previous issues found, skipping close'); |
| 80 | + return; |
| 81 | + } |
| 82 | + for (const prevIssue of prevIssues) { |
| 83 | + console.log(`found previous issue ${prevIssue.html_url}, adding comment`); |
| 84 | + await github.rest.issues.createComment({ |
| 85 | + ...context.repo, |
| 86 | + issue_number: prevIssue.number, |
| 87 | + body: txt |
| 88 | + }); |
| 89 | + console.log(`closing ${prevIssue.html_url}`); |
| 90 | + await github.rest.issues.update({ |
| 91 | + ...context.repo, |
| 92 | + issue_number: prevIssue.number, |
| 93 | + body: txt, |
| 94 | + state: 'closed' |
| 95 | + }); |
| 96 | + } |
| 97 | + }; |
| 98 | +
|
| 99 | + // Find status of check runs. |
| 100 | + // We will find check runs for each commit and then filter for the periodic. |
| 101 | + // Checks API only allows for ref and if we use main there could be edge cases where |
| 102 | + // the check run happened on a SHA that is different from head. |
| 103 | + const commits = await github.paginate(github.rest.repos.listCommits, { |
| 104 | + ...context.repo |
| 105 | + }); |
| 106 | +
|
| 107 | + var foundCheck = false; |
| 108 | + let periodicCheck = {}; |
| 109 | +
|
| 110 | + for (const commit of commits) { |
| 111 | + console.log( |
| 112 | + `checking runs at ${commit.html_url}: ${commit.commit.message}` |
| 113 | + ); |
| 114 | + const checks = await github.rest.checks.listForRef({ |
| 115 | + ...context.repo, |
| 116 | + ref: commit.sha |
| 117 | + }); |
| 118 | + // find runs for this commit |
| 119 | + for (const check of checks.data.check_runs) { |
| 120 | + console.log(`found run ${check.name} for ${commit.html_url}`); |
| 121 | + if (check.name.includes('periodic-int-trigger')) { |
| 122 | + foundCheck = true; |
| 123 | + periodicCheck = check; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + if (foundCheck) { |
| 129 | + if ( |
| 130 | + periodicCheck.status === 'completed' && |
| 131 | + periodicCheck.conclusion === 'success' |
| 132 | + ) { |
| 133 | + updateAndCloseIssues( |
| 134 | + `[Passing periodic](${periodicCheck.html_url}) at ${commit.html_url}. Closing this issue.` |
| 135 | + ); |
| 136 | + } else if (periodicCheck.status === 'in_progress') { |
| 137 | + console.log( |
| 138 | + `Check is pending ${periodicCheck.html_url} for ${commit.html_url}. Retry again later.` |
| 139 | + ); |
| 140 | + } |
| 141 | + // error case |
| 142 | + else { |
| 143 | + createOrCommentIssue( |
| 144 | + 'Failing periodic', |
| 145 | + `[Failing periodic](${periodicCheck.html_url}) at ${commit.html_url}.` |
| 146 | + ); |
| 147 | + } |
| 148 | + // exit early as check was found |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | +
|
| 153 | + // no periodic-int-trigger checks found across all commits, report it |
| 154 | + createOrCommentIssue( |
| 155 | + 'Missing periodic', |
| 156 | + `Periodic test has not run in the past 24hrs. Last checked from ${ |
| 157 | + commits[0].html_url |
| 158 | + } to ${commits[commits.length - 1].html_url}.` |
| 159 | + ); |
0 commit comments