-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add periodic reporter #86
Changes from all commits
13738d9
2d65ff8
ab62df7
f99fbc6
954968a
c8161eb
ac16150
ae8a23f
f2d5e15
7dbe3fa
52a76a5
d82140e
7a3fe13
58f2c56
fc8ca05
da18428
9ab766d
798f349
a3fd49b
840e0ef
c4e9a31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,147 @@ | ||||||
# Copyright 2023 Google LLC | ||||||
# | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
# You may obtain a copy of the License at | ||||||
# | ||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||
# | ||||||
# Unless required by applicable law or agreed to in writing, software | ||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
|
||||||
name: 'reporter' | ||||||
|
||||||
on: | ||||||
schedule: | ||||||
# 2 hours after scheduled periodic and once again in the evening | ||||||
- cron: '0 5,17 * * *' | ||||||
workflow_dispatch: | ||||||
|
||||||
jobs: | ||||||
report: | ||||||
permissions: | ||||||
issues: 'write' | ||||||
|
||||||
runs-on: 'ubuntu-latest' | ||||||
|
||||||
steps: | ||||||
- uses: 'actions/github-script@v6' | ||||||
with: | ||||||
script: |- | ||||||
// label for all issues opened by reporter | ||||||
const periodicLabel = 'periodic-failure'; | ||||||
|
||||||
// check if any reporter opened any issues previously | ||||||
const prevIssues = await github.paginate(github.rest.issues.listForRepo, { | ||||||
owner: 'GoogleCloudPlatform', | ||||||
repo: 'terraform-google-secure-cicd', | ||||||
state: 'open', | ||||||
labels: [periodicLabel] | ||||||
}); | ||||||
// createOrCommentIssue creates a new issue or comments on an existing issue. | ||||||
const createOrCommentIssue = async function (title, txt) { | ||||||
if (prevIssues.length < 1) { | ||||||
console.log('no previous issues found, creating one'); | ||||||
await github.rest.issues.create({ | ||||||
...context.repo, | ||||||
title: title, | ||||||
body: txt, | ||||||
labels: [periodicLabel] | ||||||
}); | ||||||
return; | ||||||
} | ||||||
console.log(`found previous issue ${prevIssues[0].html_url}, adding comment`); | ||||||
await github.rest.issues.createComment({ | ||||||
...context.repo, | ||||||
issue_number: prevIssues[0].number, | ||||||
body: txt | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future, it might be useful to include a warning in the update if multiple issues were returned |
||||||
}); | ||||||
}; | ||||||
|
||||||
// updateAndCloseIssue comments on an existing issue and closes it. No-op if no issue exists. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit for the future: this function operates on all matching issues
Suggested change
|
||||||
const updateAndCloseIssue = async function (txt) { | ||||||
if (prevIssues.length < 1) { | ||||||
console.log('no previous issues found, skipping close'); | ||||||
return; | ||||||
} | ||||||
for (const prevIssue of prevIssues) { | ||||||
console.log(`found previous issue ${prevIssue.html_url}, adding comment`); | ||||||
await github.rest.issues.createComment({ | ||||||
...context.repo, | ||||||
issue_number: prevIssue.number, | ||||||
body: txt | ||||||
}); | ||||||
console.log(`closing ${prevIssue.html_url}`); | ||||||
await github.rest.issues.update({ | ||||||
...context.repo, | ||||||
issue_number: prevIssue.number, | ||||||
body: txt, | ||||||
state: "closed", | ||||||
}); | ||||||
} | ||||||
}; | ||||||
|
||||||
// Find status of check runs. | ||||||
// We will find check runs for each commit and then filter for the periodic. | ||||||
// Checks API only allows for ref and if we use main there could be edge cases where | ||||||
// the check run happened on a SHA that is different from head. | ||||||
const commits = await github.paginate(github.rest.repos.listCommits, { | ||||||
...context.repo | ||||||
}); | ||||||
|
||||||
var foundCheck = false; | ||||||
let periodicCheck = {}; | ||||||
|
||||||
for (const commit of commits) { | ||||||
console.log( | ||||||
`checking runs at ${commit.html_url}: ${commit.commit.message}` | ||||||
); | ||||||
const checks = await github.rest.checks.listForRef({ | ||||||
...context.repo, | ||||||
ref: commit.sha | ||||||
}); | ||||||
// find runs for this commit | ||||||
for (const check of checks.data.check_runs) { | ||||||
console.log(`found run ${check.name} for ${commit.html_url}`); | ||||||
if (check.name.includes('periodic-int-trigger')) { | ||||||
foundCheck = true; | ||||||
periodicCheck = check; | ||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
if (foundCheck) { | ||||||
if ( | ||||||
periodicCheck.status === 'completed' && | ||||||
periodicCheck.conclusion === 'success' | ||||||
) { | ||||||
updateAndCloseIssue( | ||||||
`[Passing periodic](${periodicCheck.html_url}) at ${commit.html_url}. Closing this issue.` | ||||||
); | ||||||
} else if (periodicCheck.status === 'in_progress') { | ||||||
console.log( | ||||||
`Check is pending ${periodicCheck.html_url} for ${commit.html_url}. Retry again later.` | ||||||
); | ||||||
} | ||||||
// error case | ||||||
else { | ||||||
createOrCommentIssue( | ||||||
'Failing periodic', | ||||||
`[Failing periodic](${periodicCheck.html_url}) at ${commit.html_url}.` | ||||||
); | ||||||
} | ||||||
// exit early as check was found | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
||||||
// no periodic-int-trigger checks found across all commits, report it | ||||||
createOrCommentIssue( | ||||||
'Missing periodic', | ||||||
`Periodic test has not run in the past 24hrs. Last checked from ${ | ||||||
commits[0].html_url | ||||||
} to ${commits[commits.length - 1].html_url}.` | ||||||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the future, might want to consider filtering on
user.login
and alsotitle
to minimize someone adding the label to a issue and it being returned.