Skip to content

Commit

Permalink
feat: include more run options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Nassri committed Sep 4, 2022
1 parent 391e052 commit 1d57161
Showing 1 changed file with 77 additions and 47 deletions.
124 changes: 77 additions & 47 deletions action/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,80 @@
const core = require('@actions/core')
const release = require('semantic-release')
import core from '@actions/core'
import release from 'semantic-release'
import { resolve } from 'node:path'

// semver regex
const semver = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/

const config = core.getInput('config')

const options = config ? require(config) : {}

release(options)
.then(result => {
// exit early
if (!result) return core.setOutput('published', 'false')

// get result details
const { lastRelease, nextRelease } = result

lastRelease.parsed = semver.exec(lastRelease.version)

// outputs
core.setOutput('last-release-git-head', lastRelease.gitHead)
core.setOutput('last-release-git-tag', lastRelease.gitTag)
core.setOutput('last-release-channel', lastRelease.channel)
core.setOutput('last-release-version', lastRelease.version)

if (lastRelease.parsed) {
core.setOutput('last-release-version-major', lastRelease.parsed.groups.major)
core.setOutput('last-release-version-minor', lastRelease.parsed.groups.minor)
core.setOutput('last-release-version-patch', lastRelease.parsed.groups.patch)
core.setOutput('last-release-version-prerelease', lastRelease.parsed.groups.prerelease)
core.setOutput('last-release-version-buildmetadata', lastRelease.parsed.groups.buildmetadata)
}

nextRelease.parsed = semver.exec(nextRelease.version)

core.setOutput('published', 'true')
core.setOutput('release-type', nextRelease.type)
core.setOutput('release-git-head', nextRelease.gitHead)
core.setOutput('release-version', nextRelease.version)
core.setOutput('release-version-major', nextRelease.parsed.groups.major)
core.setOutput('release-version-minor', nextRelease.parsed.groups.minor)
core.setOutput('release-version-patch', nextRelease.parsed.groups.patch)
core.setOutput('release-version-prerelease', nextRelease.parsed.groups.prerelease)
core.setOutput('release-version-buildmetadata', nextRelease.parsed.groups.buildmetadata)
core.setOutput('release-git-tag', nextRelease.gitTag)
core.setOutput('release-notes', nextRelease.notes)
core.setOutput('release-channel', nextRelease.channel)
})
// catch error
.catch(core.setFailed)
// error handler
function errorHandler ({ message, stack }) {
core.error(message)
core.debug(stack)
process.exit(1)
}

// // catch errors and exit
// process.on('unhandledRejection', errorHandler)
// process.on('uncaughtException', errorHandler)

// parse inputs
const inputs = {
config: core.getInput('config'),
dry: core.getInput('dry') === 'true',
debug: core.getInput('debug') === 'true',
format: core.getInput('format'),
branches: core.getInput('branches')
}


const config = inputs.config ? await import(resolve(inputs.config), { assert: { type: "json"} }) : {}

const options = {
dryRun: inputs.dry,
debug: inputs.debug
}

// only add these options if they have assigned value (otherwise, defaults will be applied)
if (inputs.format) options.tagFormat = inputs.format
if (inputs.branches) options.branches = inputs.branches

const result = await release({ ...config, ...options })

// exit early
if (!result) {
core.setOutput('published', 'false')
process.exit(0)
}

// get result details
const { lastRelease, nextRelease } = result

lastRelease.parsed = semver.exec(lastRelease.version)

// outputs
core.setOutput('last-release-git-head', lastRelease.gitHead)
core.setOutput('last-release-git-tag', lastRelease.gitTag)
core.setOutput('last-release-channel', lastRelease.channel)
core.setOutput('last-release-version', lastRelease.version)

if (lastRelease.parsed) {
core.setOutput('last-release-version-major', lastRelease.parsed.groups.major)
core.setOutput('last-release-version-minor', lastRelease.parsed.groups.minor)
core.setOutput('last-release-version-patch', lastRelease.parsed.groups.patch)
core.setOutput('last-release-version-prerelease', lastRelease.parsed.groups.prerelease)
core.setOutput('last-release-version-buildmetadata', lastRelease.parsed.groups.buildmetadata)
}

nextRelease.parsed = semver.exec(nextRelease.version)

core.setOutput('published', 'true')
core.setOutput('release-type', nextRelease.type)
core.setOutput('release-git-head', nextRelease.gitHead)
core.setOutput('release-version', nextRelease.version)
core.setOutput('release-version-major', nextRelease.parsed.groups.major)
core.setOutput('release-version-minor', nextRelease.parsed.groups.minor)
core.setOutput('release-version-patch', nextRelease.parsed.groups.patch)
core.setOutput('release-version-prerelease', nextRelease.parsed.groups.prerelease)
core.setOutput('release-version-buildmetadata', nextRelease.parsed.groups.buildmetadata)
core.setOutput('release-git-tag', nextRelease.gitTag)
core.setOutput('release-notes', nextRelease.notes)
core.setOutput('release-channel', nextRelease.channel)

0 comments on commit 1d57161

Please sign in to comment.