forked from marcmac/automatic-pull-request-review
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
103 lines (89 loc) · 2.42 KB
/
index.ts
File metadata and controls
103 lines (89 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as core from '@actions/core'
import * as github from '@actions/github'
const token = core.getInput('repo-token')
const requestEvent = core.getInput('event')
const body = core.getInput('body')
const octokit = github.getOctokit(token)
if (
(requestEvent === 'COMMENT'
|| requestEvent === 'REQUEST_CHANGES'
|| requestEvent === 'DISMISS') &&
!body
) {
core.setFailed('Event types DISMISS, COMMENT & REQUEST_CHANGES require a body.')
}
const pullRequest = github.context.payload['pull_request']
const repository = github.context.payload['repository']
const repoOwner = repository?.owner?.login
const repoName = repository?.name
if (!pullRequest || !repository) {
core.setFailed('This action is meant to be run on pull requests')
}
if (
requestEvent === 'DISMISS'
) {
const getReviews = `
query {
repository(owner: "${repoOwner}", name: "${repoName}") {
pullRequest(number: ${(<any>pullRequest)['number']}) {
id
number
title
reviews(last: 100, states: APPROVED, author: "github-actions[bot]") {
nodes {
author {
login
}
state
id
}
}
}
}
viewer {
login
id
}
}`;
core.debug(`getReviews: ${getReviews}`)
octokit.graphql(getReviews).then((result) => {
core.debug(`result: ${JSON.stringify(result, null, 2)}`)
const reviews = Object(result).repository.pullRequest.reviews.nodes
for (const review of reviews) {
const reviewId = review["id"]
core.info(`Dismissing ${reviewId}`)
const dismissReview = `
mutation {dismissPullRequestReview(input: {
pullRequestReviewId: "${reviewId}",
message: "${body}"
}){clientMutationId}
}
`;
core.debug(dismissReview)
octokit.graphql(dismissReview).catch((err) => {
core.info(dismissReview)
core.error(err)
core.setFailed(err.message)
});
}
}).catch((err) => {
core.info(getReviews)
core.error(err)
core.setFailed(err.message)
});
} else {
const query = `
mutation {
addPullRequestReview(input: {
pullRequestId: "${(<any>pullRequest)['node_id']}",
event: ${requestEvent},
body: "${body}"
}) {clientMutationId}
}`;
core.info(`Query: ${query}`)
octokit.graphql(query).catch((err) => {
core.info(query)
core.error(err)
core.setFailed(err.message)
});
}