Skip to content

Commit

Permalink
Add support for custom variables input
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Mar 27, 2023
1 parent d7e0d0e commit 06e0468
Show file tree
Hide file tree
Showing 13 changed files with 10,965 additions and 8,469 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ jobs:
- uses: ./
id: draft-release
with:
variables: |
helm-chart=v0.1.1
foo=bar
my-variable=My Variable
notes-header: |
## Welcome to the {{version}} release of Draft Release
This is some text to welcome you to the release {{version-number}}.
## Helm Chart
The Helm Chart version for this release is {{helm-chart}}.
## My Variable
The value of my variable is {{my-variable}}.
notes-footer: |
## Upgrade
- For Docker, use the {{version}} image from Docker Hub.
- For Binaries use the {{version-number}} release from GitHub.
- For Helm Chart, use the {{helm-chart}} version.
- For foo use the {{foo}} version.
- run: |
echo "Version: ${{ steps.draft-release.outputs.version }}"
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ jobs:

| Name | Type | Description |
| --- | --- | --- |
| `github-token` | `string` | The GitHub token to use for the release. (default `github.token`) |
| `minor-label` | `string` | The label to use for minor releases. (default `enhancement`) |
| `major-label` | `string` | The label to use for major releases. (default `change`) |
| `notes-header` | `string` | The header to use for the release notes. |
| `notes-footer` | `string` | The footer to use for the release notes. |
| `github-token` | `string` | The GitHub token to use for the release. (default `github.token`) |
| `variables` | `list` | A list of variables to use in the header and footer. |

## Outputs

Expand All @@ -53,6 +54,8 @@ The header and footer have two special placeholders that will be replaced with t
- `{{version}}` will be replaced with the version number of the next release.
- `{{version-number}}` will be replaced with the version number of the next release without the `v` prefix.

Additionally, you can use the `variables` input to define additional variables that can be used in the header and footer. The variables are defined as a list of key-value pairs. The key is the name of the variable and the value is the value of the variable. The variables can be used in the header and footer by using the syntax `{{variable-name}}`.

## Examples

### Add Footer
Expand All @@ -74,9 +77,13 @@ jobs:
with:
minor-label: 'enhancement'
major-label: 'change'
variables: |
my-variable=My Variable
notes-footer: |
This is a footer.
It can be multiline.
And can use variables like {{version}} and {{version-number}}.
Or custom variables like {{my-variable}}.
```

### Get Version Number of Next Release
Expand Down
49 changes: 47 additions & 2 deletions __tests__/notes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {describe, expect, test} from '@jest/globals'
import {parseNotes} from '../src/notes'
import {describe, expect, test, it} from '@jest/globals'
import {parseNotes, generateReleaseNotes} from '../src/notes'
import * as github from '@actions/github'
import {Inputs} from '../src/context'
import {helpers} from 'handlebars'

jest.mock('@actions/core')
jest.mock('@actions/github')

let gh: ReturnType<typeof github.getOctokit>

describe('parseNotes', () => {
test('should return patch with empty labels', () => {
Expand Down Expand Up @@ -57,3 +65,40 @@ describe('parseNotes', () => {
expect(version).toEqual('major')
})
})

describe('generateReleaseNotes', () => {
beforeEach(() => {
jest.clearAllMocks()
gh = github.getOctokit('_')
})
it('should generate release notes for the given header and footer', async () => {
const inputs: Inputs = {
githubToken: '_',
majorLabel: 'major',
minorLabel: 'minor',
header: 'header with version-number {{version-number}} and foo {{foo}}',
footer: 'footer with version {{version}} and baz {{baz}}',
variables: ['foo=bar', 'baz=qux'],
}
const latestRelease = 'v1.0.0'
const nextRelease = 'v1.1.0'

const mockResponse: any = {
data: {
body: 'This is the body',
},
}

const mockNotes = jest.spyOn(gh.rest.repos, 'generateReleaseNotes')
mockNotes.mockResolvedValue(mockResponse)

// call the function
const notes = await generateReleaseNotes(gh, inputs, latestRelease, nextRelease)

// assert the result
expect(typeof notes).toEqual('string')
expect(notes).toContain('header with version-number 1.1.0 and foo bar')
expect(notes).toContain('This is the body')
expect(notes).toContain('footer with version v1.1.0 and baz qux')
})
})
1 change: 1 addition & 0 deletions __tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('createOrUpdateRelease', () => {
minorLabel: 'minor',
header: 'header',
footer: 'footer',
variables: [],
}
beforeEach(() => {
jest.clearAllMocks()
Expand Down
1 change: 1 addition & 0 deletions __tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('getVersionIncrease', () => {
githubToken: '',
header: '',
footer: '',
variables: [],
}

test('should return patch with empty labels (bug)', async () => {
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
description: 'Footer to use for changelog'
default: ''
required: false
variables:
description: 'List of variables to use in the header and footer'
default: ''
required: false
outputs:
version:
description: 'The version of the release'
Expand Down
Loading

0 comments on commit 06e0468

Please sign in to comment.