Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom variables input #62

Merged
merged 1 commit into from
Mar 27, 2023
Merged
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
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. These variables should be provided as a list of key-value pairs, using the format key=variable, with each pair separated by a new line. The key represents the variable name, while the value corresponds to the variable's assigned value. 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