Skip to content

Commit

Permalink
Add support for GitHub container actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ncalteen committed Aug 22, 2024
1 parent a5b978f commit 6d44b40
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 35 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
Gets the [Semantic Version](https://semver.org/) of a repository based on the
type of manifest file located. This action currently supports the following:

| Language | Manifest File |
| -------- | ---------------- |
| Dart | `pubspec.yaml` |
| Node.js | `package.json` |
| Python | `pyproject.toml` |
| | `setup.cfg` |
| | `setup.py` |
| Java | `pom.xml` |
| _Other_ | `.version` |
| Language | Manifest File |
| -------------------------- | ---------------- |
| Dart | `pubspec.yaml` |
| GitHub Actions (Container) | `action.yml` |
| Java | `pom.xml` |
| Node.js | `package.json` |
| Python | `pyproject.toml` |
| | `setup.cfg` |
| | `setup.py` |
| _Other_ | `.version` |

> [!TIP]
>
Expand Down
17 changes: 17 additions & 0 deletions __fixtures__/invalid/github/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

inputs:
who-to-greet:
description: Your input description here
required: true
default: World

outputs:
greeting:
description: Your output description here

runs:
using: node20
main: dist/index.js
19 changes: 19 additions & 0 deletions __fixtures__/invalid/github/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

inputs:
who-to-greet:
description: Your input description here
required: true
default: World

outputs:
greeting:
description: Your output description here

runs:
using: docker
image: docker://ghcr.io/actions/container-prebuilt-action:latest
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
22 changes: 22 additions & 0 deletions __fixtures__/valid/github/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

# Define your inputs here.
inputs:
who-to-greet:
description: Your input description here
required: true
default: World

# Define your outputs here.
outputs:
greeting:
description: Your output description here

# Update the image repository and tag below.
runs:
using: docker
image: docker://ghcr.io/actions/container-prebuilt-action:v1.2.3
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
22 changes: 22 additions & 0 deletions __fixtures__/valid/github/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

# Define your inputs here.
inputs:
who-to-greet:
description: Your input description here
required: true
default: World

# Define your outputs here.
outputs:
greeting:
description: Your output description here

# Update the image repository and tag below.
runs:
using: docker
image: docker://ghcr.io/actions/container-prebuilt-action:v1.2.3
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
22 changes: 20 additions & 2 deletions __tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ describe('version.ts', () => {
expect(
Version.infer('../__fixtures__/valid/java/pom.xml', __dirname)?.toString()
).toEqual('8.0.0')
expect(
Version.infer(
'../__fixtures__/valid/github/action.yaml',
__dirname
)?.toString()
).toEqual('1.2.3')
expect(
Version.infer(
'../__fixtures__/valid/github/action.yml',
__dirname
)?.toString()
).toEqual('1.2.3')
expect(
Version.infer(
'../__fixtures__/valid/python/pyproject.toml',
Expand Down Expand Up @@ -104,11 +116,17 @@ describe('version.ts', () => {
).toEqual('1.2.3-alpha.4+build.5')
})

it('does not infer the version from invalid manifests', async () => {
it('Does not infer the version from invalid manifests', async () => {
// Pass each invalid manifest file
expect(
Version.infer('../__fixtures__/invalid/java/pom.xml', __dirname)
).toEqual(undefined)
expect(
Version.infer('../__fixtures__/invalid/github/action.yaml', __dirname)
).toEqual(undefined)
expect(
Version.infer('../__fixtures__/invalid/github/action.yml', __dirname)
).toEqual(undefined)
expect(
Version.infer('../__fixtures__/invalid/python/pyproject.toml', __dirname)
).toEqual(undefined)
Expand Down Expand Up @@ -171,7 +189,7 @@ describe('version.ts', () => {
).toBe(undefined)
})

it('Rhrows an error if a manifest path is invalid', async () => {
it('Throws an error if a manifest path is invalid', async () => {
try {
expect(Version.infer('path/to/', __dirname)).toThrow()
} catch (error) {
Expand Down
22 changes: 19 additions & 3 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 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
@@ -1,7 +1,7 @@
{
"name": "issue-ops-semver",
"description": "Semantically version GitHub repository tags",
"version": "2.1.0",
"version": "2.2.0",
"type": "module",
"author": "Nick Alteen <ncalteen@github.com>",
"homepage": "https://github.com/issue-ops/semver#readme",
Expand Down
26 changes: 21 additions & 5 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,31 @@ export class Version {
const manifestFile: string = items[items.length - 1]
core.info(`Manifest file: ${manifestFile}`)

// Ref: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const regex =
/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/gm

if (manifestFile === '')
throw new Error(`Invalid manifest path: ${manifestPath}`)

// Functions for parsing each type of manifest file
// eslint-disable-next-line no-unused-vars
const parser: { [k: string]: (body: string) => string | undefined } = {
'action.yml': (body: string): string | undefined => {
return yaml
.parse(body)
.runs?.image?.split(':')
?.slice(-1)?.[0]
.match(regex)?.[0]
.replace('v', '')
},
'action.yaml': (body: string): string | undefined => {
return yaml
.parse(body)
.runs?.image?.split(':')
?.slice(-1)?.[0]
.match(regex)?.[0]
.replace('v', '')
},
'package.json': (body: string): string | undefined => {
return JSON.parse(body).version
},
Expand All @@ -141,10 +160,7 @@ export class Version {
return yamlBody.version
},
'.version': (body: string): string | undefined => {
// Ref: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
return body.match(
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/gm
)?.[0]
return body.match(regex)?.[0]?.replace('v', '')
}
}

Expand Down

0 comments on commit 6d44b40

Please sign in to comment.