Skip to content
This repository was archived by the owner on Apr 1, 2022. It is now read-only.

Use GitHub token to avoid hitting rate limiting in setup-taskfile #7

Merged
merged 5 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions setup-taskfile/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("installer tests", () => {
});

it("Downloads version of Task if no matching version is installed", async () => {
await installer.getTask("2.6.0");
await installer.getTask("2.6.0", "");
const taskDir = path.join(toolDir, "task", "2.6.0", os.arch());

expect(fs.existsSync(`${taskDir}.complete`)).toBe(true);
Expand All @@ -56,7 +56,7 @@ describe("installer tests", () => {
});

it("Gets the latest version of Task 2.5 using 2.5 and no matching version is installed", async () => {
await installer.getTask("2.5");
await installer.getTask("2.5", "");
const taskDir = path.join(toolDir, "task", "2.5.2", os.arch());

expect(fs.existsSync(`${taskDir}.complete`)).toBe(true);
Expand All @@ -68,7 +68,7 @@ describe("installer tests", () => {
});

it("Gets latest version of Task using 2.x and no matching version is installed", async () => {
await installer.getTask("2.x");
await installer.getTask("2.x", "");
const taskdir = path.join(toolDir, "task", "2.6.0", os.arch());

expect(fs.existsSync(`${taskdir}.complete`)).toBe(true);
Expand All @@ -80,7 +80,7 @@ describe("installer tests", () => {
});

it("Gets preview version of Task using 3.x and no matching version is installed", async () => {
await installer.getTask("3.x");
await installer.getTask("3.x", "");
const taskdir = path.join(toolDir, "task", "3.0.0-preview1", os.arch());

expect(fs.existsSync(`${taskdir}.complete`)).toBe(true);
Expand Down
100 changes: 54 additions & 46 deletions setup-taskfile/lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,49 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
// Load tempDirectory before it gets wiped by tool-cache
let tempDirectory = process.env['RUNNER_TEMP'] || '';
let tempDirectory = process.env["RUNNER_TEMP"] || "";
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const util = __importStar(require("util"));
const restm = __importStar(require("typed-rest-client/RestClient"));
const semver = __importStar(require("semver"));
if (!tempDirectory) {
let baseLocation;
if (process.platform === 'win32') {
if (process.platform === "win32") {
// On windows use the USERPROFILE env variable
baseLocation = process.env['USERPROFILE'] || 'C:\\';
baseLocation = process.env["USERPROFILE"] || "C:\\";
}
else {
if (process.platform === 'darwin') {
baseLocation = '/Users';
if (process.platform === "darwin") {
baseLocation = "/Users";
}
else {
baseLocation = '/home';
baseLocation = "/home";
}
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
tempDirectory = path.join(baseLocation, "actions", "temp");
}
const core = __importStar(require("@actions/core"));
const tc = __importStar(require("@actions/tool-cache"));
const io = require("@actions/io");
let osPlat = os.platform();
let osArch = os.arch();
function getTask(version) {
function getTask(version, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
// resolve the version number
const targetVersion = yield computeVersion(version);
const targetVersion = yield computeVersion(version, repoToken);
if (targetVersion) {
version = targetVersion;
}
// look if the binary is cached
let toolPath;
toolPath = tc.find('task', version);
toolPath = tc.find("task", version);
// if not: download, extract and cache
if (!toolPath) {
toolPath = yield downloadRelease(version);
core.debug('Task cached under ' + toolPath);
core.debug("Task cached under " + toolPath);
}
toolPath = path.join(toolPath, 'bin');
toolPath = path.join(toolPath, "bin");
core.addPath(toolPath);
});
}
Expand All @@ -67,7 +67,7 @@ function downloadRelease(version) {
return __awaiter(this, void 0, void 0, function* () {
// Download
let fileName = getFileName();
let downloadUrl = util.format('https://github.com/go-task/task/releases/download/%s/%s', version, fileName);
let downloadUrl = util.format("https://github.com/go-task/task/releases/download/%s/%s", version, fileName);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
Expand All @@ -78,51 +78,59 @@ function downloadRelease(version) {
}
// Extract
let extPath = null;
if (osPlat == 'win32') {
if (osPlat == "win32") {
extPath = yield tc.extractZip(downloadPath);
// Create a bin/ folder and move `task` there
yield io.mkdirP(path.join(extPath, 'bin'));
yield io.mv(path.join(extPath, 'task.exe'), path.join(extPath, 'bin'));
yield io.mkdirP(path.join(extPath, "bin"));
yield io.mv(path.join(extPath, "task.exe"), path.join(extPath, "bin"));
}
else {
extPath = yield tc.extractTar(downloadPath);
// Create a bin/ folder and move `task` there
yield io.mkdirP(path.join(extPath, 'bin'));
yield io.mv(path.join(extPath, 'task'), path.join(extPath, 'bin'));
yield io.mkdirP(path.join(extPath, "bin"));
yield io.mv(path.join(extPath, "task"), path.join(extPath, "bin"));
}
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
return yield tc.cacheDir(extPath, 'task', version);
return yield tc.cacheDir(extPath, "task", version);
});
}
function getFileName() {
const platform = osPlat == 'win32' ? 'windows' : osPlat;
const arch = osArch == 'x64' ? 'amd64' : '386';
const ext = osPlat == 'win32' ? 'zip' : 'tar.gz';
const filename = util.format('task_%s_%s.%s', platform, arch, ext);
const platform = osPlat == "win32" ? "windows" : osPlat;
const arch = osArch == "x64" ? "amd64" : "386";
const ext = osPlat == "win32" ? "zip" : "tar.gz";
const filename = util.format("task_%s_%s.%s", platform, arch, ext);
return filename;
}
// Retrieve a list of versions scraping tags from the Github API
function fetchVersions() {
function fetchVersions(repoToken) {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient('setup-taskfile');
let tags = (yield rest.get('https://github.com/api/repos/go-task/task/git/refs/tags')).result || [];
let rest;
if (repoToken != "") {
rest = new restm.RestClient("setup-taskfile", "", [], {
headers: { Authorization: "Bearer " + repoToken }
});
}
else {
rest = new restm.RestClient("setup-taskfile");
}
let tags = (yield rest.get("https://github.com/api/repos/go-task/task/git/refs/tags")).result || [];
return tags
.filter(tag => tag.ref.match(/v\d+\.[\w\.]+/g))
.map(tag => tag.ref.replace('refs/tags/v', ''));
.map(tag => tag.ref.replace("refs/tags/v", ""));
});
}
// Compute an actual version starting from the `version` configuration param.
function computeVersion(version) {
function computeVersion(version, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
// strip leading `v` char (will be re-added later)
if (version.startsWith('v')) {
if (version.startsWith("v")) {
version = version.slice(1, version.length);
}
// strip trailing .x chars
if (version.endsWith('.x')) {
if (version.endsWith(".x")) {
version = version.slice(0, version.length - 2);
}
const allVersions = yield fetchVersions();
const allVersions = yield fetchVersions(repoToken);
const possibleVersions = allVersions.filter(v => v.startsWith(version));
const versionMap = new Map();
possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v));
Expand All @@ -131,46 +139,46 @@ function computeVersion(version) {
.map(v => versionMap.get(v));
core.debug(`evaluating ${versions.length} versions`);
if (versions.length === 0) {
throw new Error('unable to get latest version');
throw new Error("unable to get latest version");
}
core.debug(`matched: ${versions[0]}`);
return 'v' + versions[0];
return "v" + versions[0];
});
}
// Make partial versions semver compliant.
function normalizeVersion(version) {
const preStrings = ['beta', 'rc', 'preview'];
const versionPart = version.split('.');
const preStrings = ["beta", "rc", "preview"];
const versionPart = version.split(".");
if (versionPart[1] == null) {
//append minor and patch version if not available
// e.g. 2 -> 2.0.0
return version.concat('.0.0');
return version.concat(".0.0");
}
else {
// handle beta and rc
// e.g. 1.10beta1 -? 1.10.0-beta1, 1.10rc1 -> 1.10.0-rc1
if (preStrings.some(el => versionPart[1].includes(el))) {
versionPart[1] = versionPart[1]
.replace('beta', '.0-beta')
.replace('rc', '.0-rc')
.replace('preview', '.0-preview');
return versionPart.join('.');
.replace("beta", ".0-beta")
.replace("rc", ".0-rc")
.replace("preview", ".0-preview");
return versionPart.join(".");
}
}
if (versionPart[2] == null) {
//append patch version if not available
// e.g. 2.1 -> 2.1.0
return version.concat('.0');
return version.concat(".0");
}
else {
// handle beta and rc
// e.g. 1.8.5beta1 -> 1.8.5-beta1, 1.8.5rc1 -> 1.8.5-rc1
if (preStrings.some(el => versionPart[1].includes(el))) {
if (preStrings.some(el => versionPart[2].includes(el))) {
versionPart[2] = versionPart[2]
.replace('beta', '-beta')
.replace('rc', '-rc')
.replace('preview', '-preview');
return versionPart.join('.');
.replace("beta", "-beta")
.replace("rc", "-rc")
.replace("preview", "-preview");
return versionPart.join(".");
}
}
return version;
Expand Down
7 changes: 4 additions & 3 deletions setup-taskfile/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const installer = __importStar(require("./installer"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
let version = core.getInput('version');
let version = core.getInput("version");
let repoToken = core.getInput("repo-token");
if (!version) {
version = '2.x';
version = "2.x";
}
if (version) {
yield installer.getTask(version);
yield installer.getTask(version, repoToken);
}
}
catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions setup-taskfile/node_modules/@actions/core/package.json

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

23 changes: 13 additions & 10 deletions setup-taskfile/node_modules/@actions/exec/package.json

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

4 changes: 2 additions & 2 deletions setup-taskfile/node_modules/@actions/io/package.json

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

Loading