Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion dotnet/versions/action.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
name: 'Version Branch Generator'
description: 'Generates the version based on the branch name'
inputs:
type:
description: Can be 'lib' or 'deploy'
required: true
package-version: # id of input
description: 'Package version'
required: true
tag-version-index:
description: Index of the version number within the parts of a tag.
required: false
default: 0
outputs:
app-version: # id of output
app-version:
description: 'The app version generated'
runs:
using: 'node12'
Expand Down
139 changes: 107 additions & 32 deletions dotnet/versions/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,108 @@
const core = require('@actions/core');
const github = require('@actions/github');

try {
const branch = github.context.ref.replace('refs/heads/', '');
const version = core.getInput('package-version');
const runNumber = github.context.runNumber;
console.log(`Branch ${branch}`);
console.log(`Version: ${version}`)

var appVersion;

switch(branch) {
case 'main':
case 'master':
appVersion = version;
break;
case 'develop':
appVersion = `${version}-dev-${runNumber}`;
break;
case branch && branch.startsWith('hotfix'):
appVersion = `${version}-hotfix-${runNumber}`;
break;
default:
appVersion = `${version}-${branch.replace('/', '-')}-${runNumber}`;
}

core.notice(`App version: ${appVersion}`);

core.setOutput("app-version", appVersion);
} catch (error) {
core.setFailed(error.message);
const core = require('@actions/core');
const github = require('@actions/github');

try {
var branch;
var isTag = false;

switch (github.context.eventName) {
case "pull_request":
console.log("Determining version number from 'pull_request' event");
// branch = process.env.GITHUB_HEAD_REF;
branch = github.context.payload.pull_request.head.ref;
break;

case "push":
default:
console.log(`Determining version number from '${github.context.eventName}' event`);
branch = github.context.ref;

if (branch.startsWith('refs/tags/')) {
isTag = true;
branch = branch.replace('refs/tags/', '');
}
else if (branch.startsWith('refs/heads/')) {
branch = github.context.ref.replace('refs/heads/', '');
}
}

if (!isTag) {
version = core.getInput('package-version');
const runNumber = github.context.runNumber;
const type = core.getInput('type');
console.log(`Branch ${branch}`);
console.log(`Version: ${version}`)
let appVersion;

switch(type) {
case 'lib':
appVersion = generateLibraryVersionString(branch, version, runNumber);
break;
case 'deploy':
appVersion = generateDeployableVersionString(branch, version, runNumber);
break;
default:
throw `'${type}' is not a valid type for this action`;
}

core.notice(`App version: ${appVersion}`);
core.setOutput("app-version", appVersion);
}
else {

versionIndex = parseInt(core.getInput('tag-version-index'));

appVersion = branch.replace('refs/tags/', '').split('/')[versionIndex];

core.notice(`App version: ${appVersion}`);
core.setOutput("app-version", appVersion);
}
} catch (error) {
core.setFailed(error.message);
}

function generateLibraryVersionString(branch, version, runNumber) {
switch(branch) {
case 'main':
case 'master':
return version;
case 'develop':
return generateFinalVersionName(version, 'dev', runNumber);
case branch && branch.startsWith('hotfix'):
return generateFinalVersionName(version, 'hotfix', runNumber);
default:
return generateFinalVersionName(version, normalizeBranchName(branch, false), runNumber);
}
}

function generateDeployableVersionString(branch, version, runNumber) {
if (branch === 'main' || branch === 'master') {
return version;
}

if (branch.startsWith('feature')) {
return generateFinalVersionName(version, "demo-" + normalizeBranchName(branch, true), runNumber);
}

if (branch.startsWith('hotfix')) {
return generateFinalVersionName(version, normalizeBranchName(branch, false), runNumber);
}

if (branch === 'develop') {
return generateFinalVersionName(version, 'dev', runNumber);
}

return generateFinalVersionName(version, normalizeBranchName(branch, false), runNumber);

}

function generateFinalVersionName(version, descriptor, runNumber) {
return `${version}-${descriptor}-${runNumber}`;
}

function normalizeBranchName(branchName, trimPrefix) {
if (trimPrefix) {
branchName = branchName.substring(branchName.indexOf('/') + 1);
}
return branchName.replace('/', '-').toLowerCase()
}
66 changes: 41 additions & 25 deletions dotnet/versions/node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions dotnet/versions/node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading