Skip to content

Commit

Permalink
Add tag support (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Apr 7, 2023
1 parent c1b2a17 commit df60c8f
Show file tree
Hide file tree
Showing 13 changed files with 3,030 additions and 2,776 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ Thumbs.db

# Ignore built ts files
__tests__/runner/*
lib/**/*
lib/**/*
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ This action creates a draft release for the next version to be released. It read

To use this action, you need to create a release file in `.github/release.yml` as shown in the GitHub documentation for [creating a release file](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes#configuring-automatically-generated-release-notes).

> **Note:**
>
> This action requires read and write access to the repository's releases. You might need to change the permissions granted to the GITHUB_TOKEN or use a personal token with the appropriate permissions.
To decide whether the next release should be a major or minor release, the action looks at the labels of the pull requests merged since the last release. If there is at least one pull request with the label specified in the `major-label` input, the next release will be a major release. Otherwise, if there is at least one pull request with the label specified in the `minor-label` input, the next release will be a minor release. Otherwise, the next release will be a patch release.

When the action is triggered on a tag push, the action will create a release with the version number specified in the tag.

## Simple Usage

```yaml
Expand Down Expand Up @@ -48,7 +54,7 @@ jobs:
| `release-id` | `string` | The ID of the next release. |
| `release-notes` | `string` | The release notes of the next release. |
| `release-url` | `string` | The URL of the next release. |
| `release-sections` | `JSON` | A JSON object containing the release sections and the pull requests in each section. |
| `release-sections` | `string` | A JSON output containing the release sections and the pull requests in each section. |

## Header and Footer

Expand All @@ -71,6 +77,9 @@ on:
push:
branches:
- main
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'


jobs:
draft-release:
Expand Down
20 changes: 14 additions & 6 deletions __tests__/notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ describe('generateReleaseNotes', () => {
variables: ['foo=bar', 'baz=qux'],
collapseAfter: 0,
}
const latestRelease = 'v1.0.0'
const nextRelease = 'v1.1.0'
const releaseData = {
releases: [],
latestRelease: 'v1.0.0',
branch: 'main',
nextRelease: 'v1.1.0',
}

const mockResponse: any = {
data: {
Expand All @@ -95,7 +99,7 @@ describe('generateReleaseNotes', () => {
mockNotes.mockResolvedValue(mockResponse)

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

// assert the result
expect(typeof notes).toEqual('string')
Expand All @@ -115,8 +119,12 @@ describe('generateReleaseNotes', () => {
collapseAfter: 3,
}

const latestRelease = 'v1.0.0'
const nextRelease = 'v1.1.0'
const releaseData = {
releases: [],
latestRelease: 'v1.0.0',
branch: 'main',
nextRelease: 'v1.1.0',
}

const mockResponse: any = {
data: {
Expand Down Expand Up @@ -160,7 +168,7 @@ describe('generateReleaseNotes', () => {
mockNotes.mockResolvedValue(mockResponse)

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

// assert the result
expect(typeof notes).toEqual('string')
Expand Down
44 changes: 29 additions & 15 deletions __tests__/release.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getRelease, createOrUpdateRelease} from '../src/release'
import {getRelease, createOrUpdateRelease, ReleaseData} from '../src/release'
import * as github from '@actions/github'
import {Inputs} from '../src/context'

Expand Down Expand Up @@ -41,10 +41,10 @@ describe('getRelease', () => {
const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases')
mockReleases.mockResolvedValue(mockResponse)

const [releases, latestRelease] = await getRelease(gh)
const releaseData = await getRelease(gh)

expect(releases).toHaveLength(3)
expect(latestRelease).toBe('v1.0.2')
expect(releaseData.releases).toHaveLength(3)
expect(releaseData.latestRelease).toBe('v1.0.2')
})

it('should return the latest for the current branch', async () => {
Expand Down Expand Up @@ -73,10 +73,10 @@ describe('getRelease', () => {
const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases')
mockReleases.mockResolvedValue(mockResponse)

const [releases, latestRelease] = await getRelease(gh)
const releaseData = await getRelease(gh)

expect(releases).toHaveLength(3)
expect(latestRelease).toBe('v1.0.1')
expect(releaseData.releases).toHaveLength(3)
expect(releaseData.latestRelease).toBe('v1.0.1')
})

it('should return the latest non-draft release', async () => {
Expand Down Expand Up @@ -105,10 +105,10 @@ describe('getRelease', () => {
const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases')
mockReleases.mockResolvedValue(mockResponse)

const [releases, latestRelease] = await getRelease(gh)
const releaseData = await getRelease(gh)

expect(releases).toHaveLength(3)
expect(latestRelease).toBe('v1.0.0')
expect(releaseData.releases).toHaveLength(3)
expect(releaseData.latestRelease).toBe('v1.0.0')
})

it('should return v0.0.0 when no releases exist', async () => {
Expand All @@ -121,10 +121,10 @@ describe('getRelease', () => {
const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases')
mockReleases.mockResolvedValue(mockResponse)

const [releases, latestRelease] = await getRelease(gh)
const releaseData = await getRelease(gh)

expect(releases).toHaveLength(0)
expect(latestRelease).toBe('v0.0.0')
expect(releaseData.releases).toHaveLength(0)
expect(releaseData.latestRelease).toBe('v0.0.0')
})
})

Expand Down Expand Up @@ -197,7 +197,14 @@ describe('createOrUpdateRelease', () => {
const mockReleaseNotes = jest.spyOn(gh.rest.repos, 'generateReleaseNotes')
mockReleaseNotes.mockResolvedValue(mockNotes)

const response = await createOrUpdateRelease(gh, inputs, mockInputCreate.data, 'v1.0.0', 'v1.0.1')
const releaseData: ReleaseData = {
latestRelease: 'v1.0.0',
releases: mockInputCreate.data,
branch: 'main',
nextRelease: 'v1.0.1',
}

const response = await createOrUpdateRelease(gh, inputs, releaseData)

expect(mockReleases).toHaveBeenCalledTimes(1)
})
Expand All @@ -222,6 +229,13 @@ describe('createOrUpdateRelease', () => {
],
}

const releaseData: ReleaseData = {
latestRelease: 'v1.0.0',
releases: mockInputUpdate.data,
branch: 'main',
nextRelease: 'v1.0.1',
}

const mockReleases = jest.spyOn(gh.rest.repos, 'updateRelease')
mockReleases.mockResolvedValue(mockResponse)

Expand All @@ -231,7 +245,7 @@ describe('createOrUpdateRelease', () => {
const mockReleaseNotes = jest.spyOn(gh.rest.repos, 'generateReleaseNotes')
mockReleaseNotes.mockResolvedValue(mockNotes)

const response = await createOrUpdateRelease(gh, inputs, mockInputUpdate.data, 'v1.0.0', 'v1.0.1')
const response = await createOrUpdateRelease(gh, inputs, releaseData)

expect(mockReleases).toHaveBeenCalledTimes(1)
})
Expand Down
18 changes: 13 additions & 5 deletions __tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@ describe('getVersionIncrease', () => {
collapseAfter: 0,
}

const releaseData = {
releases: [],
latestRelease: '1.0.0',
branch: 'main',
nextRelease: '1.0.1',
}

test('should return patch with empty labels (bug)', async () => {
let version = await getVersionIncrease('1.0.0', fakeInputs, '### πŸ› Bug Fixes')
let version = await getVersionIncrease(releaseData, fakeInputs, '### πŸ› Bug Fixes')
expect(version).toEqual('1.0.1')
})
test('should return patch with empty labels (feature)', async () => {
let version = await getVersionIncrease('1.0.0', fakeInputs, '### πŸš€ Features')
let version = await getVersionIncrease(releaseData, fakeInputs, '### πŸš€ Features')
expect(version).toEqual('1.0.1')
})
test('should return patch with empty labels (change)', async () => {
let version = await getVersionIncrease('1.0.0', fakeInputs, '### πŸ’£ Breaking Changes')
let version = await getVersionIncrease(releaseData, fakeInputs, '### πŸ’£ Breaking Changes')
expect(version).toEqual('1.0.1')
})

test('should return minor', async () => {
fakeInputs.minorLabel = 'enhancement'
fakeInputs.majorLabel = 'change'

let version = await getVersionIncrease(
'1.0.0',
releaseData,
fakeInputs,
`
### πŸš€ Features
Expand All @@ -45,7 +53,7 @@ describe('getVersionIncrease', () => {
fakeInputs.minorLabel = 'bug'
fakeInputs.majorLabel = 'change'
let version = await getVersionIncrease(
'1.0.0',
releaseData,
fakeInputs,
`
### πŸ’£ Breaking Changes
Expand Down
Loading

0 comments on commit df60c8f

Please sign in to comment.