Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ The tag to update. If the workflow event is `release`, it will use the `tag_name
tag_name: ${{ steps.releaser.outputs.tag_name }}
```

**additional_files**

If you need to include more than just `main` in your built commit, you can provide a list of comma separated files as the `additional_files` input. These files will be added and committed when the tag is updated:

```yaml
- uses: fictional/releaser@v1 # Not a real action!
id: releaser
- uses: JasonEtco/build-and-tag-action@v1
with:
additional_files: 'index.cache.js,another.js'
```

## Motivation

The [guide to JavaScript Actions](https://help.github.com/en/actions/building-actions/creating-a-javascript-action) recommends including `node_modules` in your repository, and manual steps to [following the versioning recommendations](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md#versioning). There are anti-patterns there that just don't sit right with me; so we can enable the same workflow, automatically!
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ branding:
inputs:
tag_name:
description: The tag to update. If the workflow event is `release`, it will use the `tag_name` from the event payload.
additional_files:
description: Any additional files to include in the build
67 changes: 52 additions & 15 deletions src/lib/create-commit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Toolkit } from 'actions-toolkit'
import readFile from './read-file'
import readFileBase64 from './read-file'

// This isn't exported from @octokit/types
declare type GitCreateTreeParamsTree = {
path?: string;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "blob" | "tree" | "commit";
sha?: string | null;
content?: string;
}

export default async function createCommit(tools: Toolkit) {
const { main } = tools.getPackageJSON<{ main?: string }>()
Expand All @@ -9,22 +18,40 @@ export default async function createCommit(tools: Toolkit) {
}

tools.log.info('Creating tree')
const tree = await tools.github.git.createTree({
...tools.context.repo,
tree: [
{
path: 'action.yml',
mode: '100644',
type: 'blob',
content: await readFile(tools.workspace, 'action.yml')
},
{
path: main,
let files: GitCreateTreeParamsTree[] = [
{
path: 'action.yml',
mode: '100644',
type: 'blob',
sha: (await createBlob(tools, 'action.yml')).sha
},
{
path: main,
mode: '100644',
type: 'blob',
sha: (await createBlob(tools, main)).sha
}
];

// Add any additional files
if (tools.inputs.additional_files) {
const additional: GitCreateTreeParamsTree[] = await Promise.all(tools.inputs.additional_files.split(",")
.map(async path =>{
path = path.trim();
return {
path,
mode: '100644',
type: 'blob',
content: await readFile(tools.workspace, main)
}
]
sha: (await createBlob(tools, path)).sha
} as GitCreateTreeParamsTree;
}));

files = files.concat(additional);
}

const tree = await tools.github.git.createTree({
...tools.context.repo,
tree: files
})

tools.log.complete('Tree created')
Expand All @@ -40,3 +67,13 @@ export default async function createCommit(tools: Toolkit) {

return commit.data
}

async function createBlob(tools: Toolkit, filePath: string) {
const content = await readFileBase64(tools.workspace, filePath)
const blobData = await tools.github.git.createBlob({
...tools.context.repo,
content,
encoding: 'base64',
})
return blobData.data
}
4 changes: 2 additions & 2 deletions src/lib/read-file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs'
import path from 'path'

export default async function readFile(baseDir: string, file: string) {
export default async function readFileBase64(baseDir: string, file: string) {
const pathToFile = path.join(baseDir, file)

if (!fs.existsSync(pathToFile)) {
throw new Error(`${file} does not exist.`)
}

return fs.promises.readFile(pathToFile, 'utf8')
return fs.promises.readFile(pathToFile, 'base64')
}
40 changes: 38 additions & 2 deletions tests/create-commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ describe('create-commit', () => {
let tools: Toolkit
let treeParams: any
let commitParams: any
let blobParams: any[]

beforeEach(() => {
blobParams = []
tools = generateToolkit()
})

function setupMock(args: {numFiles: number}) {
nock('https://api.github.com')
.post('/repos/JasonEtco/test/git/commits')
.reply(200, (_, body) => {
Expand All @@ -18,11 +24,17 @@ describe('create-commit', () => {
.reply(200, (_, body) => {
treeParams = body
})
.post('/repos/JasonEtco/test/git/blobs')
.times(args.numFiles)
.reply(200, (_, body) => {
blobParams.push(body)
})

tools = generateToolkit()
})
}

it('creates the tree and commit', async () => {
setupMock({numFiles: 2})

await createCommit(tools)
expect(nock.isDone()).toBe(true)

Expand All @@ -37,6 +49,30 @@ describe('create-commit', () => {
expect(commitParams.parents).toEqual([tools.context.sha])
})

it('creates the tree and commit with additional files', async () => {
setupMock({numFiles: 4})

process.env.INPUT_ADDITIONAL_FILES="package.json,additional.js"

await createCommit(tools)
expect(nock.isDone()).toBe(true)

// Test that our tree was created correctly
expect(treeParams.tree).toHaveLength(4)
expect(treeParams.tree.some((obj: any) => obj.path === 'package.json')).toBe(
true
)
expect(treeParams.tree.some((obj: any) => obj.path === 'additional.js')).toBe(
true
)

// Test that our commit was created correctly
expect(commitParams.message).toBe('Automatic compilation')
expect(commitParams.parents).toEqual([tools.context.sha])

delete process.env.INPUT_ADDITIONAL_FILES;
})

it('creates the tree and commit', async () => {
jest.spyOn(tools, 'getPackageJSON').mockReturnValueOnce({})
await expect(() => createCommit(tools)).rejects.toThrow(
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('build-and-tag-action', () => {
.reply(200, { commit: { sha: '123abc' } })
.post('/repos/JasonEtco/test/git/trees')
.reply(200)
.post('/repos/JasonEtco/test/git/blobs')
.times(2)
.reply(200)

await buildAndTagAction(tools)

Expand All @@ -50,6 +53,9 @@ describe('build-and-tag-action', () => {
.reply(200, { commit: { sha: '123abc' } })
.post('/repos/JasonEtco/test/git/trees')
.reply(200)
.post('/repos/JasonEtco/test/git/blobs')
.times(2)
.reply(200)

await buildAndTagAction(tools)

Expand All @@ -64,6 +70,9 @@ describe('build-and-tag-action', () => {
.reply(200, { commit: { sha: '123abc' } })
.post('/repos/JasonEtco/test/git/trees')
.reply(200)
.post('/repos/JasonEtco/test/git/blobs')
.times(2)
.reply(200)

tools.context.payload.release.draft = true

Expand All @@ -80,6 +89,9 @@ describe('build-and-tag-action', () => {
.reply(200, { commit: { sha: '123abc' } })
.post('/repos/JasonEtco/test/git/trees')
.reply(200)
.post('/repos/JasonEtco/test/git/blobs')
.times(2)
.reply(200)

tools.context.payload.release.prerelease = true

Expand All @@ -103,6 +115,9 @@ describe('build-and-tag-action', () => {
.reply(200, { commit: { sha: '123abc' } })
.post('/repos/JasonEtco/test/git/trees')
.reply(200)
.post('/repos/JasonEtco/test/git/blobs')
.times(2)
.reply(200)

tools.context.event = 'pull_request'
process.env.INPUT_TAG_NAME = 'v2.0.0'
Expand Down
13 changes: 9 additions & 4 deletions tests/read-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import path from 'path'
import readFile from '../src/lib/read-file'
import readFileBase64 from '../src/lib/read-file'
import {Buffer} from 'buffer'

describe('read-file', () => {
const baseDir = path.join(__dirname, 'fixtures')

it('reads the file and returns the contents', async () => {
const result = await readFile(baseDir, 'file.md')
expect(result).toBe('Hello!\n')
const result = await readFileBase64(baseDir, 'file.md')
expect(result).toBe(convertToBase64('Hello!\n'))
})

it('throws if the file does not exist', async () => {
await expect(readFile(baseDir, 'nope')).rejects.toThrowError(
await expect(readFileBase64(baseDir, 'nope')).rejects.toThrowError(
'nope does not exist.'
)
})
})

function convertToBase64(value: string) {
return Buffer.from(value, 'utf8').toString('base64')
}