-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
138 lines (114 loc) · 2.99 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
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
import { context, getOctokit } from '@actions/github'
const core = require('@actions/core')
const { promises: fs } = require('fs')
const path = require('path')
const getInputs = () => ({
message: core.getInput('message'),
file: core.getInput('file'),
singleComment: core.getInput('single_comment') === 'true',
identifier: core.getInput('identifier'),
githubToken: core.getInput('github_token') || process.env.GITHUB_TOKEN
})
const getIdentifier = () => {
const { identifier } = getInputs()
if (!identifier) {
throw new Error('Identifier should not be an empty string, identifier is optional.')
}
return `<!-- ${identifier} -->`
}
const getFileContent = async () => {
const { file } = getInputs()
if (!file) {
return null
}
const filePath = path.resolve(process.cwd(), `.github/workflows/${file}`)
const content = await fs.readFile(filePath, 'utf8')
if (!content) {
return null
}
return content.replace(/{\w+}/g, (key) => {
const envKey = key.substring(1, key.length - 1)
if (process.env[envKey]) {
return process.env[envKey]
}
return key
})
}
const getMessage = async () => {
const { message } = getInputs()
let body
if (!message) {
body = await getFileContent()
} else {
body = message
}
if (!body) {
throw new Error('You need to provide message or file input')
}
return `${getIdentifier()}\n${body}`
}
const findComment = async (client) => {
const comments = await client.rest.issues
.listComments({
owner: context.issue.owner,
repo: context.issue.repo,
issue_number: context.issue.number
})
const identifier = getIdentifier()
for (const comment of comments.data) {
if (comment.body.startsWith(identifier)) {
return comment.id
}
}
return null
}
const getClient = () => {
const { githubToken } = getInputs()
if (!githubToken) {
throw new Error('No github token provided')
}
if (!context.issue.number) {
throw new Error('This is not a PR or commenting is disabled.')
}
const client = getOctokit(githubToken)
if (!client) {
throw new Error('Client couldn\'t be created, make sure that token is correct.')
}
return client
}
const comment = async (client) => {
const { singleComment } = getInputs()
let commentId = null
if (singleComment) {
commentId = await findComment(client)
}
const body = await getMessage()
if (commentId) {
await client.rest.issues
.updateComment({
owner: context.issue.owner,
repo: context.issue.repo,
comment_id: commentId,
body
})
core.setOutput('commented', 'true')
return
}
await client.rest.issues
.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body
})
}
const run = async () => {
try {
await comment(getClient())
core.setOutput('commented', 'true')
} catch (error) {
core.setOutput('commented', 'false')
core.setFailed(error.message)
}
}
run()