fix: Fixes for pre changelog generation
parent
6a931f0306
commit
484cf10471
|
@ -1,8 +1,5 @@
|
||||||
name: 'Action to test the action locally with act'
|
name: 'Action to test the action locally with act'
|
||||||
on:
|
on: [pull_request]
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- branch-that-does-not-exist
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-json:
|
test-json:
|
||||||
|
@ -85,8 +82,8 @@ jobs:
|
||||||
skip-on-empty: 'false'
|
skip-on-empty: 'false'
|
||||||
|
|
||||||
- run: test -f pre-commit.test.json || (echo should be here && exit 1)
|
- run: test -f pre-commit.test.json || (echo should be here && exit 1)
|
||||||
- run: cat pre-commit.test.json
|
- run: cat pre-commit.test.json && echo ""
|
||||||
|
- run: cat ./package.json
|
||||||
|
|
||||||
test-pre-changelog-generation:
|
test-pre-changelog-generation:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
@ -105,9 +102,14 @@ jobs:
|
||||||
github-token: ${{ secrets.github_token }}
|
github-token: ${{ secrets.github_token }}
|
||||||
pre-changelog-generation: test/pre-changelog-generation.js
|
pre-changelog-generation: test/pre-changelog-generation.js
|
||||||
version-file: './test-file.toml'
|
version-file: './test-file.toml'
|
||||||
|
version-path: 'package.version'
|
||||||
|
|
||||||
|
- run: test -f pre-changelog-generation.version.test.json || (echo should be here && exit 1)
|
||||||
|
- run: test -f pre-changelog-generation.tag.test.json || (echo should be here && exit 1)
|
||||||
|
- run: cat pre-changelog-generation.version.test.json && echo ""
|
||||||
|
- run: cat pre-changelog-generation.tag.test.json && echo ""
|
||||||
|
- run: cat ./test-file.toml
|
||||||
|
|
||||||
- run: test -f pre-changelog-generation.test.json || (echo should be here && exit 1)
|
|
||||||
- run: cat pre-changelog-generation.test.json
|
|
||||||
test-git:
|
test-git:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
@ -132,7 +134,7 @@ jobs:
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
with:
|
with:
|
||||||
path: "./"
|
path: "./"
|
||||||
|
|
||||||
- run: git tag | xargs git tag -d
|
- run: git tag | xargs git tag -d
|
||||||
|
|
||||||
- name: Generate changelog
|
- name: Generate changelog
|
||||||
|
|
11
README.md
11
README.md
|
@ -58,17 +58,16 @@ A bunch of useful environment variables are available to the script with `proces
|
||||||
> Function in a specified file will be run right before the changelog generation phase, when the next
|
> Function in a specified file will be run right before the changelog generation phase, when the next
|
||||||
> version is already known, but it was not used anywhere yet. It can be useful if you want to manually update version or tag.
|
> version is already known, but it was not used anywhere yet. It can be useful if you want to manually update version or tag.
|
||||||
|
|
||||||
Same restrictions as for the pre-commit hook, but exported function name should be `preChangelogGeneration`
|
Same restrictions as for the pre-commit hook, but exported functions names should be `preVersionGeneration` for modifications to the version and `preTagGeneration` for modifications to the git tag.
|
||||||
|
|
||||||
Following props will be passed to the function as a single parameter and same output is expected:
|
Following props will be passed to the function as a single parameter and same output is expected:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface Props {
|
// Next version e.g. 1.12.3
|
||||||
tag: string; // Next tag e.g. v1.12.3
|
export function preVersionGeneration(version: string): string {}
|
||||||
version: string; // Next version e.g. 1.12.3
|
|
||||||
}
|
|
||||||
|
|
||||||
export function preChangelogGeneration(props: Props): Props {}
|
// Next tag e.g. v1.12.3
|
||||||
|
export function preTagGeneration(tag: string): string {}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Config-File-Path
|
### Config-File-Path
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const semverValid = require('semver').valid
|
const semverValid = require('semver').valid
|
||||||
|
|
||||||
|
const requireScript = require('./requireScript')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bumps the given version with the given release type
|
* Bumps the given version with the given release type
|
||||||
*
|
*
|
||||||
|
@ -44,5 +46,22 @@ module.exports = (releaseType, version) => {
|
||||||
core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`)
|
core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${major}.${minor}.${patch}`
|
const preChangelogGenerationFile = core.getInput('pre-changelog-generation')
|
||||||
|
|
||||||
|
let newVersion = `${major}.${minor}.${patch}`
|
||||||
|
|
||||||
|
if (preChangelogGenerationFile) {
|
||||||
|
const preChangelogGenerationScript = requireScript(preChangelogGenerationFile)
|
||||||
|
|
||||||
|
// Double check if we want to update / do something with the version
|
||||||
|
if (preChangelogGenerationScript && preChangelogGenerationScript.preVersionGeneration) {
|
||||||
|
const modifiedVersion = preChangelogGenerationScript.preVersionGeneration(newVersion)
|
||||||
|
|
||||||
|
if (modifiedVersion) {
|
||||||
|
newVersion = modifiedVersion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newVersion
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
const core = require('@actions/core')
|
||||||
|
const path = require('path')
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requires an script
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
*/
|
||||||
|
module.exports = (file) => {
|
||||||
|
const fileLocation = path.resolve(process.cwd(), file)
|
||||||
|
|
||||||
|
// Double check the script exists before loading it
|
||||||
|
if (fs.existsSync(fileLocation)) {
|
||||||
|
core.info(`Loading "${fileLocation}" script`)
|
||||||
|
|
||||||
|
return require(fileLocation)
|
||||||
|
}
|
||||||
|
|
||||||
|
core.error(`Tried to load "${fileLocation}" script but it does not exists!`)
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
58
src/index.js
58
src/index.js
|
@ -5,6 +5,7 @@ const path = require('path')
|
||||||
const getVersioning = require('./version')
|
const getVersioning = require('./version')
|
||||||
const git = require('./helpers/git')
|
const git = require('./helpers/git')
|
||||||
const changelog = require('./helpers/generateChangelog')
|
const changelog = require('./helpers/generateChangelog')
|
||||||
|
const requireScript = require('./helpers/requireScript')
|
||||||
|
|
||||||
async function handleVersioningByExtension(ext, file, versionPath, releaseType) {
|
async function handleVersioningByExtension(ext, file, versionPath, releaseType) {
|
||||||
const versioning = getVersioning(ext)
|
const versioning = getVersioning(ext)
|
||||||
|
@ -29,7 +30,7 @@ async function run() {
|
||||||
const gitUserEmail = core.getInput('git-user-email')
|
const gitUserEmail = core.getInput('git-user-email')
|
||||||
const tagPrefix = core.getInput('tag-prefix')
|
const tagPrefix = core.getInput('tag-prefix')
|
||||||
const preset = !core.getInput('config-file-path') ? core.getInput('preset') : ''
|
const preset = !core.getInput('config-file-path') ? core.getInput('preset') : ''
|
||||||
const preCommit = core.getInput('pre-commit')
|
const preCommitFile = core.getInput('pre-commit')
|
||||||
const outputFile = core.getInput('output-file')
|
const outputFile = core.getInput('output-file')
|
||||||
const releaseCount = core.getInput('release-count')
|
const releaseCount = core.getInput('release-count')
|
||||||
const versionFile = core.getInput('version-file')
|
const versionFile = core.getInput('version-file')
|
||||||
|
@ -38,7 +39,7 @@ async function run() {
|
||||||
const skipCommit = core.getInput('skip-commit').toLowerCase() === 'true'
|
const skipCommit = core.getInput('skip-commit').toLowerCase() === 'true'
|
||||||
const skipEmptyRelease = core.getInput('skip-on-empty').toLowerCase() === 'true'
|
const skipEmptyRelease = core.getInput('skip-on-empty').toLowerCase() === 'true'
|
||||||
const conventionalConfigFile = core.getInput('config-file-path')
|
const conventionalConfigFile = core.getInput('config-file-path')
|
||||||
const preChangelogGeneration = core.getInput('pre-changelog-generation')
|
const preChangelogGenerationFile = core.getInput('pre-changelog-generation')
|
||||||
|
|
||||||
core.info(`Using "${preset}" preset`)
|
core.info(`Using "${preset}" preset`)
|
||||||
core.info(`Using "${gitCommitMessage}" as commit message`)
|
core.info(`Using "${gitCommitMessage}" as commit message`)
|
||||||
|
@ -51,12 +52,12 @@ async function run() {
|
||||||
core.info(`Using "${outputFile}" as output file`)
|
core.info(`Using "${outputFile}" as output file`)
|
||||||
core.info(`Using "${conventionalConfigFile}" as config file`)
|
core.info(`Using "${conventionalConfigFile}" as config file`)
|
||||||
|
|
||||||
if (preCommit) {
|
if (preCommitFile) {
|
||||||
core.info(`Using "${preCommit}" as pre-commit script`)
|
core.info(`Using "${preCommitFile}" as pre-commit script`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preChangelogGeneration) {
|
if (preChangelogGenerationFile) {
|
||||||
core.info(`Using "${preChangelogGeneration}" as pre-changelog-generation script`)
|
core.info(`Using "${preChangelogGenerationFile}" as pre-changelog-generation script`)
|
||||||
}
|
}
|
||||||
|
|
||||||
core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`)
|
core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`)
|
||||||
|
@ -65,9 +66,9 @@ async function run() {
|
||||||
core.info('Pull to make sure we have the full git history')
|
core.info('Pull to make sure we have the full git history')
|
||||||
await git.pull()
|
await git.pull()
|
||||||
|
|
||||||
const config = conventionalConfigFile && require(path.resolve(process.cwd(), conventionalConfigFile))
|
const config = conventionalConfigFile && requireScript(conventionalConfigFile)
|
||||||
|
|
||||||
conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => {
|
conventionalRecommendedBump({ preset, tagPrefix, config }, async(error, recommendation) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
core.setFailed(error.message)
|
core.setFailed(error.message)
|
||||||
return
|
return
|
||||||
|
@ -91,9 +92,11 @@ async function run() {
|
||||||
'git',
|
'git',
|
||||||
versionFile,
|
versionFile,
|
||||||
versionPath,
|
versionPath,
|
||||||
recommendation.releaseType
|
recommendation.releaseType,
|
||||||
)
|
)
|
||||||
|
|
||||||
newVersion = versioning.newVersion
|
newVersion = versioning.newVersion
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
const files = versionFile.split(',').map((f) => f.trim())
|
const files = versionFile.split(',').map((f) => f.trim())
|
||||||
core.info(`Files to bump: ${files.join(', ')}`)
|
core.info(`Files to bump: ${files.join(', ')}`)
|
||||||
|
@ -102,8 +105,9 @@ async function run() {
|
||||||
files.map((file) => {
|
files.map((file) => {
|
||||||
const fileExtension = file.split('.').pop()
|
const fileExtension = file.split('.').pop()
|
||||||
core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`)
|
core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`)
|
||||||
|
|
||||||
return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType)
|
return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType)
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
newVersion = versioning[0].newVersion
|
newVersion = versioning[0].newVersion
|
||||||
|
@ -111,15 +115,16 @@ async function run() {
|
||||||
|
|
||||||
let gitTag = `${tagPrefix}${newVersion}`
|
let gitTag = `${tagPrefix}${newVersion}`
|
||||||
|
|
||||||
if (preChangelogGeneration) {
|
if (preChangelogGenerationFile) {
|
||||||
const newVersionAndTag = await require(path.resolve(process.cwd(), preChangelogGeneration)).preChangelogGeneration({
|
const preChangelogGenerationScript = requireScript(preChangelogGenerationFile)
|
||||||
tag: gitTag,
|
|
||||||
version: newVersion,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (newVersionAndTag) {
|
// Double check if we want to update / do something with the tag
|
||||||
if (newVersionAndTag.tag) gitTag = newVersionAndTag.tag
|
if (preChangelogGenerationScript && preChangelogGenerationScript.preTagGeneration) {
|
||||||
if (newVersionAndTag.version) newVersion = newVersionAndTag.version
|
const modifiedTag = preChangelogGenerationScript.preTagGeneration(gitTag)
|
||||||
|
|
||||||
|
if (modifiedTag) {
|
||||||
|
gitTag = modifiedTag
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,12 +152,18 @@ async function run() {
|
||||||
|
|
||||||
if (!skipCommit) {
|
if (!skipCommit) {
|
||||||
// Add changed files to git
|
// Add changed files to git
|
||||||
if (preCommit) {
|
if (preCommitFile) {
|
||||||
await require(path.resolve(process.cwd(), preCommit)).preCommit({
|
const preCommitScript = await requireScript(preCommitFile)
|
||||||
tag: gitTag,
|
|
||||||
version: newVersion,
|
// Double check if the file exists and the export exists
|
||||||
})
|
if (preCommitScript && preCommitScript.preCommit) {
|
||||||
|
preCommitScript.preCommit({
|
||||||
|
tag: gitTag,
|
||||||
|
version: newVersion,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await git.add('.')
|
await git.add('.')
|
||||||
await git.commit(gitCommitMessage.replace('{version}', gitTag))
|
await git.commit(gitCommitMessage.replace('{version}', gitTag))
|
||||||
}
|
}
|
||||||
|
@ -163,6 +174,7 @@ async function run() {
|
||||||
core.info('Push all changes')
|
core.info('Push all changes')
|
||||||
try {
|
try {
|
||||||
await git.push()
|
await git.push()
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message)
|
core.setFailed(error.message)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,29 @@
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const t = require('assert')
|
const t = require('assert')
|
||||||
|
|
||||||
exports.preChangelogGeneration = (props) => {
|
exports.preVersionGeneration = (version) => {
|
||||||
const { GITHUB_WORKSPACE } = process.env
|
const { GITHUB_WORKSPACE } = process.env
|
||||||
|
|
||||||
t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty')
|
t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty')
|
||||||
t.ok(props.tag, 'tag should not be empty')
|
t.ok(version, 'version should not be empty')
|
||||||
t.ok(props.version, 'version should not be empty')
|
|
||||||
|
|
||||||
const newVersion = '1.0.100'
|
const newVersion = '1.0.100'
|
||||||
const newTag = 'v1.0.100'
|
|
||||||
|
|
||||||
const body = {
|
fs.writeFileSync('pre-changelog-generation.version.test.json', newVersion)
|
||||||
version: newVersion,
|
|
||||||
tag: newTag,
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.writeFileSync('pre-changelog-generation.test.json', JSON.stringify(body, null, 2))
|
return newVersion
|
||||||
|
}
|
||||||
return body
|
|
||||||
|
exports.preTagGeneration = (tag) => {
|
||||||
|
const { GITHUB_WORKSPACE } = process.env
|
||||||
|
|
||||||
|
t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty')
|
||||||
|
t.ok(tag, 'tag should not be empty')
|
||||||
|
t.strictEqual(tag, 'v1.0.100')
|
||||||
|
|
||||||
|
const newTag = 'v1.0.100-alpha'
|
||||||
|
|
||||||
|
fs.writeFileSync('pre-changelog-generation.tag.test.json', newTag)
|
||||||
|
|
||||||
|
return newTag
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue