Skip to content

Commit

Permalink
Merge pull request #2 from tylermilner/releases/v1
Browse files Browse the repository at this point in the history
WIP initial action creation
  • Loading branch information
tylermilner authored Apr 4, 2024
2 parents 5687906 + da77ebe commit 742adb3
Show file tree
Hide file tree
Showing 14 changed files with 25,944 additions and 18,472 deletions.
3 changes: 3 additions & 0 deletions .github/linters/.markdown-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
MD004:
style: dash

MD013:
code_blocks: false

# Ordered list item prefix
MD029:
style: one
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ jobs:
id: test-action
uses: ./
with:
milliseconds: 1000
github-token: ${{ github.token }}
workflow-id: ci.yml
branch: main
debug: true

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
# prettier-ignore
run: echo "The last successful commit was ${{steps.test-action.outputs.commit-hash }}"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright GitHub
Copyright Tyler Milner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
160 changes: 60 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,90 @@
This action returns the commit hash of the last successful run for the given
workflow and branch.

## Update the Action Code
## Inputs

The [`src/`](./src/) directory is the heart of your action! This contains the
source code that will be run when your action is invoked. You can replace the
contents of this directory with your own code.
### `github-token`

There are a few things to keep in mind when writing your action code:
**Required** The GitHub token (e.g. `${{ github.token }}` or
`${{ secrets.GITHUB_TOKEN }}` to use the built-in values).

- Most GitHub Actions toolkit and CI/CD operations are processed asynchronously.
In `main.js`, you will see that the action is run in an `async` function.
### `workflow-id`

```javascript
const core = require('@actions/core')
//...
**Required** The workflow ID or workflow filename (e.g `test.yml`) to determine
the last successful run from.

async function run() {
try {
//...
} catch (error) {
core.setFailed(error.message)
}
}
```
### `branch`

For more information about the GitHub Actions toolkit, see the
[documentation](https://github.com/actions/toolkit/blob/master/README.md).
**Required** The branch to use for determining the last successful run.

So, what are you waiting for? Go ahead and start customizing your action!
### `debug`

1. Create a new branch
**Optional** Whether to enable debug logging. Default: `false`.

```bash
git checkout -b releases/v1
```
## Outputs

1. Replace the contents of `src/` with your action code
1. Add tests to `__tests__/` for your source code
1. Format, test, and build the action
### `commit-hash`

```bash
npm run all
```
The commit hash of the last successful run for the given workflow and branch.

> [!WARNING]
>
> This step is important! It will run [`ncc`](https://github.com/vercel/ncc)
> to build the final JavaScript action code with all dependencies included.
> If you do not run this step, your action will not work correctly when it is
> used in a workflow. This step also includes the `--license` option for
> `ncc`, which will create a license file for all of the production node
> modules used in your project.
## Example usage

1. Commit your changes

```bash
git add .
git commit -m "My first action is ready!"
```

1. Push them to your repository

```bash
git push -u origin releases/v1
```
<!-- prettier-ignore-start -->
```yaml
steps:
- name: Get last successful commit
id: last_successful_commit
uses: tylermilner/last-successful-commit-action@v1
with:
github-token: ${{ github.token }}
workflow-id: ci.yml
branch: main
- name: Use last successful commit
run:
echo "The last successful commit was ${{steps.last_successful_commit.outputs.commit-hash }}"
```
<!-- prettier-ignore-end -->
1. Create a pull request and get feedback on your action
1. Merge the pull request into the `main` branch
## Contributing
Your action is now published! :rocket:
### Source Code Overview
For information about versioning your action, see
[Versioning](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
in the GitHub Actions toolkit.
The following files make up this action:
## Validate the Action
- `action.yml` - action metadata.
- `src/index.js` - main action entry point. Calls `main.js` to run the action.
- `src/main.js` - main action logic. Changes to the action's functionality
should be made here.
- `package.json` / `package-lock.json` - defines the JavaScript dependencies
that the action needs to run.
- `dist/*` - the compiled version of the action with all of its dependencies.
These files are automatically generated and should **NOT** be modified
directly.

You can now validate the action by referencing it in a workflow file. For
example, [`ci.yml`](./.github/workflows/ci.yml) demonstrates how to reference an
action in the same repository.
### Making Code Changes

```yaml
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v3
First, `cd` into the action folder and install the project dependencies via
[npm](https://www.npmjs.com):

- name: Test Local Action
id: test-action
uses: ./
with:
milliseconds: 1000

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
```Shell
npm install
```

For example workflow runs, check out the
[Actions tab](https://github.com/actions/javascript-action/actions)! :rocket:
In order to avoid the need to check in the `node_modules` folder, this action
utilizes [@vercel/ncc](https://github.com/vercel/ncc) to compile the action code
and its dependencies into a single JavaScript file that can be used for
distribution.

## Usage
⚠️ **Important!** - After making code changes to this action, you will need to
recompile the action before committing your changes:

After testing, you can create version tag(s) that developers can use to
reference different stable versions of your action. For more information, see
[Versioning](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
in the GitHub Actions toolkit.
To include the action in a workflow in another repository, you can use the
`uses` syntax with the `@` symbol to reference a specific branch, tag, or commit
hash.
```Shell
npm run all
```

```yaml
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4
This will run all of the formatters, tests, and compile the action into the
`dist` folder. Make sure to include any updated files in your commit.

- name: Run my Action
id: run-action
uses: actions/javascript-action@v1 # Commit with the `v1` tag
with:
milliseconds: 1000
## License

- name: Print Output
id: output
run: echo "${{ steps.run-action.outputs.time }}"
```
[MIT](LICENSE)
91 changes: 3 additions & 88 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,8 @@
const core = require('@actions/core')
const main = require('../src/main')

// Mock the GitHub Actions core library
const debugMock = jest.spyOn(core, 'debug').mockImplementation()
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
// TODO: Implement tests

// Mock the action's main function
const runMock = jest.spyOn(main, 'run')

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
)
})

it('sets a failed status', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
)
})

it('fails if no input is provided', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
throw new Error('Input required and not supplied: milliseconds')
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Input required and not supplied: milliseconds'
)
})
test('test_zero', () => {
expect(true).toBe(true)
})
24 changes: 0 additions & 24 deletions __tests__/wait.test.js

This file was deleted.

3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ outputs:
runs:
using: node20
main: dist/index.js
branding:
icon: 'git-commit'
color: 'blue'
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 742adb3

Please sign in to comment.