mirror of
https://github.com/opentofu/setup-opentofu.git
synced 2025-12-06 07:50:37 +00:00
feat: added versions resolution logic to use user's input.
Signed-off-by: Dmitry Kisler <admin@dkisler.com>
This commit is contained in:
parent
137fcca5fe
commit
c68fac3149
6 changed files with 3715 additions and 272 deletions
|
|
@ -45,18 +45,55 @@ async function fetchReleases () {
|
|||
return releasesMeta.map(releaseMeta => new Release(releaseMeta));
|
||||
}
|
||||
|
||||
const semver = require('semver');
|
||||
|
||||
async function findLatestVersion (versions) {
|
||||
return versions.sort((a, b) => semver.rcompare(a, b))[0];
|
||||
}
|
||||
|
||||
async function findLatestVersionInRange (versions, range) {
|
||||
return semver.maxSatisfying(versions, range, { prerelease: true, loose: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the release given the version.
|
||||
*
|
||||
* @param {string} version: Release version.
|
||||
* @param {function} fetchReleasesFn: Optional function to fetch releases.
|
||||
*/
|
||||
async function getRelease (version) {
|
||||
const releases = await fetchReleases();
|
||||
return releases.find(release => release.version === version);
|
||||
async function getRelease (version, fetchReleasesFn = fetchReleases) {
|
||||
const latestVersionLabel = 'latest';
|
||||
|
||||
const versionsRange = semver.validRange(version, { prerelease: true, loose: true });
|
||||
if (versionsRange === null && version !== latestVersionLabel) {
|
||||
throw new Error('Input version cannot be used, see semver: https://semver.org/spec/v2.0.0.html');
|
||||
}
|
||||
|
||||
const releases = await fetchReleasesFn();
|
||||
|
||||
if (releases === null || releases.length === 0) {
|
||||
throw new Error('No tofu releases found, please contact OpenTofu');
|
||||
}
|
||||
|
||||
const versionsFound = releases.map(release => release.version);
|
||||
let versionSelected;
|
||||
if (version === latestVersionLabel) {
|
||||
versionSelected = findLatestVersion(versionsFound);
|
||||
} else {
|
||||
versionSelected = await findLatestVersionInRange(versionsFound, versionsRange);
|
||||
}
|
||||
|
||||
if (versionSelected === null) {
|
||||
throw new Error('No matching version found');
|
||||
}
|
||||
|
||||
return releases.find(release => release.version === versionSelected);
|
||||
}
|
||||
|
||||
// Note that the export is defined as adaptor to replace hashicorp/js-releases
|
||||
// See: https://github.com/hashicorp/setup-terraform/blob/e192cfcbae6c6ed207c277ed7624131996c9bf13/lib/setup-terraform.js#L15
|
||||
module.exports = {
|
||||
getRelease
|
||||
getRelease,
|
||||
Release,
|
||||
Build
|
||||
};
|
||||
|
|
|
|||
|
|
@ -124,9 +124,7 @@ credentials "${credentialsHostname}" {
|
|||
async function run () {
|
||||
try {
|
||||
// Gather GitHub Actions inputs
|
||||
const version = '1.6.0-alpha2';
|
||||
// TODO: allow dynamic version selection once logic is ready
|
||||
// const version = core.getInput('tofu_version');
|
||||
const version = core.getInput('tofu_version');
|
||||
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
|
||||
const credentialsToken = core.getInput('cli_config_credentials_token');
|
||||
const wrapper = core.getInput('tofu_wrapper') === 'true';
|
||||
|
|
|
|||
195
lib/test/releases.test.js
Normal file
195
lib/test/releases.test.js
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
const pkg = require('../releases');
|
||||
|
||||
function mockFetchReleases () {
|
||||
const mockReleasesMeta = [{
|
||||
tag_name: 'v1.6.0-alpha2',
|
||||
assets: [{
|
||||
name: 'tofu_1.6.0-alpha2_386.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_386.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_386.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.rpm'
|
||||
}, {
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_darwin_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_darwin_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_darwin_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_openbsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_openbsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_openbsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_openbsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS.pem',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS.pem'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS.sig',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS.sig'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_solaris_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_solaris_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_windows_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_windows_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_windows_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_windows_amd64.zip'
|
||||
}]
|
||||
}, {
|
||||
tag_name: 'v1.6.0-alpha1',
|
||||
assets: [{
|
||||
name: 'tofu_1.6.0-alpha1_386.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_386.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_386.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_darwin_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_darwin_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_darwin_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_darwin_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_openbsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_openbsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_openbsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_openbsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS.pem',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS.pem'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS.sig',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS.sig'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_solaris_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_solaris_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_windows_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_windows_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_windows_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_windows_amd64.zip'
|
||||
}]
|
||||
}];
|
||||
|
||||
return mockReleasesMeta.map(el => new pkg.Release(el));
|
||||
}
|
||||
|
||||
describe('getRelease', () => {
|
||||
it('shall return the latest release', async () => {
|
||||
const version = '<1.6.0-alpha3';
|
||||
const want = mockFetchReleases().find(el => el.version === '1.6.0-alpha2');
|
||||
const gotRelease = await pkg.getRelease(version, mockFetchReleases);
|
||||
expect(gotRelease).toEqual(want);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue