forked from probot/metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (54 loc) · 1.58 KB
/
index.js
File metadata and controls
70 lines (54 loc) · 1.58 KB
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
const regex = /\n\n<!-- probot = (.*) -->/
const secondRegex = /:([^;]*)}/
module.exports = (context, issue = null) => {
const github = 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, number} = issue
return github.issues.update({owner, repo, number, body})
},
async getAll () {
let body = issue.body
if (!body) {
body = (await github.issues.get(issue)).data.body || ''
}
const all = body.match(regex)
if (all) {
const fullSet = all[1].match(secondRegex)
const data = JSON.parse(fullSet[1])
const dict = {}
for (let i in data) {
dict[i] = data[i]
}
return dict
}
}
}
}