detect branch and parse semver

This commit is contained in:
David Hiendl 2023-12-02 16:46:53 +01:00
parent 5add488dc0
commit 29c27575d5
3 changed files with 54 additions and 11 deletions

View File

@ -5,25 +5,63 @@ import * as semverParser from 'semver-parser';
try {
// `who-to-greet` input defined in action metadata file
const stripTaxPrefix = core.getBooleanInput('strip_tag_prefix');
const stripTagPrefix = core.getInput('strip_tag_prefix');
console.log(JSON.stringify(github.context))
console.log("github.context.ref="+github.context.ref)
console.log("github.context.ref=" + github.context.ref)
const isTag = github.context.ref.startsWith("refs/tags/")
core.setOutput("git_is_tag", isTag ? "true" : "false")
const defaultBranch = github.context.payload?.repository?.defaultBranch
core.setOutput("git_default_branch", defaultBranch)
if(!isTag) {
const baseRef = github.context.payload?.pull_request?.base?.ref || process.env.BODY_REF
const isPullRequest = github.context.payload.pull_request != null
core.setOutput("git_is_pull_request", isPullRequest)
if(isPullRequest) {
const baseRef = github.context.baseRef || process.env.BODY_REF
const headRef = github.context.payload?.pull_request?.head?.ref || process.env.HEAD_REF
console.log("baseRef="+baseRef)
console.log("headRef="+headRef)
console.log("baseRef=" + baseRef)
console.log("headRef=" + headRef)
} else if(!isTag) {
if(!github.context.ref.startsWith("refs/head/")) {
throw new Error("Failed to determine branch for non-PR and non-Tag action")
}
const currentBranch = github.context.ref.slice("refs/head/".length)
console.log("git_current_branch=" + currentBranch)
core.setOutput("git_current_branch", currentBranch)
const isDefaultBranch = currentBranch == defaultBranch
console.log("git_current_branch=" + isDefaultBranch ? "true" : "false")
core.setOutput("git_is_default_branch", isDefaultBranch ? "true" : "false")
}
if(isTag) {
const tag = github.context.ref.slice("refs/tags/".length)
console.log("git_tag=" + tag)
core.setOutput("git_tag", tag)
// const parsed = semverParser.parseSemVer("1.1.1+abc")
const parsed = semverParser.parseSemVer(tag)
console.log("semver=" + JSON.stringify(parsed))
core.setOutput("semver_valid", parsed.matches ? "true" : "false")
core.setOutput("semver_major", parsed.major)
core.setOutput("semver_minor", parsed.minor)
core.setOutput("semver_patch", parsed.patch)
core.setOutput("semver_build", parsed.build)
core.setOutput("semver_pre", parsed.pre)
} else {
core.setOutput("git_tag", "false")
core.setOutput("semver_valid", "false")
core.setOutput("semver_major", "false")
core.setOutput("semver_minor", "false")
core.setOutput("semver_patch", "false")
core.setOutput("semver_build", "false")
core.setOutput("semver_pre", "false")
}
} catch (error) {
core.setFailed(error.message);

View File

@ -1,10 +1,10 @@
name: Branch Names
description: Retrieve github branch or tag information without the /ref/* prefix
author: tj-actions
author: David Hiendl
inputs:
strip_tag_prefix:
description: 'The prefix that should be stripped from the tag e.g `v` -> with a tag `v0.0.1` -> returns `0.0.1`'
default: "false"
default: ""
required: false
outputs:
@ -36,6 +36,10 @@ outputs:
description: "Semver minor version"
semver_patch:
description: "Semver patch version"
semver_build:
description: "Semver build string"
semver_pre:
description: "Semver pre-release version"
runs:
using: "node20"

1
node_modules/.bin/uuid generated vendored
View File

@ -1 +0,0 @@
../uuid/dist/bin/uuid

2
node_modules/.bin/uuid generated vendored Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../uuid-bin');