-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (48 loc) · 1.7 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
const core = require('@actions/core')
const github = require('@actions/github')
const apis = require('./apis')
const actionTag = "<!--okami-action-->"
async function run() {
const context = github.context
if (context.payload.pull_request == null) {
console.error("Not currently running in a pull request")
return
}
var animal = core.getInput('animal-type')
const image = await apis.getAnimalImageUrl(animal)
if (image === "") {
core.setFailed("No image url found! Not posting a comment")
} else {
postImageToPR(context, image)
}
}
async function postImageToPR(context, picture) {
const token = core.getInput('okami-token')
const shouldUpdateImage = core.getInput('update-image')
const octokit = github.getOctokit(token)
const commentBody = `${actionTag}\n![](${picture})`
const prNumber = context.payload.pull_request.number
const commentsResponse = await octokit.issues.listComments({
...context.repo,
issue_number: prNumber
})
const findResult = commentsResponse.data.find(element => element.body.includes(actionTag))
if (shouldUpdateImage && findResult != undefined) {
console.log("Updating comment with: " + commentBody)
await octokit.issues.updateComment({
...context.repo,
comment_id: findResult.id,
body: commentBody
})
console.log("Finished updateComment")
} else {
console.log("Creating comment with: " + commentBody)
await octokit.issues.createComment({
...context.repo,
issue_number: prNumber,
body: commentBody
})
console.log("Finished createComment")
}
}
run()