@@ -6,41 +6,52 @@ import * as github from "@actions/github";
6
6
import * as glob from "@actions/glob" ;
7
7
import * as tc from "@actions/tool-cache" ;
8
8
import fs from "fs" ;
9
- import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated" ;
10
9
import * as os from "os" ;
11
10
import { join } from "node:path" ;
12
11
import { exit } from "process" ;
13
12
import * as semver from "semver" ;
14
13
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 ;
22
18
const repoSet = {
23
19
owner : "typst" ,
24
20
repo : "typst" ,
25
21
} ;
26
22
let version = core . getInput ( "typst-version" ) ;
27
23
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
+
32
42
const versions = releases
33
43
. map ( ( release ) => release . tag_name . slice ( 1 ) )
34
44
. filter ( ( v ) => semver . valid ( v ) ) ;
35
45
version = semver . maxSatisfying ( versions , version === "latest" ? "*" : version , {
36
46
includePrerelease : allowPrereleases ? true : false ,
37
47
} ) ! ;
38
48
core . debug ( `Resolved version: v${ version } ` ) ;
39
- if ( ! version )
49
+ if ( ! version ) {
40
50
core . setFailed (
41
- `Typst ${ core . getInput ( "typst-version" ) } could not be resolved` ,
51
+ `Typst ${ core . getInput ( "typst-version" ) } could not be resolved.`
42
52
) ;
43
-
53
+ process . exit ( 1 ) ;
54
+ }
44
55
let found = tc . find ( "typst" , version ) ;
45
56
core . setOutput ( "cache-hit" , ! ! found ) ;
46
57
if ( ! found ) {
@@ -60,7 +71,7 @@ if (!found) {
60
71
const folder = `typst-${ target } ` ;
61
72
const file = `${ folder } ${ archiveExt } ` ;
62
73
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 } `
64
75
) ;
65
76
if ( file . endsWith ( ".zip" ) ) {
66
77
found = await tc . extractZip ( found ) ;
@@ -83,7 +94,7 @@ if (cachePackage) {
83
94
join (
84
95
process . env . XDG_CACHE_HOME ||
85
96
( os . homedir ( ) ? join ( os . homedir ( ) , ".cache" ) : undefined ) ! ,
86
- "typst/packages" ,
97
+ "typst/packages"
87
98
) ,
88
99
darwin : ( ) => join ( process . env . HOME ! , "Library/Caches" , "typst/packages" ) ,
89
100
win32 : ( ) => join ( process . env . LOCALAPPDATA ! , "typst/packages" ) ,
@@ -98,18 +109,15 @@ if (cachePackage) {
98
109
let cacheId = 0 ;
99
110
try {
100
111
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 } .` ) ;
104
114
}
105
115
if ( cacheId != - 1 ) {
106
116
core . info ( `✅ Cache saved with the key: ${ primaryKey } ` ) ;
107
117
}
108
118
exit ( 0 ) ;
109
119
}
110
120
} 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.` ) ;
114
122
}
115
123
}
0 commit comments