Merge pull request #54 from TriPSs/master

Release
releases/v3
Tycho Bokdam 2020-08-13 08:18:14 +02:00 committed by GitHub
commit 38281da7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 268 additions and 59 deletions

View File

@ -21,6 +21,49 @@ jobs:
with: with:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
test-json-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.json'
- name: Show file
id: show
run: |
echo "$(<test-file-new.json)"
test-json-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: touch ./test-file-empty.json
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.json'
- run: echo "$(<./test-file-empty.json)"
test-pre-commit: test-pre-commit:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@ -61,6 +104,25 @@ jobs:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
skip-commit: 'true' skip-commit: 'true'
test-git-fallback:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: git tag | xargs git tag -d
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
skip-commit: 'true'
test-yaml: test-yaml:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@ -79,6 +141,48 @@ jobs:
version-file: 'test-file.yaml' version-file: 'test-file.yaml'
version-path: 'package.version' version-path: 'package.version'
test-yaml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: touch ./test-file-empty.yaml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.yaml'
version-path: 'package.version'
- run: echo "$(<test-file-new.yaml)"
test-yaml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.yaml'
version-path: 'package.version'
- run: echo "$(<./test-file-empty.yaml)"
test-toml: test-toml:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@ -97,6 +201,48 @@ jobs:
version-file: 'test-file.toml' version-file: 'test-file.toml'
version-path: 'package.version' version-path: 'package.version'
test-toml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.toml'
version-path: 'package.version'
- run: echo "$(<test-file-new.toml)"
test-toml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: touch ./test-file-empty.toml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: './test/test-file-empty.toml'
version-path: 'package.version'
- run: echo "$(<./test/test-file-empty.toml)"
test-multiple-files: test-multiple-files:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@ -15,10 +15,11 @@ This action will bump version, tag commit and generate a changelog with conventi
- **Optional** `release-count`: Number of releases to preserve in changelog. Default `5`, use `0` to regenerate all. - **Optional** `release-count`: Number of releases to preserve in changelog. Default `5`, use `0` to regenerate all.
- **Optional** `version-file`: The path to the file that contains the version to bump. Default `./package.json`. - **Optional** `version-file`: The path to the file that contains the version to bump. Default `./package.json`.
- **Optional** `version-path`: The place inside the version file to bump. Default `version`. - **Optional** `version-path`: The place inside the version file to bump. Default `version`.
- **Optional** `skip-on-empty`: Boolean to specify if you want to skip empty release (no-changelog generated). This case occured when you push `chore` commit with `angular` for example. Default `'false'`. - **Optional** `skip-on-empty`: Boolean to specify if you want to skip empty release (no-changelog generated). This case occured when you push `chore` commit with `angular` for example. Default `'true'`.
- **Optional** `skip-version-file`: Do not update the version file. Default `'false'`. - **Optional** `skip-version-file`: Do not update the version file. Default `'false'`.
- **Optional** `skip-commit`: Do create a release commit. Default `'false'`. - **Optional** `skip-commit`: Do create a release commit. Default `'false'`.
- **Optional** `pre-commit`: Path to the pre-commit script file. No hook by default. - **Optional** `pre-commit`: Path to the pre-commit script file. No hook by default.
- **Optional** `fallback-version`: The fallback version, if no older one can be detected, or if it is the first one. Default `'0.1.0'`
### Pre-Commit hook ### Pre-Commit hook
@ -29,6 +30,7 @@ This action will bump version, tag commit and generate a changelog with conventi
Specified path could be relative or absolute. If it is relative, then it will be based on the `GITHUB_WORKSPACE` path. Specified path could be relative or absolute. If it is relative, then it will be based on the `GITHUB_WORKSPACE` path.
Script should: Script should:
- be a CommonJS module - be a CommonJS module
- have a single export: `exports.preCommit = (props) => { /* ... */ }` - have a single export: `exports.preCommit = (props) => { /* ... */ }`
- not have any return value - not have any return value
@ -96,7 +98,7 @@ No file changelog
uses: TriPSs/conventional-changelog-action@v3 uses: TriPSs/conventional-changelog-action@v3
with: with:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
output-file: 'false' output-file: "false"
``` ```
Tag only Tag only
@ -106,7 +108,7 @@ Tag only
uses: TriPSs/conventional-changelog-action@v3 uses: TriPSs/conventional-changelog-action@v3
with: with:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
skip-commit: 'true' skip-commit: "true"
``` ```
Use a custom file for versioning Use a custom file for versioning
@ -116,7 +118,7 @@ Use a custom file for versioning
uses: TriPSs/conventional-changelog-action@v3 uses: TriPSs/conventional-changelog-action@v3
with: with:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
version-file: 'my-custom-file.yaml' version-file: "my-custom-file.yaml"
``` ```
Use a pre-commit hook Use a pre-commit hook
@ -137,8 +139,7 @@ Github releases
uses: TriPSs/conventional-changelog-action@v3 uses: TriPSs/conventional-changelog-action@v3
with: with:
github-token: ${{ secrets.github_token }} github-token: ${{ secrets.github_token }}
output-file: 'false' output-file: "false"
skip-on-empty: 'true'
- name: Create Release - name: Create Release
uses: actions/create-release@v1 uses: actions/create-release@v1
@ -152,9 +153,12 @@ Github releases
``` ```
## Development ## Development
If you'd like to contribute to this project, all you need to do is clone and install [act](https://github.com/nektos/act) this project and run: If you'd like to contribute to this project, all you need to do is clone and install [act](https://github.com/nektos/act) this project and run:
> Make sure that `main: 'src/index.js'` is updated to `main: '../src/index.js'` inside the `action.yml` > Make sure that `main: 'src/index.js'` is updated to `main: '../src/index.js'` inside the `action.yml`
> Note: The image used is 18 gb! > Note: The image used is 18 gb!
```shell ```shell
$ yarn install $ yarn install
@ -164,12 +168,18 @@ $ act -j test-json -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s gith
# To run / test git versioning # To run / test git versioning
$ act -j test-git -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token $ act -j test-git -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token
# To run / test git fallback versioning
$ act -j test-git-fallback -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token
# To run / test yaml versioning # To run / test yaml versioning
$ act -j test-yaml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token $ act -j test-yaml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token
# To run / toml git versioning # To run / test toml versioning
$ act -j test-toml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token $ act -j test-toml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token
# To run / test empty / new files test
$ act -j test-[json/toml/yaml]-[empty/new] -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token
# To run pre-commit test # To run pre-commit test
$ act -j test-pre-commit -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token $ act -j test-pre-commit -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token

View File

@ -83,6 +83,11 @@ inputs:
description: 'Path to the pre-commit script file' description: 'Path to the pre-commit script file'
required: false required: false
fallback-version:
description: 'The fallback version, if no older one can be detected, or if it is the first one'
default: '0.1.0'
required: false
outputs: outputs:
changelog: changelog:

10
package-lock.json generated
View File

@ -372,6 +372,11 @@
"find-up": "^4.0.0" "find-up": "^4.0.0"
} }
}, },
"@iarna/toml": {
"version": "2.2.5",
"resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz",
"integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg=="
},
"@marionebl/sander": { "@marionebl/sander": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/@marionebl/sander/-/sander-0.6.1.tgz", "resolved": "https://registry.npmjs.org/@marionebl/sander/-/sander-0.6.1.tgz",
@ -2238,11 +2243,6 @@
"readable-stream": "2 || 3" "readable-stream": "2 || 3"
} }
}, },
"toml": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz",
"integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w=="
},
"trim-newlines": { "trim-newlines": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.0.tgz", "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.0.tgz",

View File

@ -26,11 +26,12 @@
"dependencies": { "dependencies": {
"@actions/core": "1.2.4", "@actions/core": "1.2.4",
"@actions/exec": "1.0.4", "@actions/exec": "1.0.4",
"@iarna/toml": "^2.2.5",
"conventional-changelog": "3.1.21", "conventional-changelog": "3.1.21",
"conventional-recommended-bump": "6.0.9", "conventional-recommended-bump": "6.0.9",
"git-semver-tags": "4.0.0", "git-semver-tags": "4.0.0",
"object-path": "^0.11.4", "object-path": "^0.11.4",
"toml": "^3.0.0", "semver": "^6.0.0",
"yaml": "^1.10.0" "yaml": "^1.10.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,3 +1,6 @@
const core = require('@actions/core')
const semverValid = require('semver').valid
/** /**
* Bumps the given version with the given release type * Bumps the given version with the given release type
* *
@ -6,7 +9,10 @@
* @returns {string} * @returns {string}
*/ */
module.exports = (releaseType, version) => { module.exports = (releaseType, version) => {
let [major, minor, patch] = version.split('.') let major, minor, patch
if (version) {
[major, minor, patch] = version.split('.')
switch (releaseType) { switch (releaseType) {
case 'major': case 'major':
@ -23,6 +29,20 @@ module.exports = (releaseType, version) => {
default: default:
patch = parseInt(patch, 10) + 1 patch = parseInt(patch, 10) + 1
} }
} else {
let version = semverValid(core.getInput('fallback-version'))
if (version) {
[major, minor, patch] = version.split('.')
} else {
// default
major = 0
minor = 1
patch = 0
}
core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`)
}
return `${major}.${minor}.${patch}` return `${major}.${minor}.${patch}`
} }

View File

@ -97,7 +97,7 @@ module.exports = new (class Git {
* @return {Promise<>} * @return {Promise<>}
*/ */
pull = () => ( pull = () => (
this.exec(`pull --unshallow ${core.getInput('git-pull-method')}`) this.exec(`pull --unshallow --tags ${core.getInput('git-pull-method')}`)
) )
/** /**

View File

@ -25,7 +25,7 @@ module.exports = class BaseVersioning {
* @return {string} * @return {string}
*/ */
read = () => { read = () => {
return fs.readFileSync(this.fileLocation, 'utf8') return fs.existsSync(this.fileLocation) ? fs.readFileSync(this.fileLocation, 'utf8') : ''
} }
/** /**

View File

@ -13,7 +13,7 @@ module.exports = new (class Git extends BaseVersioning {
gitSemverTags({ gitSemverTags({
tagPrefix, tagPrefix,
}, (err, tags) => { }, (err, tags) => {
const currentVersion = tags.shift().replace(tagPrefix, '') const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null
// Get the new version // Get the new version
this.newVersion = bumpVersion( this.newVersion = bumpVersion(

View File

@ -1,3 +1,4 @@
const core = require('@actions/core')
const objectPath = require('object-path') const objectPath = require('object-path')
const BaseVersioning = require('./base') const BaseVersioning = require('./base')
@ -14,8 +15,22 @@ module.exports = new (class Json extends BaseVersioning {
bump = (releaseType) => { bump = (releaseType) => {
// Read the file // Read the file
const fileContent = this.read() const fileContent = this.read()
const jsonContent = JSON.parse(fileContent)
const oldVersion = objectPath.get(jsonContent, this.versionPath) // Parse the file
let jsonContent
try {
jsonContent = JSON.parse(fileContent)
} catch (error) {
core.startGroup(`Error when parsing the file '${this.fileLocation}'`)
core.info(`File-Content: ${fileContent}`)
core.info(error) // should be 'warning' ?
core.endGroup()
jsonContent = {}
}
// Get the old version
const oldVersion = objectPath.get(jsonContent, this.versionPath, null)
// Get the new version // Get the new version
this.newVersion = bumpVersion( this.newVersion = bumpVersion(

View File

@ -1,5 +1,5 @@
const objectPath = require('object-path') const objectPath = require('object-path')
const toml = require('toml') const toml = require('@iarna/toml')
const BaseVersioning = require('./base') const BaseVersioning = require('./base')
const bumpVersion = require('../helpers/bumpVersion') const bumpVersion = require('../helpers/bumpVersion')
@ -16,7 +16,7 @@ module.exports = new (class Toml extends BaseVersioning{
// Read the file // Read the file
const fileContent = this.read() const fileContent = this.read()
const tomlContent = toml.parse(fileContent) const tomlContent = toml.parse(fileContent)
const oldVersion = objectPath.get(tomlContent, this.versionPath) const oldVersion = objectPath.get(tomlContent, this.versionPath, null)
// Get the new version // Get the new version
this.newVersion = bumpVersion( this.newVersion = bumpVersion(
@ -24,17 +24,23 @@ module.exports = new (class Toml extends BaseVersioning{
oldVersion, oldVersion,
) )
// Update the file
if (oldVersion) {
// Get the name of where the version is in // Get the name of where the version is in
const versionName = this.versionPath.split('.').pop() const versionName = this.versionPath.split('.').pop()
// Update the file
this.update( this.update(
// We use replace so we can preserve white spaces and comments // We use replace instead of yaml.stringify so we can preserve white spaces and comments
fileContent.replace( fileContent.replace(
`${versionName} = "${oldVersion}"`, `${versionName} = "${oldVersion}"`,
`${versionName} = "${this.newVersion}"`, `${versionName} = "${this.newVersion}"`,
), ),
) )
} else {
// Update the content with the new version
objectPath.set(tomlContent, this.versionPath, this.newVersion)
this.update(toml.stringify(tomlContent))
}
} }
}) })

View File

@ -15,8 +15,8 @@ module.exports = new (class Yaml extends BaseVersioning{
bump = (releaseType) => { bump = (releaseType) => {
// Read the file // Read the file
const fileContent = this.read() const fileContent = this.read()
const yamlContent = yaml.parse(fileContent) const yamlContent = yaml.parse(fileContent) || {}
const oldVersion = objectPath.get(yamlContent, this.versionPath) const oldVersion = objectPath.get(yamlContent, this.versionPath, null)
// Get the new version // Get the new version
this.newVersion = bumpVersion( this.newVersion = bumpVersion(
@ -24,10 +24,11 @@ module.exports = new (class Yaml extends BaseVersioning{
oldVersion, oldVersion,
) )
// Update the file
if (oldVersion) {
// Get the name of where the version is in // Get the name of where the version is in
const versionName = this.versionPath.split('.').pop() const versionName = this.versionPath.split('.').pop()
// Update the file
this.update( this.update(
// We use replace instead of yaml.stringify so we can preserve white spaces and comments // We use replace instead of yaml.stringify so we can preserve white spaces and comments
fileContent.replace( fileContent.replace(
@ -35,6 +36,11 @@ module.exports = new (class Yaml extends BaseVersioning{
`${versionName}: '${this.newVersion}'`, `${versionName}: '${this.newVersion}'`,
), ),
) )
} else {
// Update the content with the new version
objectPath.set(yamlContent, this.versionPath, this.newVersion)
this.update(yaml.stringify(yamlContent))
}
} }
}) })