Skip to content

Commit

Permalink
feat(version): validated supported cli major version
Browse files Browse the repository at this point in the history
  • Loading branch information
Malinskiy committed Feb 6, 2024
1 parent 65f2733 commit 9eb3284
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
30 changes: 20 additions & 10 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ import { sha256File } from './hash';
const TOOL_NAME = 'marathon-cloud';

export type Platform =
| 'darwin_all'
| 'linux_amd64'
| 'linux_arm64'
| 'windows_amd64';
| 'universal-apple-darwin'
| 'x86_64-unknown-linux-gnu'
| 'x86_64-unknown-linux-musl'
| 'aarch64-unknown-linux-gnu'
| 'aarch64-unknown-linux-musl'
| 'arm-unknown-linux-gnueabihf'
| 'arm-unknown-linux-musleabihf'
| 'i686-unknown-linux-gnu'
| 'i686-unknown-linux-musl'
| 'x86_64-pc-windows-gnu'
| 'x86_64-pc-windows-msvc'
| 'i686-pc-windows-msvc';

function resolvePlatform(): Platform {
const platform = process.platform;
const arch = process.arch;
if (platform === 'darwin') {
return 'darwin_all';
return 'universal-apple-darwin';
} else if (platform === 'linux' && arch === 'x64') {
return 'linux_amd64';
return 'x86_64-unknown-linux-gnu';
} else if (platform === 'linux' && arch === 'arm64') {
return 'linux_arm64';
return 'aarch64-unknown-linux-gnu';
} else if (platform === 'win32' && arch === 'x64') {
return 'windows_amd64';
return 'x86_64-pc-windows-msvc';
} else {
throw new Error(`Unsupported platform: ${platform}_${arch}`);
}
Expand Down Expand Up @@ -52,7 +60,7 @@ export async function getReleaseArtifact(
platform = platform || resolvePlatform();
const extension = resolveExtension(platform);

const artifactName = `${TOOL_NAME}_${platform}-${version}.${extension}`;
const artifactName = `${TOOL_NAME}_v${version}-${platform}.${extension}`;

return {
version,
Expand Down Expand Up @@ -163,7 +171,9 @@ export async function setupArtifact(

function resolveExtension(platform: Platform): string {
switch (platform) {
case 'windows_amd64':
case 'x86_64-pc-windows-gnu':
case 'x86_64-pc-windows-msvc':
case 'i686-pc-windows-msvc':
return 'zip';
default:
return 'tar.gz';
Expand Down
12 changes: 8 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core';
import { getReleaseArtifact, setupArtifact } from './release';
import { validateMajorVersion } from './semver';

const inputNameVersion = 'version';
const inputNameSkipCache = 'skip-cache';
Expand All @@ -8,11 +9,14 @@ async function main() {
const version = core.getInput(inputNameVersion);
const skipCache = core.getInput(inputNameSkipCache) === 'true';
core.debug(`version: ${version} skip-cache: ${skipCache}`);
if (validateMajorVersion(version, '1')) {
const artifact = await getReleaseArtifact(version);
core.debug(`Resolved artifact: ${JSON.stringify(artifact)}`);

const artifact = await getReleaseArtifact(version);
core.debug(`Resolved artifact: ${JSON.stringify(artifact)}`);

await setupArtifact(artifact, skipCache);
await setupArtifact(artifact, skipCache);
} else {
core.setFailed(`Unsupported cli version ${version}. This action supports version ${1}`);
}
}

try {
Expand Down
11 changes: 11 additions & 0 deletions src/semver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { validateMajorVersion } from "./semver";

test('versionValidation', () => {
['1', '1.0.0', '1.2.3'].forEach((v) => {
expect(validateMajorVersion(v, '1')).toBe(true);
});

['0', '0.1.0', '0.3.3'].forEach((v) => {
expect(validateMajorVersion(v, '1')).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function validateMajorVersion(versionString: string, supported: string): boolean {
const majorVersion = versionString.split('.')[0];
return majorVersion === supported;
}

0 comments on commit 9eb3284

Please sign in to comment.