forked from JasonEtco/create-an-issue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (54 loc) · 2.04 KB
/
index.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
59
60
61
62
63
64
65
const core = require('@actions/core')
const { Toolkit } = require('actions-toolkit')
const fm = require('front-matter')
const nunjucks = require('nunjucks')
const dateFilter = require('nunjucks-date-filter')
function listToArray (list) {
if (!list) return []
return Array.isArray(list) ? list : list.split(', ')
}
Toolkit.run(async tools => {
const template = core.getInput('filename') || '.github/ISSUE_TEMPLATE.md'
const assignees = core.getInput('assignees')
const env = nunjucks.configure({ autoescape: false })
env.addFilter('date', dateFilter)
const templateVariables = {
...tools.context,
date: Date.now()
}
// Get the file
tools.log.debug('Reading from file', template)
const file = tools.getFile(template)
// Grab the front matter as JSON
const { attributes, body } = fm(file)
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)
tools.log.info(`Creating new issue ${templated.title}`)
// Create the new issue
try {
const issue = await tools.github.issues.create({
...tools.context.repo,
...templated,
assignees: assignees ? listToArray(assignees) : listToArray(attributes.assignees),
labels: listToArray(attributes.labels),
milestone: attributes.milestone
})
core.setOutput('number', issue.data.number)
core.setOutput('url', issue.data.html_url)
tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
} catch (err) {
// Log the error message
tools.log.error(`An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`)
tools.log.error(err)
// The error might have more details
if (err.errors) tools.log.error(err.errors)
// Exit with a failing status
tools.exit.failure()
}
}, {
secrets: ['GITHUB_TOKEN']
})