2022-10-13 06:52:57 +07:00
import * as core from '@actions/core'
2020-04-02 00:27:19 +07:00
import os from 'os'
2021-01-30 22:19:26 +07:00
import semver from 'semver'
2020-04-02 00:27:19 +07:00
import Setup from './lib/setup.js'
import Package from './lib/package.js'
import Tag from './lib/tag.js'
import Regex from './lib/regex.js'
2020-04-03 01:58:51 +07:00
import Dockerfile from './lib/docker.js'
2020-04-02 00:27:19 +07:00
async function run ( ) {
try {
Setup . debug ( )
Setup . requireAnyEnv ( 'GITHUB_TOKEN' , 'INPUT_GITHUB_TOKEN' )
2020-04-02 01:41:56 +07:00
// Configure the default output
core . setOutput ( 'tagcreated' , 'no' )
2020-04-02 00:27:19 +07:00
// Identify the tag parsing strategy
2022-10-14 23:28:34 +07:00
// Use manual strategy if a version is supplied in the action config
const versionSupplied = core . getInput ( 'root' , { required : false } ) !== null && core . getInput ( 'root' , { required : false } ) !== undefined && core . getInput ( 'root' , { required : false } ) . trim ( ) . length > 0
2022-10-14 23:22:56 +07:00
const strategy = versionSupplied ? 'manual' : ( core . getInput ( 'regex_pattern' , { required : false } ) || '' ) . trim ( ) . length > 0 ? 'regex' : ( ( core . getInput ( 'strategy' , { required : false } ) || 'package' ) . trim ( ) . toLowerCase ( ) )
2022-10-14 23:28:34 +07:00
// Identify the root directory to use for auto-identifying a tag version
2022-10-13 06:42:16 +07:00
const root = core . getInput ( 'root' , { required : false } ) || core . getInput ( 'package_root' , { required : false } ) || ( strategy === 'composer' ? './composer.json' : './' )
2021-01-30 22:19:26 +07:00
2021-06-02 17:53:21 +07:00
// If this value is true, the tag will not be pushed
2022-10-13 06:34:58 +07:00
const isDryRun = ( core . getInput ( 'dry_run' , { required : false } ) || '' ) . trim ( ) . toLowerCase ( ) === 'true'
2020-04-02 00:27:19 +07:00
// Extract the version number using the supplied strategy
let version = core . getInput ( 'root' , { required : false } )
2022-10-14 23:22:56 +07:00
version = version === null || version . trim ( ) . length === v0 ? null : version
2022-10-14 23:28:34 +07:00
// If Regex strategy is specified, retrieve the Regex pattern
2020-09-11 09:29:49 +07:00
const pattern = core . getInput ( 'regex_pattern' , { required : false } )
2020-04-02 00:27:19 +07:00
2022-10-14 23:28:34 +07:00
// Identify the version by strategy
2022-10-14 23:22:56 +07:00
switch ( strategy ) {
case 'docker' :
version = ( new Dockerfile ( root ) ) . version
break
case 'composer' :
case 'package' :
// Extract using the package strategy (this is the default strategy)
version = ( new Package ( root ) ) . version
break
case 'regex' :
version = ( new Regex ( root , new RegExp ( pattern , 'gim' ) ) ) . version
break
case 'manual' :
2022-10-14 23:28:34 +07:00
core . notice ( ` " ${ version } " version was manually specified in the action configuration ` )
2022-10-14 23:22:56 +07:00
break
default :
2022-10-14 23:28:34 +07:00
core . setFailed ( ` " ${ strategy } " is not a recognized tagging strategy. Choose from: 'package' (package.json), 'composer' (composer.json), 'docker' (uses Dockerfile), or 'regex' (JS-based RegExp). Specify a version to use the "manual" strategy. ` )
2022-10-14 23:22:56 +07:00
return
2020-04-02 00:27:19 +07:00
}
2020-09-11 09:22:51 +07:00
const msg = ` using the ${ strategy } extraction ${ strategy === 'regex' ? ' with the /' + pattern + '/gim pattern.' : '' } . `
if ( ! version ) {
throw new Error ( ` No version identified ${ msg } ` )
}
2021-01-30 22:19:26 +07:00
2021-01-31 03:34:05 +07:00
// Ensure that version and minVersion are valid SemVer strings
2022-10-14 23:22:56 +07:00
const minVersion = core . getInput ( 'min_version' , { required : false } )
const versionSemVer = semver . coerce ( version )
2021-01-31 03:34:05 +07:00
const minVersionSemVer = semver . coerce ( minVersion )
2022-10-14 23:28:34 +07:00
2021-01-31 03:34:05 +07:00
if ( ! minVersionSemVer ) {
2022-10-13 06:52:57 +07:00
core . info ( ` Skipping min version check. ${ minVersion } is not valid SemVer ` )
2021-01-31 03:34:05 +07:00
}
2022-10-14 23:28:34 +07:00
2021-01-31 03:34:05 +07:00
if ( ! versionSemVer ) {
2022-10-13 06:52:57 +07:00
core . info ( ` Skipping min version check. ${ version } is not valid SemVer ` )
2021-01-31 03:34:05 +07:00
}
2022-10-14 23:28:34 +07:00
2021-02-01 17:12:31 +07:00
if ( minVersionSemVer && versionSemVer && semver . lt ( versionSemVer , minVersionSemVer ) ) {
2022-10-13 06:52:57 +07:00
core . info ( ` Version " ${ version } " is lower than minimum " ${ minVersion } " ` )
2021-01-30 22:09:56 +07:00
return
}
2020-09-11 09:22:51 +07:00
2022-10-13 06:52:57 +07:00
core . notice ( ` Recognized " ${ version } " ${ msg } ` )
2020-04-02 00:27:19 +07:00
core . setOutput ( 'version' , version )
core . debug ( ` Detected version ${ version } ` )
// Configure a tag using the identified version
const tag = new Tag (
core . getInput ( 'tag_prefix' , { required : false } ) ,
version ,
core . getInput ( 'tag_suffix' , { required : false } )
)
2022-10-13 05:48:45 +07:00
if ( isDryRun ) {
2022-10-13 06:52:57 +07:00
core . notice ( ` " ${ tag . name } " tag was not pushed because the dry_run option was set. ` )
2021-06-02 17:53:21 +07:00
} else {
2022-10-13 06:52:57 +07:00
core . info ( ` Attempting to create ${ tag . name } tag. ` )
2021-06-02 17:53:21 +07:00
}
2020-04-02 10:19:36 +07:00
core . setOutput ( 'tagrequested' , tag . name )
2020-04-05 06:28:01 +07:00
core . setOutput ( 'prerelease' , tag . prerelease ? 'yes' : 'no' )
core . setOutput ( 'build' , tag . build ? 'yes' : 'no' )
2020-04-02 02:28:38 +07:00
2020-04-02 00:27:19 +07:00
// Check for existance of tag and abort (short circuit) if it already exists.
if ( await tag . exists ( ) ) {
2021-06-02 17:53:21 +07:00
core . setFailed ( ` " ${ tag . name } " tag already exists. ` + os . EOL )
2020-04-02 00:27:19 +07:00
core . setOutput ( 'tagname' , '' )
return
}
// The tag setter will autocorrect the message if necessary.
tag . message = core . getInput ( 'tag_message' , { required : false } ) . trim ( )
2020-04-02 01:41:56 +07:00
2022-10-13 05:48:45 +07:00
if ( ! isDryRun ) {
2021-06-02 17:53:21 +07:00
await tag . push ( )
2022-10-13 05:34:21 +07:00
core . setOutput ( 'tagcreated' , 'yes' )
2021-06-02 17:53:21 +07:00
}
2022-10-13 05:34:21 +07:00
2020-04-02 02:24:38 +07:00
core . setOutput ( 'tagname' , tag . name )
core . setOutput ( 'tagsha' , tag . sha )
core . setOutput ( 'taguri' , tag . uri )
core . setOutput ( 'tagmessage' , tag . message )
core . setOutput ( 'tagref' , tag . ref )
2020-04-02 00:27:19 +07:00
} catch ( error ) {
2022-10-14 23:22:56 +07:00
core . warning ( error . message + '\n' + error . stack )
2020-04-02 00:27:19 +07:00
core . setOutput ( 'tagname' , '' )
core . setOutput ( 'tagsha' , '' )
core . setOutput ( 'taguri' , '' )
core . setOutput ( 'tagmessage' , '' )
core . setOutput ( 'tagref' , '' )
core . setOutput ( 'tagcreated' , 'no' )
}
}
run ( )