From 29c27575d585a3c8820e1c506ae7f9e0d288eff7 Mon Sep 17 00:00:00 2001 From: David Hiendl Date: Sat, 2 Dec 2023 16:46:53 +0100 Subject: [PATCH] detect branch and parse semver --- action.js | 54 +++++++++++++++++++++++++++++++++++------- action.yml | 8 +++++-- node_modules/.bin/uuid | 3 ++- 3 files changed, 54 insertions(+), 11 deletions(-) mode change 120000 => 100755 node_modules/.bin/uuid diff --git a/action.js b/action.js index 4a480e9..a7c929e 100644 --- a/action.js +++ b/action.js @@ -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); diff --git a/action.yml b/action.yml index c514977..a36b7b7 100644 --- a/action.yml +++ b/action.yml @@ -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" diff --git a/node_modules/.bin/uuid b/node_modules/.bin/uuid deleted file mode 120000 index 588f70e..0000000 --- a/node_modules/.bin/uuid +++ /dev/null @@ -1 +0,0 @@ -../uuid/dist/bin/uuid \ No newline at end of file diff --git a/node_modules/.bin/uuid b/node_modules/.bin/uuid new file mode 100755 index 0000000..f38d2ee --- /dev/null +++ b/node_modules/.bin/uuid @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('../uuid-bin');