|
1 | | -import * as core from '@actions/core' |
2 | | -import { Toolkit } from 'actions-toolkit' |
3 | | -import fm from 'front-matter' |
4 | | -import nunjucks from 'nunjucks' |
| 1 | +import * as core from "@actions/core"; |
| 2 | +import { Toolkit } from "actions-toolkit"; |
| 3 | +import fm from "front-matter"; |
| 4 | +import nunjucks from "nunjucks"; |
5 | 5 | // @ts-expect-error |
6 | | -import dateFilter from 'nunjucks-date-filter' |
7 | | -import { ZodError } from 'zod' |
8 | | -import { FrontMatterAttributes, frontmatterSchema, listToArray, setOutputs } from './helpers' |
9 | | - |
10 | | -function logError(tools: Toolkit, template: string, action: 'creating' | 'updating' | 'parsing', err: any) { |
| 6 | +import dateFilter from "nunjucks-date-filter"; |
| 7 | +import { ZodError } from "zod"; |
| 8 | +import { |
| 9 | + FrontMatterAttributes, |
| 10 | + frontmatterSchema, |
| 11 | + listToArray, |
| 12 | + setOutputs, |
| 13 | +} from "./helpers"; |
| 14 | + |
| 15 | +function logError( |
| 16 | + tools: Toolkit, |
| 17 | + template: string, |
| 18 | + action: "creating" | "updating" | "parsing", |
| 19 | + err: any |
| 20 | +) { |
11 | 21 | // Log the error message |
12 | | - 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}!` |
13 | | - tools.log.error(errorMessage) |
14 | | - tools.log.error(err) |
| 22 | + 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}!`; |
| 23 | + tools.log.error(errorMessage); |
| 24 | + tools.log.error(err); |
15 | 25 |
|
16 | 26 | // The error might have more details |
17 | | - if (err.errors) tools.log.error(err.errors) |
| 27 | + if (err.errors) tools.log.error(err.errors); |
18 | 28 |
|
19 | 29 | // Exit with a failing status |
20 | | - core.setFailed(errorMessage + '\n\n' + err.message) |
21 | | - return tools.exit.failure() |
| 30 | + core.setFailed(errorMessage + "\n\n" + err.message); |
| 31 | + return tools.exit.failure(); |
22 | 32 | } |
23 | 33 |
|
24 | | -export async function createAnIssue (tools: Toolkit) { |
25 | | - const template = tools.inputs.filename || '.github/ISSUE_TEMPLATE.md' |
26 | | - const assignees = tools.inputs.assignees |
| 34 | +export async function createAnIssue(tools: Toolkit) { |
| 35 | + const template = tools.inputs.filename || ".github/ISSUE_TEMPLATE.md"; |
| 36 | + const assignees = tools.inputs.assignees; |
27 | 37 |
|
28 | | - let updateExisting: Boolean | null = null |
| 38 | + let updateExisting: Boolean | null = null; |
29 | 39 | if (tools.inputs.update_existing) { |
30 | | - if (tools.inputs.update_existing === 'true') { |
31 | | - updateExisting = true |
32 | | - } else if (tools.inputs.update_existing === 'false') { |
33 | | - updateExisting = false |
| 40 | + if (tools.inputs.update_existing === "true") { |
| 41 | + updateExisting = true; |
| 42 | + } else if (tools.inputs.update_existing === "false") { |
| 43 | + updateExisting = false; |
34 | 44 | } else { |
35 | | - tools.exit.failure(`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`) |
| 45 | + tools.exit.failure( |
| 46 | + `Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false` |
| 47 | + ); |
36 | 48 | } |
37 | 49 | } |
38 | 50 |
|
39 | | - const env = nunjucks.configure({ autoescape: false }) |
40 | | - env.addFilter('date', dateFilter) |
| 51 | + const env = nunjucks.configure({ autoescape: false }); |
| 52 | + env.addFilter("date", dateFilter); |
41 | 53 |
|
42 | 54 | const templateVariables = { |
43 | 55 | ...tools.context, |
44 | 56 | repo: tools.context.repo, |
45 | 57 | env: process.env, |
46 | | - date: Date.now() |
47 | | - } |
| 58 | + date: Date.now(), |
| 59 | + }; |
48 | 60 |
|
49 | 61 | // Get the file |
50 | | - tools.log.debug('Reading from file', template) |
51 | | - const file = await tools.readFile(template) as string |
| 62 | + tools.log.debug("Reading from file", template); |
| 63 | + const file = (await tools.readFile(template)) as string; |
52 | 64 |
|
53 | 65 | // Grab the front matter as JSON |
54 | | - const { attributes: rawAttributes, body } = fm<FrontMatterAttributes>(file) |
| 66 | + const { attributes: rawAttributes, body } = fm<FrontMatterAttributes>(file); |
55 | 67 |
|
56 | | - let attributes: FrontMatterAttributes |
| 68 | + let attributes: FrontMatterAttributes; |
57 | 69 | try { |
58 | | - attributes = await frontmatterSchema.parseAsync(rawAttributes) |
| 70 | + attributes = await frontmatterSchema.parseAsync(rawAttributes); |
59 | 71 | } catch (err) { |
60 | 72 | if (err instanceof ZodError) { |
61 | | - const formatted = err.format() |
62 | | - return logError(tools, template, 'parsing', formatted) |
| 73 | + const formatted = err.format(); |
| 74 | + return logError(tools, template, "parsing", formatted); |
63 | 75 | } |
64 | | - throw err |
| 76 | + throw err; |
65 | 77 | } |
66 | 78 |
|
67 | | - tools.log(`Front matter for ${template} is`, attributes) |
| 79 | + tools.log(`Front matter for ${template} is`, attributes); |
68 | 80 |
|
69 | 81 | const templated = { |
70 | 82 | body: env.renderString(body, templateVariables), |
71 | | - title: env.renderString(attributes.title, templateVariables) |
72 | | - } |
73 | | - tools.log.debug('Templates compiled', templated) |
| 83 | + title: env.renderString(attributes.title, templateVariables), |
| 84 | + }; |
| 85 | + tools.log.debug("Templates compiled", templated); |
74 | 86 |
|
75 | 87 | if (updateExisting !== null) { |
76 | | - tools.log.info(`Fetching issues with title "${templated.title}"`) |
| 88 | + tools.log.info(`Fetching issues with title "${templated.title}"`); |
77 | 89 |
|
78 | | - let query = `is:issue repo:${process.env.GITHUB_REPOSITORY} in:title "${templated.title.replace(/['"]/g, "\\$&")}"` |
| 90 | + let query = `is:issue repo:${ |
| 91 | + process.env.GITHUB_REPOSITORY |
| 92 | + } in:title "${templated.title.replace(/['"]/g, "\\$&")}"`; |
79 | 93 |
|
80 | | - const searchExistingType = tools.inputs.search_existing || 'open' |
81 | | - const allowedStates = ['open', 'closed'] |
| 94 | + const searchExistingType = tools.inputs.search_existing || "open"; |
| 95 | + const allowedStates = ["open", "closed"]; |
82 | 96 | if (allowedStates.includes(searchExistingType)) { |
83 | | - query += ` is:${searchExistingType}` |
| 97 | + query += ` is:${searchExistingType}`; |
84 | 98 | } |
85 | 99 |
|
86 | | - const existingIssues = await tools.github.search.issuesAndPullRequests({ q: query }) |
87 | | - const existingIssue = existingIssues.data.items.find(issue => issue.title === templated.title) |
| 100 | + const existingIssues = await tools.github.search.issuesAndPullRequests({ |
| 101 | + q: query, |
| 102 | + }); |
| 103 | + const existingIssue = existingIssues.data.items.find( |
| 104 | + (issue) => issue.title === templated.title |
| 105 | + ); |
88 | 106 | if (existingIssue) { |
89 | 107 | if (updateExisting === false) { |
90 | | - tools.exit.success(`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`) |
| 108 | + tools.exit.success( |
| 109 | + `Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated` |
| 110 | + ); |
91 | 111 | } else { |
92 | 112 | try { |
93 | | - tools.log.info(`Updating existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`) |
| 113 | + tools.log.info( |
| 114 | + `Updating existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}` |
| 115 | + ); |
94 | 116 | const issue = await tools.github.issues.update({ |
95 | 117 | ...tools.context.repo, |
96 | 118 | issue_number: existingIssue.number, |
97 | | - body: templated.body |
98 | | - }) |
99 | | - setOutputs(tools, issue.data) |
100 | | - tools.exit.success(`Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`) |
| 119 | + body: templated.body, |
| 120 | + }); |
| 121 | + setOutputs(tools, issue.data); |
| 122 | + tools.exit.success( |
| 123 | + `Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}` |
| 124 | + ); |
101 | 125 | } catch (err: any) { |
102 | | - return logError(tools, template, 'updating', err) |
| 126 | + return logError(tools, template, "updating", err); |
103 | 127 | } |
104 | 128 | } |
105 | 129 | } else { |
106 | | - tools.log.info('No existing issue found to update') |
| 130 | + tools.log.info("No existing issue found to update"); |
107 | 131 | } |
108 | 132 | } |
109 | 133 |
|
110 | 134 | // Create the new issue |
111 | | - tools.log.info(`Creating new issue ${templated.title}`) |
| 135 | + tools.log.info(`Creating new issue ${templated.title}`); |
112 | 136 | try { |
113 | 137 | const issue = await tools.github.issues.create({ |
114 | 138 | ...tools.context.repo, |
115 | 139 | ...templated, |
116 | | - assignees: assignees ? listToArray(assignees) : listToArray(attributes.assignees), |
| 140 | + assignees: assignees |
| 141 | + ? listToArray(assignees) |
| 142 | + : listToArray(attributes.assignees), |
117 | 143 | labels: listToArray(attributes.labels), |
118 | | - milestone: Number(tools.inputs.milestone || attributes.milestone) || undefined |
119 | | - }) |
120 | | - |
121 | | - setOutputs(tools, issue.data) |
122 | | - tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`) |
| 144 | + milestone: |
| 145 | + Number(tools.inputs.milestone || attributes.milestone) || undefined, |
| 146 | + }); |
| 147 | + |
| 148 | + setOutputs(tools, issue.data); |
| 149 | + tools.log.success( |
| 150 | + `Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}` |
| 151 | + ); |
123 | 152 | } catch (err: any) { |
124 | | - return logError(tools, template, 'creating', err) |
| 153 | + return logError(tools, template, "creating", err); |
125 | 154 | } |
126 | 155 | } |
0 commit comments