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
7 changes: 5 additions & 2 deletions src/lib/create-commit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Toolkit } from 'actions-toolkit'
import readFile from './read-file'
import findActionFileName from './find-action-file-name'

export default async function createCommit(tools: Toolkit) {
const { main } = tools.getPackageJSON<{ main?: string }>()
Expand All @@ -8,15 +9,17 @@ export default async function createCommit(tools: Toolkit) {
throw new Error('Property "main" does not exist in your `package.json`.')
}

const actionFileName = findActionFileName(tools.workspace)

tools.log.info('Creating tree')
const tree = await tools.github.git.createTree({
...tools.context.repo,
tree: [
{
path: 'action.yml',
path: actionFileName,
mode: '100644',
type: 'blob',
content: await readFile(tools.workspace, 'action.yml')
content: await readFile(tools.workspace, actionFileName)
},
{
path: main,
Expand Down
18 changes: 18 additions & 0 deletions src/lib/find-action-file-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs'
import path from 'path'

export default function findActionFileName(baseDir: string): string {
const actionYmlPath = path.join(baseDir, 'action.yml')

if (fs.existsSync(actionYmlPath)) {
return 'action.yml'
}

const actionYamlPath = path.join(baseDir, 'action.yaml')

if (fs.existsSync(actionYamlPath)) {
return 'action.yaml'
}

throw new Error('No action file found. Please create an action.yml or action.yaml file.')
}
21 changes: 21 additions & 0 deletions tests/find-action-file-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path'
import findActionFileName from '../src/lib/find-action-file-name'

describe('read-file', () => {

it('finds the action file name in the workspace', () => {
const result = findActionFileName(path.join(__dirname, 'fixtures', 'workspace'))
expect(result).toBe('action.yml')
})

it('finds the action file name in the workspace with action.yaml', () => {
const result = findActionFileName(path.join(__dirname, 'fixtures', 'workspace-action-yaml'))
expect(result).toBe('action.yaml')
})

it('throws an error when no action file is found', () => {
expect(() => findActionFileName(path.join(__dirname, 'fixtures'))).toThrowError(
new Error('No action file found. Please create an action.yml or action.yaml file.')
)
})
})
12 changes: 12 additions & 0 deletions tests/fixtures/workspace-action-yaml/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Build and Tag
description: Properly tags your GitHub Action
runs:
using: node12
main: dist/index.js
branding:
icon: archive
color: blue
inputs:
setup:
default: 'npm ci && npm run build --if-present'
description: A command that runs before publishing the action to the release's tag.