|
1 | | -const regex = /(\n\n|\r\n)<!-- probot = (.*) -->/ |
| 1 | +const probotMetadataRegex = /(?:\n\n|\r\n)<!-- probot = (.*) -->/ |
| 2 | +const prototypePollutionKeys = /** @type {const} */(['__proto__', 'constructor', 'prototype']) |
2 | 3 |
|
3 | | -module.exports = (context, issue = null) => { |
4 | | - const github = context.octokit || context.github |
5 | | - const prefix = context.payload.installation.id |
| 4 | +const metadata = /** @type {ProbotMetadataConstructor} */ (context, issue) => { |
| 5 | + const octokit = context.octokit |
| 6 | + const prefix = /** @type {{ id: number; node_id: string; }} */ (context.payload.installation).id |
6 | 7 |
|
7 | 8 | if (!issue) issue = context.issue() |
8 | 9 |
|
9 | 10 | return { |
10 | | - async get (key = null) { |
| 11 | + async get (key) { |
11 | 12 | let body = issue.body |
12 | 13 |
|
13 | 14 | if (!body) { |
14 | | - body = (await github.issues.get(issue)).data.body || '' |
| 15 | + body = (await octokit.rest.issues.get(issue)).data.body || '' |
15 | 16 | } |
16 | 17 |
|
17 | | - const match = body.match(regex) |
| 18 | + const match = body.match(probotMetadataRegex) |
18 | 19 |
|
19 | 20 | if (match) { |
20 | | - const data = JSON.parse(match[2])[prefix] |
21 | | - return key ? data && data[key] : data |
| 21 | + const probotMetadata = JSON.parse(match[1]) |
| 22 | + const data = probotMetadata[prefix] |
| 23 | + return typeof key === 'string' || typeof key === 'number' |
| 24 | + ? data && data[key] |
| 25 | + : data |
22 | 26 | } |
23 | 27 | }, |
24 | 28 |
|
25 | 29 | async set (key, value) { |
26 | 30 | let body = issue.body |
27 | | - let data = {} |
| 31 | + /** @type {Record<number, Record<number|string, any>>} */ |
| 32 | + let data = Object.create(null) |
28 | 33 |
|
29 | | - if (!body) body = (await github.issues.get(issue)).data.body || '' |
| 34 | + if (!body) body = (await octokit.rest.issues.get(issue)).data.body || '' |
30 | 35 |
|
31 | | - const match = body.match(regex) |
| 36 | + const match = body.match(probotMetadataRegex) |
32 | 37 |
|
33 | 38 | if (match) { |
34 | | - data = JSON.parse(match[2]) |
| 39 | + data = JSON.parse(match[1]) |
35 | 40 | } |
36 | 41 |
|
37 | | - body = body.replace(regex, '') |
| 42 | + body = body.replace(probotMetadataRegex, '') |
38 | 43 |
|
39 | | - if (!data[prefix]) data[prefix] = {} |
| 44 | + if (!data[prefix]) data[prefix] = Object.create(null) |
40 | 45 |
|
41 | | - if (typeof key === 'object') { |
42 | | - Object.assign(data[prefix], key) |
43 | | - } else { |
| 46 | + // should never happen, but just in case |
| 47 | + if (typeof prefix === 'string' && prototypePollutionKeys.includes(prefix)) { |
| 48 | + throw new TypeError('Invalid prefix value') |
| 49 | + } |
| 50 | + if (typeof key === 'string' || typeof key === 'number') { |
44 | 51 | data[prefix][key] = value |
| 52 | + } else { |
| 53 | + Object.assign(data[prefix], key) |
45 | 54 | } |
46 | 55 |
|
47 | | - body = `${body}\n\n<!-- probot = ${JSON.stringify(data)} -->` |
| 56 | + await octokit.rest.issues.update({ |
| 57 | + owner: issue.owner, |
| 58 | + repo: issue.repo, |
| 59 | + issue_number: issue.issue_number, |
| 60 | + body: `${body}\n\n<!-- probot = ${JSON.stringify(data)} -->` |
| 61 | + }) |
48 | 62 |
|
49 | | - const { owner, repo, issue_number } = issue |
50 | | - return github.issues.update({ owner, repo, issue_number, body }) |
| 63 | + issue.body = `${body}\n\n<!-- probot = ${JSON.stringify(data)} -->` |
51 | 64 | } |
52 | 65 | } |
53 | 66 | } |
| 67 | + |
| 68 | +export default metadata |
| 69 | +export { metadata } |
| 70 | + |
| 71 | +/** @typedef {string|number} StringOrNumber */ |
| 72 | +/** @typedef {Record<string, StringOrNumber>|StringOrNumber|StringOrNumber[]} Key */ |
| 73 | +/** @typedef {Key} Value */ |
| 74 | + |
| 75 | +/** |
| 76 | + * @typedef {object} IssueOption |
| 77 | + * @property {string} owner |
| 78 | + * @property {string} repo |
| 79 | + * @property {number} issue_number |
| 80 | + * @property {string} [body] |
| 81 | + */ |
| 82 | + |
| 83 | +/** |
| 84 | + * @typedef {object} ProbotMetadata |
| 85 | + * @property {(key?: Key)=>Promise<Value|undefined>} get |
| 86 | + * @property {(key?: Key, value?: Value)=>Promise<void>} set |
| 87 | + */ |
| 88 | + |
| 89 | +/** @typedef {(context: import('probot').Context<'issue_comment'>, issue?: IssueOption)=>ProbotMetadata} ProbotMetadataConstructor */ |
0 commit comments