Skip to content

Commit

Permalink
Add config file support and dry run option (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed May 31, 2024
1 parent 396a5da commit 8a63d32
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 44 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,39 @@ jobs:
echo "Release URL: ${{ steps.draft-release.outputs.release-url }}"
echo "Release ID: ${{ steps.draft-release.outputs.release-id }}"
echo "Release Sections: ${{ steps.draft-release.outputs.release-sections }}"
test-dry-run-config:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./
id: draft-release
with:
config-path: .github/release-notes.yml
dry-run: true
variables: |
helm-chart=v0.1.1
foo=bar
my-variable=My Variable
notes-header: |
## Welcome to the {{version}} release of Draft Release
The previous release was {{previous-version}}.
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 }}"
echo "Previous Version: ${{ steps.draft-release.outputs.previous-version }}"
echo "Release Notes: ${{ steps.draft-release.outputs.release-notes }}"
echo "Release URL: ${{ steps.draft-release.outputs.release-url }}"
echo "Release ID: ${{ steps.draft-release.outputs.release-id }}"
echo "Release Sections: ${{ steps.draft-release.outputs.release-sections }}"
4 changes: 4 additions & 0 deletions __tests__/notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ describe('generateReleaseNotes', () => {
variables: ['foo=bar', 'baz=qux'],
collapseAfter: 0,
publish: false,
configPath: '.github/release.yml',
dryRun: false,
}
const releaseData = {
releases: [],
Expand Down Expand Up @@ -119,6 +121,8 @@ describe('generateReleaseNotes', () => {
variables: [],
collapseAfter: 3,
publish: false,
configPath: '.github/release.yml',
dryRun: false,
}

const releaseData = {
Expand Down
2 changes: 2 additions & 0 deletions __tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ describe('createOrUpdateRelease', () => {
variables: [],
collapseAfter: 0,
publish: false,
configPath: '.github/release.yml',
dryRun: false,
}
beforeEach(() => {
jest.clearAllMocks()
Expand Down
2 changes: 2 additions & 0 deletions __tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('getVersionIncrease', () => {
variables: [],
collapseAfter: 0,
publish: false,
configPath: '.github/release.yml',
dryRun: false,
}

const releaseData = {
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ inputs:
description: 'Whether to publish the release'
default: 'false'
required: false
config-path:
description: 'Path to the configuration file'
default: '.github/release.yml'
required: false
dry-run:
description: 'Whether to run the action without creating a release'
default: 'false'
required: false
outputs:
version:
description: 'The version of the release'
Expand Down
38 changes: 22 additions & 16 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.

4 changes: 4 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface Inputs {
variables: string[]
collapseAfter: number
publish: boolean
configPath: string
dryRun: boolean
}

export function getInputs(): Inputs {
Expand All @@ -22,5 +24,7 @@ export function getInputs(): Inputs {
variables: Util.getInputList('variables'),
collapseAfter: parseInt(core.getInput('collapse-after'), 10),
publish: core.getBooleanInput('publish'),
configPath: core.getInput('config-path'),
dryRun: core.getBooleanInput('dry-run'),
}
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function run(): Promise<void> {
core.setOutput('version', releaseData.nextRelease)

// create or update release

await createOrUpdateRelease(client, inputs, releaseData)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
Expand Down
4 changes: 3 additions & 1 deletion src/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function generateReleaseNotes(
const context = github.context
const latestRelease = releaseData.latestRelease
const nextRelease = releaseData.nextRelease
const configPath = inputs.configPath
let body = ''
let sections: SectionData = {}

Expand All @@ -30,6 +31,7 @@ export async function generateReleaseNotes(
tag_name: nextRelease,
previous_tag_name: semver.gt(latestRelease, '0.0.0') ? latestRelease : '',
target_commitish: releaseData.branch,
configuration_file_path: configPath,
})

body = notes.data.body
Expand All @@ -49,7 +51,7 @@ export async function generateReleaseNotes(
'previous-version-number': latestRelease.replace('v', ''),
...variables,
}
const categories = await getCategories()
const categories = await getCategories(inputs)
sections = await splitMarkdownSections(body, categories)

body = await collapseSections(body, sections, categories, inputs.collapseAfter)
Expand Down
43 changes: 23 additions & 20 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,26 @@ export async function createOrUpdateRelease(
core.debug(`releaseData.branch: ${releaseData.branch}`)
const newReleaseNotes = await generateReleaseNotes(client, inputs, releaseData)

const releaseParams = {
...context.repo,
tag_name: nextRelease,
name: nextRelease,
target_commitish: releaseData.branch,
body: newReleaseNotes,
draft: draft,
}
let response
if (!inputs.dryRun) {
const releaseParams = {
...context.repo,
tag_name: nextRelease,
name: nextRelease,
target_commitish: releaseData.branch,
body: newReleaseNotes,
draft: draft,
}

const response = await (releaseDraft === undefined
? client.rest.repos.createRelease({
...releaseParams,
})
: client.rest.repos.updateRelease({
...releaseParams,
release_id: releaseDraft.id,
}))
response = await (releaseDraft === undefined
? client.rest.repos.createRelease({
...releaseParams,
})
: client.rest.repos.updateRelease({
...releaseParams,
release_id: releaseDraft.id,
}))
}

const separator = '----------------------------------'
core.startGroup(`${releaseDraft === undefined ? 'Create' : 'Update'} release draft for ${nextRelease}`)
Expand All @@ -112,13 +115,13 @@ export async function createOrUpdateRelease(
core.info(separator)
core.info(`releaseNotes: ${newReleaseNotes}`)
core.info(separator)
core.info(`releaseURL: ${response.data?.html_url}`)
core.info(`releaseURL: ${response?.data?.html_url}`)
core.info(separator)
core.debug(`releaseDraft: ${JSON.stringify(releaseDraft, null, 2)}`)
core.debug(`${releaseDraft === undefined ? 'create' : 'update'}Release: ${JSON.stringify(response.data, null, 2)}`)
core.debug(`${releaseDraft === undefined ? 'create' : 'update'}Release: ${JSON.stringify(response?.data, null, 2)}`)
core.endGroup()

core.setOutput('release-notes', newReleaseNotes?.trim())
core.setOutput('release-id', response.data?.id)
core.setOutput('release-url', response.data?.html_url?.trim())
core.setOutput('release-id', response?.data?.id)
core.setOutput('release-url', response?.data?.html_url?.trim())
}
12 changes: 6 additions & 6 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export interface Category {
labels: string[]
}

export async function getCategories(): Promise<Category[]> {
const content = await fsPromises.readFile('.github/release.yml', 'utf8')
export async function getCategories(inputs: Inputs): Promise<Category[]> {
const content = await fsPromises.readFile(inputs.configPath, 'utf8')
const doc = yaml.load(content) as ReleaseYAML
return doc.changelog.categories.map((category) => {
return {
Expand All @@ -50,11 +50,11 @@ export async function getCategories(): Promise<Category[]> {
}

// function that returns tile for matching label
async function getTitleForLabel(label: string): Promise<string> {
async function getTitleForLabel(inputs: Inputs, label: string): Promise<string> {
if (label === '') {
return ''
}
const categories = await getCategories()
const categories = await getCategories(inputs)
const category = categories.find((category) => category.labels.includes(label))
if (category === undefined) {
return ''
Expand All @@ -64,8 +64,8 @@ async function getTitleForLabel(label: string): Promise<string> {

// function getVersionIncrease returns the version increase based on the labels. Major, minor, patch
export async function getVersionIncrease(releaseData: ReleaseData, inputs: Inputs, notes: string): Promise<string> {
const majorTitle = await getTitleForLabel(inputs.majorLabel)
const minorTitle = await getTitleForLabel(inputs.minorLabel)
const majorTitle = await getTitleForLabel(inputs, inputs.majorLabel)
const minorTitle = await getTitleForLabel(inputs, inputs.minorLabel)
const version = parseNotes(notes, majorTitle, minorTitle) as semver.ReleaseType

return semver.inc(releaseData.latestRelease, version) || ''
Expand Down

0 comments on commit 8a63d32

Please sign in to comment.