Apply PR suggestions

This commit is contained in:
panticmilos 2022-11-23 18:39:47 +01:00
parent 9d73e829e4
commit a29996aa9d
6 changed files with 148 additions and 174 deletions

View file

@ -6,6 +6,7 @@ import * as httpm from '@actions/http-client';
import * as sys from './system';
import fs from 'fs';
import os, {arch} from 'os';
import {StableReleaseAlias} from './utils';
type InstallationType = 'dist' | 'manifest';
@ -34,7 +35,7 @@ export async function getGo(
checkLatest: boolean,
auth: string | undefined,
arch = os.arch(),
releases: tc.IToolRelease[] | undefined
manifest: tc.IToolRelease[] | undefined
) {
let osPlat: string = os.platform();
@ -45,7 +46,7 @@ export async function getGo(
true,
auth,
arch,
releases
manifest
);
if (resolvedVersion) {
versionSpec = resolvedVersion;
@ -55,6 +56,18 @@ export async function getGo(
}
}
if (
versionSpec === StableReleaseAlias.Stable ||
versionSpec === StableReleaseAlias.OldStable
) {
versionSpec = await resolveStableVersionInput(
versionSpec,
auth,
arch,
manifest
);
}
// check cache
let toolPath: string;
toolPath = tc.find('go', versionSpec, arch);
@ -121,7 +134,7 @@ export async function resolveVersionFromManifest(
stable: boolean,
auth: string | undefined,
arch: string,
releases: tc.IToolRelease[] | undefined
manifest: tc.IToolRelease[] | undefined
): Promise<string | undefined> {
try {
const info = await getInfoFromManifest(
@ -129,7 +142,7 @@ export async function resolveVersionFromManifest(
stable,
auth,
arch,
releases
manifest
);
return info?.resolvedVersion;
} catch (err) {
@ -183,27 +196,26 @@ export async function extractGoArchive(archivePath: string): Promise<string> {
return extPath;
}
export async function getAllManifestReleases(auth: string | undefined) {
export async function getManifest(auth: string | undefined) {
return tc.getManifestFromRepo('actions', 'go-versions', auth, 'main');
}
export async function getAllToolCacheReleases(arch = os.arch()) {
return tc.findAllVersions('go', arch);
}
export async function getInfoFromManifest(
versionSpec: string,
stable: boolean,
auth: string | undefined,
arch = os.arch(),
releases?: tc.IToolRelease[] | undefined
manifest?: tc.IToolRelease[] | undefined
): Promise<IGoVersionInfo | null> {
let info: IGoVersionInfo | null = null;
releases = releases ? releases : await getAllManifestReleases(auth);
if (!manifest) {
core.debug('No manifest cached');
manifest = await getManifest(auth);
}
core.info(`matching ${versionSpec}...`);
let rel = await tc.findFromManifest(versionSpec, stable, releases, arch);
const rel = await tc.findFromManifest(versionSpec, stable, manifest, arch);
if (rel && rel.files.length > 0) {
info = <IGoVersionInfo>{};
@ -341,3 +353,46 @@ export function parseGoVersionFile(versionFilePath: string): string {
return contents.trim();
}
async function resolveStableVersionInput(
versionSpec: string,
auth: string | undefined,
arch = os.arch(),
manifest: tc.IToolRelease[] | undefined
): Promise<string> {
if (!manifest) {
core.debug('No manifest cached');
manifest = await getManifest(auth);
}
const releases = manifest.map(release => release.version);
if (versionSpec === StableReleaseAlias.Stable) {
core.info(`stable version resolved as ${releases[0]}`);
return releases[0];
} else {
const versions = releases.map(
release => `${semver.major(release)}.${semver.minor(release)}`
);
const uniqueVersions = Array.from(new Set(versions));
const oldstableVersion = await getInfoFromManifest(
uniqueVersions[1],
true,
auth,
arch,
manifest
);
core.info(
`oldstable version resolved as ${oldstableVersion?.resolvedVersion}`
);
if (!oldstableVersion) {
return versionSpec;
}
return oldstableVersion.resolvedVersion;
}
}