forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr.js
More file actions
99 lines (85 loc) · 2.26 KB
/
Copy pathpr.js
File metadata and controls
99 lines (85 loc) · 2.26 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
/* eslint-disable unicorn/no-abusive-eslint-disable -- Uses globals */
/* eslint-disable -- Uses globals */
export async function blockAi({github, context, core}) {
const marker = "This looks like an AI-generated PR, so we're preemptively closing it.";
const pr = context.payload.pull_request;
const {owner, repo} = context.repo;
const {data: comments} = await github.rest.issues.listComments({
owner,
repo,
issue_number: pr.number,
per_page: 5,
});
const alreadyProcessed = comments.some(
comment =>
comment.user?.type === 'Bot'
&& comment.body?.startsWith(marker),
);
if (alreadyProcessed) {
core.info(`PR #${pr.number} was already processed; skipping.`);
return;
}
const isAi = pr.title.startsWith('AI:')
|| /npx|build:bundle|build:typescript|Verification|Validation|Summary|Changes|Testing|—/.test(pr.body ?? '');
if (!isAi) {
core.info(`PR #${pr.number} did not match the AI detection rules.`);
return;
}
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'closed',
title: 'AI SPAM',
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body:
`${marker} If you're human and tested it, include a screenshot/video/gif of the working PR and we can reopen the PR. Don't open more PRs until this one is resolved.`,
});
}
export async function inheritLabels({github, context, core}) {
const allowed = ['bug', 'enhancement', 'meta'];
const {owner, repo} = context.repo;
const pr = context.payload.pull_request;
const query = `
query($owner:String!, $repo:String!, $number:Int!) {
repository(owner:$owner, name:$repo) {
pullRequest(number:$number) {
closingIssuesReferences(first: 10) {
nodes {
labels(first: 20) {
nodes { name }
}
}
}
}
}
}
`;
const res = await github.graphql(query, {
owner,
repo,
number: pr.number,
});
const labels = new Set();
for (
const issue of res.repository.pullRequest.closingIssuesReferences.nodes
) {
for (const label of issue.labels.nodes) {
if (allowed.includes(label.name)) {
labels.add(label.name);
}
}
}
if (labels.size > 0) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pr.number,
labels: [...labels],
});
}
}