ci/github-script/commits: split review function into separate file

This allows re-using postReview in the next commit.

Changed files
+91 -71
ci
github-script
+6 -71
ci/github-script/commits.js
···
const { execFileSync } = require('node:child_process')
const { classify } = require('../supportedBranches.js')
const withRateLimit = require('./withRateLimit.js')
await withRateLimit({ github, core }, async (stats) => {
stats.prs = 1
···
// An empty results array will always trigger this condition, which is helpful
// to clean up reviews created by the prepare step when on the wrong branch.
if (results.every(({ severity }) => severity === 'info')) {
-
if (!dry) {
-
await Promise.all(
-
(
-
await github.paginate(github.rest.pulls.listReviews, {
-
...context.repo,
-
pull_number,
-
})
-
)
-
.filter((review) => review.user.login === 'github-actions[bot]')
-
.map(async (review) => {
-
if (review.state === 'CHANGES_REQUESTED') {
-
await github.rest.pulls.dismissReview({
-
...context.repo,
-
pull_number,
-
review_id: review.id,
-
message: 'All good now, thank you!',
-
})
-
}
-
await github.graphql(
-
`mutation($node_id:ID!) {
-
minimizeComment(input: {
-
classifier: RESOLVED,
-
subjectId: $node_id
-
})
-
{ clientMutationId }
-
}`,
-
{ node_id: review.node_id },
-
)
-
}),
-
)
-
}
return
}
···
const body = core.summary.stringify()
core.summary.write()
-
const pendingReview = (
-
await github.paginate(github.rest.pulls.listReviews, {
-
...context.repo,
-
pull_number,
-
})
-
).find(
-
(review) =>
-
review.user.login === 'github-actions[bot]' &&
-
// If a review is still pending, we can just update this instead
-
// of posting a new one.
-
(review.state === 'CHANGES_REQUESTED' ||
-
// No need to post a new review, if an older one with the exact
-
// same content had already been dismissed.
-
review.body === body),
-
)
-
-
if (dry) {
-
if (pendingReview)
-
core.info(`pending review found: ${pendingReview.html_url}`)
-
else core.info('no pending review found')
-
} else {
-
// Either of those two requests could fail for very long comments. This can only happen
-
// with multiple commits all hitting the truncation limit for the diff. If you ever hit
-
// this case, consider just splitting up those commits into multiple PRs.
-
if (pendingReview) {
-
await github.rest.pulls.updateReview({
-
...context.repo,
-
pull_number,
-
review_id: pendingReview.id,
-
body,
-
})
-
} else {
-
await github.rest.pulls.createReview({
-
...context.repo,
-
pull_number,
-
event: 'REQUEST_CHANGES',
-
body,
-
})
-
}
-
}
})
}
···
const { execFileSync } = require('node:child_process')
const { classify } = require('../supportedBranches.js')
const withRateLimit = require('./withRateLimit.js')
+
const { dismissReviews, postReview } = require('./reviews.js')
await withRateLimit({ github, core }, async (stats) => {
stats.prs = 1
···
// An empty results array will always trigger this condition, which is helpful
// to clean up reviews created by the prepare step when on the wrong branch.
if (results.every(({ severity }) => severity === 'info')) {
+
await dismissReviews({ github, context, dry })
return
}
···
const body = core.summary.stringify()
core.summary.write()
+
// Posting a review could fail for very long comments. This can only happen with
+
// multiple commits all hitting the truncation limit for the diff. If you ever hit
+
// this case, consider just splitting up those commits into multiple PRs.
+
await postReview({ github, context, core, dry, body })
})
}
+85
ci/github-script/reviews.js
···
···
+
async function dismissReviews({ github, context, dry }) {
+
const pull_number = context.payload.pull_request.number
+
+
if (dry) {
+
return
+
}
+
+
await Promise.all(
+
(
+
await github.paginate(github.rest.pulls.listReviews, {
+
...context.repo,
+
pull_number,
+
})
+
)
+
.filter((review) => review.user.login === 'github-actions[bot]')
+
.map(async (review) => {
+
if (review.state === 'CHANGES_REQUESTED') {
+
await github.rest.pulls.dismissReview({
+
...context.repo,
+
pull_number,
+
review_id: review.id,
+
message: 'All good now, thank you!',
+
})
+
}
+
await github.graphql(
+
`mutation($node_id:ID!) {
+
minimizeComment(input: {
+
classifier: RESOLVED,
+
subjectId: $node_id
+
})
+
{ clientMutationId }
+
}`,
+
{ node_id: review.node_id },
+
)
+
}),
+
)
+
}
+
+
async function postReview({ github, context, core, dry, body }) {
+
const pull_number = context.payload.pull_request.number
+
+
const pendingReview = (
+
await github.paginate(github.rest.pulls.listReviews, {
+
...context.repo,
+
pull_number,
+
})
+
).find(
+
(review) =>
+
review.user.login === 'github-actions[bot]' &&
+
// If a review is still pending, we can just update this instead
+
// of posting a new one.
+
(review.state === 'CHANGES_REQUESTED' ||
+
// No need to post a new review, if an older one with the exact
+
// same content had already been dismissed.
+
review.body === body),
+
)
+
+
if (dry) {
+
if (pendingReview)
+
core.info(`pending review found: ${pendingReview.html_url}`)
+
else core.info('no pending review found')
+
core.info(body)
+
} else {
+
if (pendingReview) {
+
await github.rest.pulls.updateReview({
+
...context.repo,
+
pull_number,
+
review_id: pendingReview.id,
+
body,
+
})
+
} else {
+
await github.rest.pulls.createReview({
+
...context.repo,
+
pull_number,
+
event: 'REQUEST_CHANGES',
+
body,
+
})
+
}
+
}
+
}
+
+
module.exports = {
+
dismissReviews,
+
postReview,
+
}