Skip to content

Commit c595823

Browse files
committed
Remove createUnauthenticatedAuth for better token handling
1 parent 471e391 commit c595823

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

package-lock.json

-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@actions/github": "^6.0.0",
88
"@actions/glob": "^0.5.0",
99
"@actions/tool-cache": "^2.0.1",
10-
"@octokit/auth-unauthenticated": "^5.0.1",
1110
"@vercel/ncc": "^0.38.1",
1211
"semver": "^7.5.4"
1312
},

src/main.ts

+31-23
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,52 @@ import * as github from "@actions/github";
66
import * as glob from "@actions/glob";
77
import * as tc from "@actions/tool-cache";
88
import fs from "fs";
9-
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
109
import * as os from "os";
1110
import { join } from "node:path";
1211
import { exit } from "process";
1312
import * as semver from "semver";
1413

15-
const octokit = core.getInput("token")
16-
? github.getOctokit(core.getInput("token"))
17-
: github.getOctokit(undefined!, {
18-
authStrategy: createUnauthenticatedAuth,
19-
auth: { reason: "no 'token' input" },
20-
});
21-
14+
const token = core.getInput("token");
15+
const octokit = token
16+
? github.getOctokit(token, { baseUrl: "https://api.github.com" })
17+
: null;
2218
const repoSet = {
2319
owner: "typst",
2420
repo: "typst",
2521
};
2622
let version = core.getInput("typst-version");
2723
const allowPrereleases = core.getBooleanInput("allow-prereleases");
28-
const releases = await octokit.paginate(
29-
octokit.rest.repos.listReleases,
30-
repoSet,
31-
);
24+
25+
let releases: any[] = [];
26+
27+
if (octokit) {
28+
releases = await octokit.paginate(octokit.rest.repos.listReleases, repoSet);
29+
} else {
30+
const releasesUrl = `https://api.github.com/repos/typst/typst/releases`;
31+
const releasesResponse = await tc.downloadTool(releasesUrl);
32+
try {
33+
releases = JSON.parse(fs.readFileSync(releasesResponse, "utf8"));
34+
} catch (error) {
35+
core.setFailed(
36+
`Failed to parse releases: ${(error as Error).message}. This could be caused by API rate limit exceeded.`
37+
);
38+
process.exit(1);
39+
}
40+
}
41+
3242
const versions = releases
3343
.map((release) => release.tag_name.slice(1))
3444
.filter((v) => semver.valid(v));
3545
version = semver.maxSatisfying(versions, version === "latest" ? "*" : version, {
3646
includePrerelease: allowPrereleases ? true : false,
3747
})!;
3848
core.debug(`Resolved version: v${version}`);
39-
if (!version)
49+
if (!version) {
4050
core.setFailed(
41-
`Typst ${core.getInput("typst-version")} could not be resolved`,
51+
`Typst ${core.getInput("typst-version")} could not be resolved.`
4252
);
43-
53+
process.exit(1);
54+
}
4455
let found = tc.find("typst", version);
4556
core.setOutput("cache-hit", !!found);
4657
if (!found) {
@@ -60,7 +71,7 @@ if (!found) {
6071
const folder = `typst-${target}`;
6172
const file = `${folder}${archiveExt}`;
6273
found = await tc.downloadTool(
63-
`https://github.com/typst/typst/releases/download/v${version}/${file}`,
74+
`https://github.com/typst/typst/releases/download/v${version}/${file}`
6475
);
6576
if (file.endsWith(".zip")) {
6677
found = await tc.extractZip(found);
@@ -83,7 +94,7 @@ if (cachePackage) {
8394
join(
8495
process.env.XDG_CACHE_HOME ||
8596
(os.homedir() ? join(os.homedir(), ".cache") : undefined)!,
86-
"typst/packages",
97+
"typst/packages"
8798
),
8899
darwin: () => join(process.env.HOME!, "Library/Caches", "typst/packages"),
89100
win32: () => join(process.env.LOCALAPPDATA!, "typst/packages"),
@@ -98,18 +109,15 @@ if (cachePackage) {
98109
let cacheId = 0;
99110
try {
100111
cacheId = await cache.saveCache([cacheDir], primaryKey);
101-
} catch (err) {
102-
const message = (err as Error).message;
103-
core.warning(message);
112+
} catch (error) {
113+
core.warning(`Failed to save cache: ${(error as Error).message}.`);
104114
}
105115
if (cacheId != -1) {
106116
core.info(`✅ Cache saved with the key: ${primaryKey}`);
107117
}
108118
exit(0);
109119
}
110120
} else {
111-
core.warning(
112-
`${cachePackage} was not found. Packages will not be cached.`,
113-
);
121+
core.warning(`${cachePackage} is not found. Packages will not be cached.`);
114122
}
115123
}

0 commit comments

Comments
 (0)