Skip to content

Commit

Permalink
Use the action to build the full version and tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-v committed Sep 22, 2023
1 parent 85c9c4f commit 0b33060
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 29 deletions.
79 changes: 79 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as main from '../src/main'

// Mock the GitHub Actions core library
const getInputMock = jest.spyOn(core, 'getInput')
const etOutputMock = jest.spyOn(core, 'setOutput')
const setFailedMock = jest.spyOn(core, 'setFailed')

// Mock the action's main function
Expand All @@ -25,6 +26,8 @@ describe('action', () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'MAJOR'
case 'INPUT_VERSION_JSON':
return '{"major":31,"minor":1,"patch":0,"build":457,"revision":0,"versionSuffix":"alpha"}'
default:
Expand All @@ -36,10 +39,86 @@ describe('action', () => {
expect(runMock).toHaveReturned()
})

it('valid version json; update major', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'MAJOR'
case 'INPUT_VERSION_JSON':
return '{"major":31,"minor":1,"patch":0,"build":457,"revision":0,"versionSuffix":"alpha"}'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
})

it('valid version json; update minor', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'MINOR'
case 'INPUT_VERSION_JSON':
return '{"major":31,"minor":1,"patch":0,"build":457,"revision":0,"versionSuffix":"alpha"}'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
})

it('valid version json; update patch', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'PATCH'
case 'INPUT_VERSION_JSON':
return '{"major":31,"minor":1,"patch":0,"build":457,"revision":0,"versionSuffix":"alpha"}'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
})

it('invalid update type', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'invalid'
case 'INPUT_VERSION_JSON':
return '{"major":31,"minor":1,"patch":0,"build":457,"revision":0,"versionSuffix":"alpha"}'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Invalid update type specified. Valid options: MAJOR, MINOR or PATCH.'
)
})

it('invalid version json', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'INPUT_UPDATE_TYPE':
return 'MAJOR'
case 'INPUT_VERSION_JSON':
return 'this is not valid version json'
default:
Expand Down
59 changes: 58 additions & 1 deletion dist/index.js

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

82 changes: 54 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import { debug } from 'console'

interface Version {
major: number
Expand All @@ -10,9 +11,9 @@ interface Version {
}

enum UpdateType {
MAJOR = 1,
MINOR = 2,
PATCH = 3
MAJOR = 'MAJOR',
MINOR = 'MINOR',
PATCH = 'PATCH'
}

/**
Expand All @@ -21,44 +22,70 @@ enum UpdateType {
*/
export async function run(): Promise<void> {
try {
const updateType: keyof typeof UpdateType = core.getInput('INPUT_UPDATE_TYPE');
// TypeScript doest not have anyting like ENUM.TryParse and does not throw an
// error when trying to cast a string to the enum.
// Per https://thoughtbot.com/blog/the-trouble-with-typescript-enums, what's implemented below
// seems to be the best workaround
const updateType: UpdateType | undefined = Object.values(UpdateType).find(
x => x === core.getInput('INPUT_UPDATE_TYPE')
)

if (updateType === undefined) {
throw new Error('Update type is undefined')
}

processVersionJson(updateType)
} catch (error) {
console.log(error)

// Fail the workflow run if an error occurs
if (error instanceof Error) {
core.setFailed(
'Invalid update type specified. Valid options: MAJOR, MINOR or PATCH.'
)
}
}
}

async function processVersionJson(updateType: UpdateType) {
try {
const version: Version = JSON.parse(core.getInput('INPUT_VERSION_JSON'))

// NOTE: for Trading Toolbox, patch and reversion are the same.
switch (updateType) {
case: UpdateType.MAJOR: {
// Increment major version component is unchanged.
case UpdateType.MAJOR: {
// Increment major version component is unchanged.
// Reset minor, patch/revision to 0.
version.major++;

version.major++
version.minor = 0
version.patch = version.revision = 0;
break;
version.patch = version.revision = 0

break
}
case: UpdateType.MINOR: {
// Major version component is unchanged.
case UpdateType.MINOR: {
// Major version component is unchanged.
// Increment minor version component.
// Reset patch/revision to 0.

version.minor++;
version.patch = version.revision = 0;

break;
version.minor++
version.patch = version.revision = 0

break
}
case: UpdateType.PATCH: {
// Major version component is unchanged.
// Minor version component is unchanged.
case UpdateType.PATCH: {
// Major version component is unchanged.
// Minor version component is unchanged.
// Incremment patch/revision.
version.patch = version.revision++;
break;

version.patch = version.revision++
break
}
}

// Build version component always increments.
version.build++;
version.build++

// Set outputs for other workflow steps to use
core.setOutput('major', version.major)
core.setOutput('minor', version.minor)
Expand All @@ -67,11 +94,10 @@ export async function run(): Promise<void> {
core.setOutput('revision', version.revision)
core.setOutput('suffix', version.suffix)

const versionString: String = `${version.major}.${version.minor}.${version.build}.${version.revision}`;
const versionString: String = `${version.major}.${version.minor}.${version.build}.${version.revision}`

core.setOutput('version', versionString)
core.setOutput('tag', `v${versionString}`)

} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) {
Expand Down

0 comments on commit 0b33060

Please sign in to comment.