1async function dismissReviews({ github, context, dry }) {
2 const pull_number = context.payload.pull_request.number
3
4 if (dry) {
5 return
6 }
7
8 await Promise.all(
9 (
10 await github.paginate(github.rest.pulls.listReviews, {
11 ...context.repo,
12 pull_number,
13 })
14 )
15 .filter((review) => review.user?.login === 'github-actions[bot]')
16 .map(async (review) => {
17 if (review.state === 'CHANGES_REQUESTED') {
18 await github.rest.pulls.dismissReview({
19 ...context.repo,
20 pull_number,
21 review_id: review.id,
22 message: 'All good now, thank you!',
23 })
24 }
25 await github.graphql(
26 `mutation($node_id:ID!) {
27 minimizeComment(input: {
28 classifier: RESOLVED,
29 subjectId: $node_id
30 })
31 { clientMutationId }
32 }`,
33 { node_id: review.node_id },
34 )
35 }),
36 )
37}
38
39async function postReview({ github, context, core, dry, body }) {
40 const pull_number = context.payload.pull_request.number
41
42 const pendingReview = (
43 await github.paginate(github.rest.pulls.listReviews, {
44 ...context.repo,
45 pull_number,
46 })
47 ).find(
48 (review) =>
49 review.user?.login === 'github-actions[bot]' &&
50 // If a review is still pending, we can just update this instead
51 // of posting a new one.
52 (review.state === 'CHANGES_REQUESTED' ||
53 // No need to post a new review, if an older one with the exact
54 // same content had already been dismissed.
55 review.body === body),
56 )
57
58 if (dry) {
59 if (pendingReview)
60 core.info(`pending review found: ${pendingReview.html_url}`)
61 else core.info('no pending review found')
62 core.info(body)
63 } else {
64 if (pendingReview) {
65 await github.rest.pulls.updateReview({
66 ...context.repo,
67 pull_number,
68 review_id: pendingReview.id,
69 body,
70 })
71 } else {
72 await github.rest.pulls.createReview({
73 ...context.repo,
74 pull_number,
75 event: 'REQUEST_CHANGES',
76 body,
77 })
78 }
79 }
80}
81
82module.exports = {
83 dismissReviews,
84 postReview,
85}