Skip to content

Commit

Permalink
Merge pull request #6 from JasonEtco/actions-toolkit-200
Browse files Browse the repository at this point in the history
actions-toolkit@2.0.0
  • Loading branch information
JasonEtco committed Mar 31, 2019
2 parents 64520c0 + 2484525 commit f956175
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ COPY package*.json ./
RUN npm ci
COPY . .

ENTRYPOINT ["node", "/entrypoint.js"]
ENTRYPOINT ["node", "/index.js"]
9 changes: 0 additions & 9 deletions entrypoint.js

This file was deleted.

79 changes: 36 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
const { Toolkit } = require('actions-toolkit')
const fm = require('front-matter')
const nunjucks = require('nunjucks')
const dateFilter = require('nunjucks-date-filter')

class IssueCreator {
/**
* @param {import('actions-toolkit').Toolkit} tools
*/
constructor (tools) {
this.tools = tools
this.template = this.tools.arguments._[0] || '.github/ISSUE_TEMPLATE.md'
this.env = nunjucks.configure({ autoescape: false })
this.env.addFilter('date', dateFilter)
Toolkit.run(async tools => {
const template = tools.arguments._[0] || '.github/ISSUE_TEMPLATE.md'
const env = nunjucks.configure({ autoescape: false })
env.addFilter('date', dateFilter)

const templateVariables = {
...tools.context,
date: Date.now()
}

async go () {
const templateVariables = {
...this.tools.context,
date: Date.now()
}

// Get the file
this.tools.log('Reading from file', this.template)
const file = this.tools.getFile(this.template)

// Grab the front matter as JSON
const { attributes, body } = fm(file)
this.tools.log(`Front matter for ${this.template} is`, attributes)

const templated = {
body: this.env.renderString(body, templateVariables),
title: this.env.renderString(attributes.title, templateVariables)
}

this.tools.log('Templates compiled', templated)
this.tools.log('Creating new issue')

// Create the new issue
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
return this.tools.github.issues.create({
owner,
repo,
...templated,
assignees: attributes.assignees || [],
labels: attributes.labels || [],
milestone: attributes.milestone
})
// Get the file
tools.log('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)
}
}

module.exports = IssueCreator
tools.log('Templates compiled', templated)
tools.log('Creating new issue')

// Create the new issue
const issue = await tools.github.issues.create({
...tools.context.repo,
...templated,
assignees: attributes.assignees || [],
labels: attributes.labels || [],
milestone: attributes.milestone
})

tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
}, {
secrets: ['GITHUB_TOKEN']
})
99 changes: 66 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "Jason Etcovitch <jasonetco@gmail.com>",
"license": "MIT",
"dependencies": {
"actions-toolkit": "^1.5.0",
"actions-toolkit": "^2.0.0",
"front-matter": "^3.0.1",
"js-yaml": "^3.12.0",
"nunjucks": "^3.1.4",
Expand Down
8 changes: 8 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Array [
]
`;

exports[`create-an-issue creates a new issue 2`] = `
Array [
Array [
"Created issue Hello!#1: www",
],
]
`;

exports[`create-an-issue creates a new issue with assignees, labels and a milestone 1`] = `
Array [
Array [
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"repository": {
"owner": { "login": "JasonEtco" },
"name": "waddup"
}
}

0 comments on commit f956175

Please sign in to comment.