-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (82 loc) · 3 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const RegexParser = require("regex-parser");
const token = core.getInput('token');
const octokit = github.getOctokit(token);
const validateBody = (body) => {
const minOccurrences = core.getInput('minOccurrences');
const regex = RegexParser(core.getInput('regex'));
if (!new RegExp(regex)) {
throw new Error('Invalid regex');
}
if (minOccurrences < 0 || !minOccurrences.match(/\d+/g)) {
throw new Error('minOccurrences must be a positive number');
}
const occurrencesCount = (body.match(regex) || []).length;
return occurrencesCount < minOccurrences;
}
const sanitizeComment = async (body, commentId) => {
const isValid = validateBody(body);
if (!isValid) {
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
await octokit.rest.issues.deleteComment({
owner,
repo,
comment_id: commentId
});
}
}
const sanitizeIssue = async (body) => {
const isValid = validateBody(body);
if (!isValid) {
const title = core.getInput('title');
const body = core.getInput('body');
await octokit.rest.issues.update({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.issue.number,
state: 'closed',
title,
body
});
}
}
const main = async () => {
// check if runned by workflow_dispatch
if (github.context.eventName === 'workflow_dispatch') {
const issueId = github.context.payload.inputs.issueId;
if (!issueId) {
throw new Error('You must provide an issue id with manual runs');
} else {
// Check all comments from the issue
const comments = await octokit.rest.issues.listComments({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issueId
});
if (!comments.data.length) {
throw new Error('No comments found for this issue');
} else {
for await (const comment of comments.data) {
await sanitizeComment(comment.body, comment.id);
}
}
}
} else if (github.context.eventName === 'issue_comment') {
// Check only the comment that triggered the workflow
const body = github.context.payload.comment.body;
const commentId = github.context.payload.comment.id;
await sanitizeComment(body, commentId);
} else if (github.context.eventName === 'issues' && github.context.payload.action === 'opened') {
// Check on issue creation
await sanitizeIssue(github.context.payload.issue.body);
} else {
throw new Error('This action only works on issue comments and issue opened');
}
}
try {
main();
} catch (error) {
core.setFailed(error.message);
}