Skip to content

Commit

Permalink
Initial import of npm-package-versioner code
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-matt-hillsdon committed Apr 16, 2024
1 parent 76af5c0 commit f6250f3
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 291 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,3 @@ jobs:
uses: ./
with:
milliseconds: 2000

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
3 changes: 0 additions & 3 deletions CODEOWNERS

This file was deleted.

4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright GitHub
Copyright Micro:bit Educational Foundation, GitHub

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 All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
62 changes: 9 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
# Create a GitHub Action Using TypeScript
# NPM package versioner GitHub action

[![GitHub Super-Linter](https://github.com/actions/typescript-action/actions/workflows/linter.yml/badge.svg)](https://github.com/super-linter/super-linter)
![CI](https://github.com/actions/typescript-action/actions/workflows/ci.yml/badge.svg)
[![Check dist/](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml)
[![CodeQL](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml)
[![Coverage](./badges/coverage.svg)](./badges/coverage.svg)

Use this template to bootstrap the creation of a TypeScript action. :rocket:
Manages the `package.json` version field for GitHub action.

This template includes compilation support, tests, a validation workflow,
publishing, and versioning guidance.
The action will write the new version number to package.json.

If you are new, there's also a simpler introduction in the
[Hello world JavaScript action repository](https://github.com/actions/hello-world-javascript-action).
Usage:

## Create Your Own Action

To create your own action, you can use this repository as a template! Just
follow the below instructions:

1. Click the **Use this template** button at the top of the repository
1. Select **Create a new repository**
1. Select an owner and name for your new repository
1. Click **Create repository**
1. Clone your new repository

> [!IMPORTANT]
>
> Make sure to remove or update the [`CODEOWNERS`](./CODEOWNERS) file! For
> details on how to use this file, see
> [About code owners](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners).
```
steps:
- uses: microbit-foundation/npm-package-versioner-action@v1
```

## Initial Setup
## Development documentation from template repo

After you've cloned the repository to your local machine or codespace, you'll
need to perform some initial setup steps before you can develop your action.
Expand Down Expand Up @@ -72,15 +58,6 @@ need to perform some initial setup steps before you can develop your action.
...
```

## Update the Action Metadata

The [`action.yml`](action.yml) file defines metadata about your action, such as
input(s) and output(s). For details about this file, see
[Metadata syntax for GitHub Actions](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions).

When you copy this repository, update `action.yml` with the name, description,
inputs, and outputs for your action.

## Update the Action Code

The [`src/`](./src/) directory is the heart of your action! This contains the
Expand Down Expand Up @@ -206,24 +183,3 @@ steps:
id: output
run: echo "${{ steps.test-action.outputs.time }}"
```
## Publishing a New Release
This project includes a helper script, [`script/release`](./script/release)
designed to streamline the process of tagging and pushing new releases for
GitHub Actions.

GitHub Actions allows users to select a specific version of the action to use,
based on release tags. This script simplifies this process by performing the
following steps:

1. **Retrieving the latest release tag:** The script starts by fetching the most
recent release tag by looking at the local data available in your repository.
1. **Prompting for a new release tag:** The user is then prompted to enter a new
release tag. To assist with this, the script displays the latest release tag
and provides a regular expression to validate the format of the new tag.
1. **Tagging the new release:** Once a valid new tag is entered, the script tags
the new release.
1. **Pushing the new tag to the remote:** Finally, the script pushes the new tag
to the remote repository. From here, you will need to create a new release in
GitHub and users can easily reference the new tag in their workflows.
89 changes: 0 additions & 89 deletions __tests__/main.test.ts

This file was deleted.

182 changes: 182 additions & 0 deletions __tests__/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import assert from 'node:assert'
import { contextFromEnvironment, generateVersion } from '../src/version'

describe(`generateVersion`, () => {
const defaultContext = {
ci: true,
buildNumber: 34,
tag: undefined,
branch: undefined
}

it(`should use local suffix when not in CI`, () => {

Check warning on line 13 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
ci: false
}
assert.deepEqual(generateVersion('1.2.3-foo', context), {
version: '1.2.3-local'
})
})

it(`should use tag when tag specified`, () => {

Check warning on line 23 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
tag: '3.2.1'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '3.2.1'
})
})

it(`should strip the v prefix from tags`, () => {

Check warning on line 33 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
tag: 'v3.2.1'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '3.2.1'
})
})

it(`work for v1.1.1`, () => {

Check warning on line 43 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
tag: 'v1.1.1'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.1.1'
})
})

it(`should error for non-semver tag`, () => {

Check warning on line 53 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
tag: 'wibble'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
error: 'Invalid semver tag: wibble'
})
})

it(`should use the build number for branches`, () => {

Check warning on line 63 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
branch: 'wobble'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-wobble.34'
})
})

// Assumption is that you only use one of these for versioned builds.
for (const usesDev of ['master', 'main', 'develop']) {
it(`should use dev for ${usesDev}`, () => {

Check warning on line 75 in __tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Test has no assertions
const context = {
...defaultContext,
branch: usesDev
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-dev.34'
})
})
}

it(`should error if no branch or tag`, () => {
assert.deepEqual(generateVersion('1.0.0-local', defaultContext), {
error: 'Could not determine a version. CI environment invalid?'
})
})

it(`should sanitise branch names`, () => {
const context = {
...defaultContext,
branch: 'feature/£-foo-bar\\blort-1234.99'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-feature.foo.bar.blort.1234.99.34'
})
})

it(`should use dot separated components as per semver`, () => {
const context = {
...defaultContext,
branch: 'feature/my-fave-feature'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-feature.my.fave.feature.34'
})
})

it(`treat underscores as separators`, () => {
const context = {
...defaultContext,
branch: 'foo_bar'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-foo.bar.34'
})
})

it(`should use a placeholder if the branch is sanitized away`, () => {
const context = {
...defaultContext,
branch: '---'
}
assert.deepEqual(generateVersion('1.0.0-local', context), {
version: '1.0.0-branch.34'
})
})
})

describe('contextFromEnvironment', () => {
it('works for GitHub actions branch case', () => {
assert.deepEqual(
contextFromEnvironment({
GITHUB_ACTION: 'lala',
CI: 'true',
GITHUB_REF: 'refs/heads/asdf',
GITHUB_RUN_NUMBER: '12'
}),
{
branch: 'asdf',
buildNumber: 12,
ci: true,
tag: undefined
}
)
})
it('works for GitHub actions tag case', () => {
assert.deepEqual(
contextFromEnvironment({
GITHUB_ACTION: 'lala',
CI: 'true',
GITHUB_REF: 'refs/tags/asdf',
GITHUB_RUN_NUMBER: '12'
}),
{
branch: undefined,
buildNumber: 12,
ci: true,
tag: 'asdf'
}
)
})
it('works for GitHub actions tag case numeric', () => {
assert.deepEqual(
contextFromEnvironment({
GITHUB_ACTION: 'lala',
CI: 'true',
GITHUB_REF: 'refs/tags/v1.1.1',
GITHUB_RUN_NUMBER: '12'
}),
{
branch: undefined,
buildNumber: 12,
ci: true,
tag: 'v1.1.1'
}
)
})
})
Loading

0 comments on commit f6250f3

Please sign in to comment.