-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks.ts
More file actions
258 lines (221 loc) · 6.1 KB
/
checks.ts
File metadata and controls
258 lines (221 loc) · 6.1 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { GuildMember, ThreadChannel } from 'discord.js'
import { gql, GraphQLClient } from 'graphql-request'
import { genericLog } from '../communication/thread'
import config from '../config'
import { query } from '../db/client'
import { ApiSubmission, ValidatedSubmission } from '../types/submission'
import { runCatching } from '../utils/request'
import { Submission } from '@prisma/client'
interface RequiredValuesOk {
author: GuildMember
error: false
}
interface RequiredValuesErr {
message: string
error: true
}
export type RequiredValuesResult = RequiredValuesOk | RequiredValuesErr
// The check functions here will handle the reporting of errors to the user
// but will not handle the cleanup / any further operations after that.
// Those should be handled by the caller.
export async function resolveRequiredValues (
submission: ApiSubmission,
reviewThread: ThreadChannel
): Promise<RequiredValuesResult> {
const guild = config.guilds().current
try {
const member = await guild.members.fetch(submission.authorId)
// d.js has been observed to return undefined in some cases here, for unknown reasons.
// the types do not specify this, so it's not known why this happens.
// check it for sanity.
if (!member?.user) {
genericLog.error({
type: 'text',
content: `Could not locate author for this submission (${submission.authorId})`,
ctx: reviewThread
})
return {
error: true,
message: `Unknown user ${submission.authorId}`
}
}
return {
error: false,
author: member
}
} catch (err) {
genericLog.error({
type: 'text',
content: `Could not locate author for this submission (${submission.authorId})`,
ctx: reviewThread
})
return {
error: true,
message: (err as Error).message
}
}
}
/**
* Runs the non critical checks.
* The result of these checks is not critical for application function.
* Returns `true` if the checks passed, `false` otherwise.
*/
export async function runNonCriticalChecks (
submission: ValidatedSubmission
): Promise<boolean> {
let result = true
const duplicateSubmissions = await listDuplicates(submission)
const isDuplicate = duplicateSubmissions.length > 0
if (isDuplicate) {
let duplicateContent = 'Possible duplicate submission detected, matched submission links. Matches:\n'
for (const duplicate of duplicateSubmissions) {
duplicateContent += ` ${duplicate.name} [${duplicate.sourceLinks}] (<#${duplicate.reviewThreadId}>)\n`
}
genericLog.warning({
type: 'text',
content: duplicateContent,
ctx: submission.reviewThread
})
result = false
}
if (!submission.author.roles.cache.has(config.roles().establishedMember)) {
genericLog.warning({
type: 'text',
content: 'Submitter does not appear to have Established Member role. Check and reject as neccesary.',
ctx: submission.reviewThread
})
result = false
}
if (isGitHubSource(submission)) {
const licenseRes = await runGitHubChecks(submission)
if (licenseRes.outcome !== 'success') {
genericLog.warning({
type: 'text',
content: licenseRes.message,
ctx: submission.reviewThread
})
result = false
} else {
genericLog.info({
type: 'text',
content: licenseRes.message,
ctx: submission.reviewThread
})
}
}
return result
}
async function listDuplicates (
submission: ValidatedSubmission
): Promise<Submission[]> {
const duplicates = await query(async (db) =>
await db.submission.findMany({
where: {
AND: [
{
sourceLinks: {
contains: submission.links.source
}
},
{
otherLinks: {
contains: submission.links.other
}
}
]
},
// Order by the submission date
orderBy: {
submittedAt: 'desc'
}
})
)
// We insert the submission before this code runs, so we slice the first element off.
return duplicates.slice(1)
}
const ghClient = new GraphQLClient('https://api.github.com/graphql')
interface GitHubResult {
outcome:
| 'error'
| 'success'
| 'invalid-license'
| 'empty-repository'
| 'archived-repository'
| 'locked-repository'
message: string
}
function isGitHubSource (submission: ValidatedSubmission): boolean {
return submission.links.source.includes('github.com')
}
async function runGitHubChecks (
submission: ValidatedSubmission
): Promise<GitHubResult> {
const ghQuery = gql`
query ($url: URI!) {
resource(url: $url) {
... on Repository {
licenseInfo {
spdxId
}
isEmpty
isArchived
isDisabled
isLocked
lockReason
}
}
}
`
const res: undefined | any = await runCatching(
async () =>
await ghClient.request(
ghQuery,
{ url: submission.links.source },
{ authorization: `Bearer ${config.github().token}` }
),
'suppress'
)
if (!res) {
return {
outcome: 'error',
message: 'Failed to run GitHub checks, API returned an error.'
}
}
const data = res.resource
if (!data) {
return {
outcome: 'error',
message:
"GitHub reported no data on this submission, repository likely doesn't exist or is private."
}
}
if (data.isEmpty) {
return {
outcome: 'empty-repository',
message: 'GitHub reports this repository as being empty.'
}
}
if (data.isArchived) {
return {
outcome: 'archived-repository',
message: 'GitHub reports this repository as being archived.'
}
}
if (data.isLocked) {
return {
outcome: 'locked-repository',
message: `GitHub reports this repository as being locked (${data.lockReason}).`
}
}
const hasValidLicense = data?.licenseInfo
if (!hasValidLicense) {
return {
outcome: 'invalid-license',
message: 'GitHub reports no valid SPDX license for the project.'
}
}
return {
outcome: 'success',
message: 'GitHub checks passed.'
}
}