This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
227 lines (210 loc) · 6.04 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
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
const core = require('@actions/core')
const github = require('@actions/github')
const fs = require('fs')
const fsPromise = fs.promises
const text2png = require('text2png')
const fetch = require('node-fetch')
const FormData = require('form-data')
const execute = require('@actions/exec')
const remark = require('remark')
const strip = require('strip-markdown')
async function dependencies () {
try {
await execute.exec('sudo', ['apt-get', 'install', '-y', 'espeak'])
await execute.exec('wget', [
'-O',
'OpenDyslexic-Regular.otf',
'https://raw.githubusercontent.com/antijingoist/opendyslexic/master/compiled/OpenDyslexic-Regular.otf'
])
} catch (error) {
core.setFailed(error.message)
return false
}
}
function speak (text) {
const { exec } = require('child_process')
fs.writeFileSync('body.txt', text)
const outputFile = 'tts.wav'
const voice = 'en-uk-north'
const command = `espeak -v ${voice} -f body.txt --stdout > ${outputFile}`
return new Promise(resolve => {
exec(command, (err, stout, sterr) => {
if (err) {
core.setFailed(err.message)
throw err
}
resolve(err ? stout : sterr)
})
})
}
function stripMd (input) {
return String(
remark()
.use(strip)
.processSync(input, { keep: ['code'] })
)
}
async function saveImage (input) {
const success = true
try {
await fsPromise.appendFile(
'image.png',
text2png(input, {
font: '30px OpenDyslexic',
backgroundColor: 'white',
localFontPath: './OpenDyslexic-Regular.otf',
localFontName: 'OpenDyslexic'
})
)
} catch (err) {
core.setFailed(err.message)
throw err
}
return success
}
async function getServer () {
const url = 'https://apiv2.gofile.io/getServer'
try {
const response = await fetch(url)
const json = await response.json()
if (json.status === 'ok') {
return `https://${json.data.server}.gofile.io/uploadFile`
} else {
core.setFailed('[ERROR]: GoFile returned ' + json.status)
return process.exit(1)
}
} catch (err) {
core.setFailed(err.message)
throw err
}
}
async function upload (file, filename, url) {
const buffer = await fsPromise.readFile('./' + file)
try {
const form = new FormData()
form.append('file', buffer, {
name: 'file',
filename: filename
})
const response = await fetch(url, {
method: 'POST',
body: form
})
const json = await response.json()
if (json.status === 'ok') {
return `https://gofile.io/d/${json.data.code}`
} else {
core.setFailed('[ERROR]: GoFile returned ' + json.status)
return process.exit(1)
}
} catch (err) {
core.setFailed(err.message)
throw err
}
}
function createComment (type, number, body) {
const token = core.getInput('token')
const octokit = github.getOctokit(token)
const octokitBody = {}
octokitBody.owner = github.context.payload.repository.owner.login
octokitBody.repo = github.context.payload.repository.name
octokitBody.body = body
if (type === 'issues') {
octokitBody.issue_number = number
return octokit.issues.createComment(octokitBody)
} else if (type === 'issue_comment') {
octokitBody.issue_number = number
return octokit.issues.createComment(octokitBody)
} else if (type === 'pull_request') {
octokitBody.pull_number = number
octokitBody.event = 'COMMENT'
return octokit.pulls.createReview(octokitBody)
}
}
function replyBody (imageUrl, audioUrl, extra) {
return (
'Accessibility Links:\n[Audio](' +
audioUrl +
')\n[Image](' +
imageUrl +
')\n' +
extra
)
}
async function run () {
try {
await dependencies()
const type = github.context.eventName
let comment
let commentNum
let extra = ''
if (
github.context.eventName === 'issues' &&
github.context.payload.action === 'opened'
) {
const issue = github.context.payload.issue
const issueTitle = issue.title
const issueBody = issue.body
commentNum = issue.number
comment =
'Issue Title: ' +
issueTitle +
'\nIssue Body: ' +
(await stripMd(issueBody))
extra =
'The action was triggered by the creation of this issue by [' +
github.context.payload.sender.login +
'](' +
github.context.payload.sender.html_url +
').'
} else if (
github.context.eventName === 'issue_comment' &&
github.context.payload.action === 'created'
) {
const issueComment = github.context.payload.comment
const issueBody = issueComment.body
commentNum = github.context.payload.issue.number
comment = 'Issue Comment: ' + (await stripMd(issueBody))
extra =
'The action was triggered by [this comment](' +
issueComment.html_url +
') from [' +
github.context.payload.sender.login +
'](' +
github.context.payload.sender.html_url +
').'
} else if (
github.context.eventName === 'pull_request' &&
github.context.payload.action === 'opened'
) {
const pullRequest = github.context.payload.pull_request
const prTitle = pullRequest.title
const prBody = pullRequest.body
commentNum = pullRequest.number
comment =
'Pull Request Title: ' +
prTitle +
'.\nPull Request Body: ' +
(await stripMd(prBody))
extra =
'The action was triggered by the creation of this pull request by [' +
github.context.payload.sender.login +
'](' +
github.context.payload.sender.html_url +
').'
} else {
core.info('This is not an event Accessibility handles')
return process.exit(0)
}
await speak(comment)
await saveImage(comment)
const url = await getServer()
const audioUrl = await upload('tts.wav', 'tts.wav', url)
const imageUrl = await upload('image.png', 'image.png', url)
await createComment(type, commentNum, replyBody(imageUrl, audioUrl, extra))
} catch (err) {
core.setFailed(err.message)
throw err
}
}
run()