Skip to content

Commit

Permalink
Improve code based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
szymmis committed Jul 27, 2023
1 parent 153d98e commit fb18745
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
42 changes: 20 additions & 22 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

38 changes: 18 additions & 20 deletions lib/versions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from "fs";
import fs from "fs/promises";
import * as core from "@actions/core";
import { HttpClient } from "@actions/http-client";

export async function determineVersion(repo, versionInput) {
versionInput = versionInput?.trim();

if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
if (!versionInput) {
let toolVersion = await getVersionFromToolVersionsFile();
versionInput = toolVersion ?? "latest";
}

if (!versionInput) {
let toolVersion = getVersionFromToolVersionsFile();
versionInput = toolVersion ?? (await fetchLatestTag(repo));
if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
}

return versionInput.startsWith("v")
Expand All @@ -37,38 +37,36 @@ function fetchLatestTag(repo) {

if (res.message.statusCode < 300 || res.message.statusCode >= 400) {
throw new Error(
`failed to determine latest version: expected releases request to redirect, instead got http status: ${res.message.statusCode}`,
`failed to determine latest version: expected releases request to redirect, instead got http status: ${res.message.statusCode}`
);
}

const location = res.message.headers.location;
core.debug(`Location: ${location}`);
if (!location) {
throw new Error(
`failed to determine latest version: releases request response misses 'location' header`,
`failed to determine latest version: releases request response misses 'location' header`
);
}

const tag = location.replace(/.*\/tag\/(v.*)(?:\/.*)?/, "$1");
if (!tag) {
throw new Error(
`failed to determine latest version: could not extract tag from release url`,
`failed to determine latest version: could not extract tag from release url`
);
}

return tag;
},
}
);
}

function getVersionFromToolVersionsFile() {
const fileName = ".tool-versions";
if (!fs.existsSync(fileName)) return;

const toolVersions = fs.readFileSync(fileName, "utf-8").split("\n");
const scarbVersionInfo = toolVersions.find((tool) => tool.match(/^scarb/));
if (!scarbVersionInfo) return;

const [, version] = scarbVersionInfo.match(/scarb ([\w.-]+)/);
return version;
async function getVersionFromToolVersionsFile() {
try {
return (await fs.readFile(".tool-versions", "utf-8")).match(
/^scarb ([\w.-]+)/m
)?.[1];
} catch (e) {
return;
}
}

0 comments on commit fb18745

Please sign in to comment.