-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathos_probot_metadata.js
58 lines (43 loc) · 1.37 KB
/
os_probot_metadata.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
/**
* Based on probot-metadata - https://github.com/probot/metadata
*/
const regex = /\n\n<!-- probot = (.*) -->/
const { Octokit } = require("@octokit/action")
const octokit = new Octokit()
module.exports = (context, issue = null) => {
console.log(context)
const prefix = "onesignal-probot"
if (!issue) issue = context.payload.issue
return {
async get (key = null) {
let body = issue.body
if (!body) {
body = (await octokit.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 octokit.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] = process.env.GITHUB_REPOSITORY.split("/")
const issue_number = context.payload.issue.number
return octokit.issues.update({ owner, repo, issue_number, body })
}
}
}