forked from probot/metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (37 loc) · 1.17 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
const regex = /\n\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[1])[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 || ''
body = body.replace(regex, (_, json) => {
data = JSON.parse(json)
return ''
})
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 })
}
}
}