/
action.ts
155 lines (137 loc) · 4.77 KB
/
action.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as core from "@actions/core";
import { Toolkit } from "actions-toolkit";
import fm from "front-matter";
import nunjucks from "nunjucks";
// @ts-expect-error
import dateFilter from "nunjucks-date-filter";
import { ZodError } from "zod";
import {
FrontMatterAttributes,
frontmatterSchema,
listToArray,
setOutputs,
} from "./helpers";
function logError(
tools: Toolkit,
template: string,
action: "creating" | "updating" | "parsing",
err: any
) {
// Log the error message
const errorMessage = `An error occurred while ${action} the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`;
tools.log.error(errorMessage);
tools.log.error(err);
// The error might have more details
if (err.errors) tools.log.error(err.errors);
// Exit with a failing status
core.setFailed(errorMessage + "\n\n" + err.message);
return tools.exit.failure();
}
export async function createAnIssue(tools: Toolkit) {
const template = tools.inputs.filename || ".github/ISSUE_TEMPLATE.md";
const assignees = tools.inputs.assignees;
let updateExisting: Boolean | null = null;
if (tools.inputs.update_existing) {
if (tools.inputs.update_existing === "true") {
updateExisting = true;
} else if (tools.inputs.update_existing === "false") {
updateExisting = false;
} else {
tools.exit.failure(
`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`
);
}
}
const env = nunjucks.configure({ autoescape: false });
env.addFilter("date", dateFilter);
const templateVariables = {
...tools.context,
repo: tools.context.repo,
env: process.env,
date: Date.now(),
};
// Get the file
tools.log.debug("Reading from file", template);
const file = (await tools.readFile(template)) as string;
// Grab the front matter as JSON
const { attributes: rawAttributes, body } = fm<FrontMatterAttributes>(file);
let attributes: FrontMatterAttributes;
try {
attributes = await frontmatterSchema.parseAsync(rawAttributes);
} catch (err) {
if (err instanceof ZodError) {
const formatted = err.format();
return logError(tools, template, "parsing", formatted);
}
throw err;
}
tools.log(`Front matter for ${template} is`, attributes);
const templated = {
body: env.renderString(body, templateVariables),
title: env.renderString(attributes.title, templateVariables),
};
tools.log.debug("Templates compiled", templated);
if (updateExisting !== null) {
tools.log.info(`Fetching issues with title "${templated.title}"`);
let query = `is:issue repo:${
process.env.GITHUB_REPOSITORY
} in:title "${templated.title.replace(/['"]/g, "\\$&")}"`;
const searchExistingType = tools.inputs.search_existing || "open";
const allowedStates = ["open", "closed"];
if (allowedStates.includes(searchExistingType)) {
query += ` is:${searchExistingType}`;
}
const existingIssues = await tools.github.search.issuesAndPullRequests({
q: query,
});
const existingIssue = existingIssues.data.items.find(
(issue) => issue.title === templated.title
);
if (existingIssue) {
if (updateExisting === false) {
tools.exit.success(
`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`
);
} else {
try {
tools.log.info(
`Updating existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`
);
const issue = await tools.github.issues.update({
...tools.context.repo,
issue_number: existingIssue.number,
body: templated.body,
});
setOutputs(tools, issue.data);
tools.exit.success(
`Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`
);
} catch (err: any) {
return logError(tools, template, "updating", err);
}
}
} else {
tools.log.info("No existing issue found to update");
}
}
// Create the new issue
tools.log.info(`Creating new issue ${templated.title}`);
try {
const issue = await tools.github.issues.create({
...tools.context.repo,
...templated,
assignees: assignees
? listToArray(assignees)
: listToArray(attributes.assignees),
labels: listToArray(attributes.labels),
milestone:
Number(tools.inputs.milestone || attributes.milestone) || undefined,
});
setOutputs(tools, issue.data);
tools.log.success(
`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`
);
} catch (err: any) {
return logError(tools, template, "creating", err);
}
}