-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
53 lines (38 loc) · 1.21 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
const regex = /(\n\n|\r\n)<!-- probot = (.*) -->/
module.exports = (context, issue = null) => {
const github = context.octokit || context.github
const prefix = context.payload.installation.id
if (!issue) issue = context.issue()
return {
async get (key = null) {
let body = issue.body
if (!body) {
body = (await github.issues.get(issue)).data.body || ''
}
const match = body.match(regex)
if (match) {
const data = JSON.parse(match[2])[prefix]
return key ? data && data[key] : data
}
},
async set (key, value) {
let body = issue.body
let data = {}
if (!body) body = (await github.issues.get(issue)).data.body || ''
const match = body.match(regex)
if (match) {
data = JSON.parse(match[2])
}
body = body.replace(regex, '')
if (!data[prefix]) data[prefix] = {}
if (typeof key === 'object') {
Object.assign(data[prefix], key)
} else {
data[prefix][key] = value
}
body = `${body}\n\n<!-- probot = ${JSON.stringify(data)} -->`
const { owner, repo, issue_number } = issue
return github.issues.update({ owner, repo, issue_number, body })
}
}
}