-
Notifications
You must be signed in to change notification settings - Fork 3
/
issue-to-json.js
51 lines (36 loc) · 1.41 KB
/
issue-to-json.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
import { writeFile, mkdir } from "node:fs/promises";
import { createHash } from "node:crypto";
import path from "node:path";
import { getInput, exportVariable, setFailed } from "@actions/core";
import * as github from "@actions/github";
import { parseIssueBody } from "./parse-issue-body.js";
function getFileName(url) {
let hash = createHash("sha256");
hash.update(url);
return hash.digest("base64url").substr(0, 10) + ".json";
}
export async function issueToJson() {
try {
const outputDir = getInput("folder");
if (!github.context.payload.issue) {
setFailed("Cannot find GitHub issue");
return;
}
let issueTemplatePath = path.join("./.github/ISSUE_TEMPLATE/", getInput("issue-template"));
let { title, number, body, user } = github.context.payload.issue;
if (!title || !body) {
throw new Error("Unable to parse GitHub issue.");
}
let issueData = await parseIssueBody(issueTemplatePath, body);
issueData.opened_by = user.login;
exportVariable("IssueNumber", number);
// create output dir
await mkdir(outputDir, { recursive: true });
let hashPropertyName = getInput("hash-property-name");
let fileName = getFileName(issueData[ hashPropertyName ]); // usually .url
await writeFile(path.join(outputDir, fileName), JSON.stringify(issueData, null, 2));
} catch (error) {
setFailed(error.message);
}
}
export default issueToJson();