From 714330612535ae25eb483d0f24fb2fe0c091dc86 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Wed, 1 Jul 2020 19:31:50 +0200 Subject: [PATCH] feat: Added versioning through GIT --- .github/workflows/test.yml | 18 ++++++++++++++++++ README.md | 12 +++++++++++- package.json | 1 + src/helpers/bumpVersion.js | 28 ++++++++++++++++++++++++++++ src/helpers/git.js | 6 +++--- src/index.js | 2 +- src/version/git.js | 34 ++++++++++++++++++++++++++++++++++ src/version/index.js | 5 +++-- src/version/json.js | 27 +++++++-------------------- yarn.lock | 2 +- 10 files changed, 107 insertions(+), 28 deletions(-) create mode 100644 src/helpers/bumpVersion.js create mode 100644 src/version/git.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b4218ab..902ee8b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,3 +18,21 @@ jobs: ENV: 'test' with: github-token: ${{ secrets.github_token }} + + + test-git: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'test' + with: + github-token: ${{ secrets.github_token }} + skip-commit: 'true' diff --git a/README.md b/README.md index a0c5599..a921725 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ Tag only skip-commit: 'true' ``` +Use a custom file for versioning + +```yaml +- name: Conventional Changelog Action + uses: TriPSs/conventional-changelog-action@v3 + with: + github-token: ${{ secrets.github_token }} + version-file: 'my-custom-file.yaml' +``` + Github releases ```yaml @@ -103,7 +113,7 @@ Github releases ## 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: - +> Make sure that `main: 'src/index.js'` is updated to `main: '../src/index.js'` inside the `action.yml` ```shell $ yarn install diff --git a/package.json b/package.json index 55bb379..53b21d1 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@actions/exec": "1.0.4", "conventional-changelog": "3.1.21", "conventional-recommended-bump": "6.0.9", + "git-semver-tags": "4.0.0", "object-path": "^0.11.4" } } diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js new file mode 100644 index 0000000..ec5d67c --- /dev/null +++ b/src/helpers/bumpVersion.js @@ -0,0 +1,28 @@ +/** + * Bumps the given version with the given release type + * + * @param releaseType + * @param version + * @returns {string} + */ +module.exports = (releaseType, version) => { + let [major, minor, patch] = version.split('.') + + switch (releaseType) { + case 'major': + major = parseInt(major, 10) + 1 + minor = 0 + patch = 0 + break + + case 'minor': + minor = parseInt(minor, 10) + 1 + patch = 0 + break + + default: + patch = parseInt(patch, 10) + 1 + } + + return `${major}.${minor}.${patch}` +} diff --git a/src/helpers/git.js b/src/helpers/git.js index 553512b..d5ae9fe 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -17,9 +17,9 @@ module.exports = new (class Git { const gitUserEmail = core.getInput('git-user-email') if (ENV === 'test') { - const noop = () => {console.log('skipping because of test env')} - - this.exec = noop + this.exec = (command) => { + console.log(`Skipping "git ${command}" because of test env`) + } } // Set config diff --git a/src/index.js b/src/index.js index fa8ce6e..0c0f049 100644 --- a/src/index.js +++ b/src/index.js @@ -67,7 +67,7 @@ async function run() { versioning.init(path.resolve(versionFile), versionPath) // Bump the version in the package.json - versioning.bump( + await versioning.bump( recommendation.releaseType, ) diff --git a/src/version/git.js b/src/version/git.js new file mode 100644 index 0000000..f1ba820 --- /dev/null +++ b/src/version/git.js @@ -0,0 +1,34 @@ +const core = require('@actions/core') +const gitSemverTags = require('git-semver-tags') + +const bumpVersion = require('../helpers/bumpVersion') + +module.exports = new (class Git { + + newVersion = null + + init = (fileLocation, versionPath) => {} + + bump = (releaseType) => { + return new Promise((resolve) => { + const tagPrefix = core.getInput('tag-prefix') + + gitSemverTags({ + tagPrefix, + }, (err, tags) => { + const currentVersion = tags.shift().replace(tagPrefix, '') + + // Get the new version + this.newVersion = bumpVersion( + releaseType, + currentVersion + ) + + // We are done + resolve() + }) + }) + } + +}) + diff --git a/src/version/index.js b/src/version/index.js index 142805a..c5e5e93 100644 --- a/src/version/index.js +++ b/src/version/index.js @@ -1,4 +1,5 @@ const JSON = require('./json') +const Git = require('./git') module.exports = (fileExtension) => { switch (fileExtension.toLowerCase()) { @@ -12,8 +13,8 @@ module.exports = (fileExtension) => { // case 'toml': // return Toml // - // case 'git': - // return Git + case 'git': + return Git default: return null diff --git a/src/version/json.js b/src/version/json.js index 73738b3..6e3bc17 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -1,6 +1,8 @@ const fs = require('fs') const objectPath = require('object-path') +const bumpVersion = require('../helpers/bumpVersion') + module.exports = new (class JSON { fileLocation = null @@ -33,26 +35,11 @@ module.exports = new (class JSON { // Read the JSON file const jsonFile = this.read() - let [major, minor, patch] = objectPath.get(jsonFile, this.versionPath).split('.') - - // TODO:: Move this to a helper - switch (releaseType) { - case 'major': - major = parseInt(major, 10) + 1 - minor = 0 - patch = 0 - break - - case 'minor': - minor = parseInt(minor, 10) + 1 - patch = 0 - break - - default: - patch = parseInt(patch, 10) + 1 - } - - this.newVersion = `${major}.${minor}.${patch}` + // Get the new version + this.newVersion = bumpVersion( + releaseType, + objectPath.get(jsonFile, this.versionPath), + ) // Update the json file with the new version objectPath.set(jsonFile, this.versionPath, this.newVersion) diff --git a/yarn.lock b/yarn.lock index be136cf..c35c774 100644 --- a/yarn.lock +++ b/yarn.lock @@ -483,7 +483,7 @@ git-remote-origin-url@^2.0.0: gitconfiglocal "^1.0.0" pify "^2.3.0" -git-semver-tags@^4.0.0: +git-semver-tags@4.0.0, git-semver-tags@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/git-semver-tags/-/git-semver-tags-4.0.0.tgz#a9dd58a0dd3561a4a9898b7e9731cf441c98fc38" integrity sha512-LajaAWLYVBff+1NVircURJFL8TQ3EMIcLAfHisWYX/nPoMwnTYfWAznQDmMujlLqoD12VtLmoSrF1sQ5MhimEQ==