-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.ts
111 lines (99 loc) · 2.87 KB
/
main.ts
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
import * as core from '@actions/core'
import * as github from '@actions/github'
import {inspect} from 'util'
interface Inputs {
token: string
repository: string
issueNumber: number
commentAuthor: string
bodyIncludes: string
bodyRegex: string
direction: string
}
interface Comment {
id: number
body?: string
user: {
login: string
} | null
}
function findCommentPredicate(inputs: Inputs, comment: Comment): boolean {
return (
(inputs.commentAuthor && comment.user
? comment.user.login === inputs.commentAuthor
: true) &&
((inputs.bodyIncludes && comment.body
? comment.body.includes(inputs.bodyIncludes)
: true) ||
(inputs.bodyRegex && comment.body
? isMatchedRegex(comment.body, inputs.bodyRegex)
: true))
)
}
function isMatchedRegex(body: string, regex: string): boolean {
return (body ? body.match(regex) !== null : true) || (!body ? false : false)
}
async function findComment(inputs: Inputs): Promise<Comment | undefined> {
const octokit = github.getOctokit(inputs.token)
const [owner, repo] = inputs.repository.split('/')
const parameters = {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber
}
if (inputs.direction == 'first') {
for await (const {data: comments} of octokit.paginate.iterator(
octokit.rest.issues.listComments,
parameters
)) {
// Search each page for the comment
const comment = comments.find(comment =>
findCommentPredicate(inputs, comment)
)
if (comment) return comment
}
} else {
// direction == 'last'
const comments = await octokit.paginate(
octokit.rest.issues.listComments,
parameters
)
comments.reverse()
const comment = comments.find(comment =>
findCommentPredicate(inputs, comment)
)
if (comment) return comment
}
return undefined
}
function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}
async function run(): Promise<void> {
try {
const inputs: Inputs = {
token: core.getInput('token'),
repository: core.getInput('repository'),
issueNumber: Number(core.getInput('issue-number')),
commentAuthor: core.getInput('comment-author'),
bodyIncludes: core.getInput('body-includes'),
direction: core.getInput('direction')
}
core.debug(`Inputs: ${inspect(inputs)}`)
const comment = await findComment(inputs)
if (comment) {
core.setOutput('comment-id', comment.id.toString())
core.setOutput('comment-body', comment.body)
core.setOutput('comment-author', comment.user ? comment.user.login : '')
} else {
core.setOutput('comment-id', '')
core.setOutput('comment-body', '')
core.setOutput('comment-author', '')
}
} catch (error) {
core.debug(inspect(error))
core.setFailed(getErrorMessage(error))
}
}
run()