@@ -2,10 +2,13 @@ import * as core from '@actions/core'
2
2
import * as tc from '@actions/tool-cache'
3
3
import * as os from 'os'
4
4
import * as path from 'path'
5
+ import * as httpClient from '@actions/http-client'
5
6
6
7
const TAR_ARCHIVE = { ext : 'tar.gz' , extract : tc . extractTar } ;
7
8
const ZIP_ARCHIVE = { ext : 'zip' , extract : tc . extractZip } ;
8
9
10
+ const HTTP_CLIENT = new httpClient . HttpClient ( 'haskell-actions/hlint-setup' ) ;
11
+
9
12
interface PlatformArchiveConfig {
10
13
toolType : { pkgPlatform : string , ext : string } ,
11
14
archiveType : { ext : string , extract : ( archivePath : string , toDir : string ) => Promise < string > } ,
@@ -45,7 +48,21 @@ interface HLintReleaseConfig {
45
48
archive : ArchiveConfig ,
46
49
} ;
47
50
48
- function mkHlintReleaseConfig ( nodeOsPlatform : string , nodeArch : string , hlintVersion : string ) : HLintReleaseConfig {
51
+ async function getLatestHlintVersion ( githubToken : string ) : Promise < string > {
52
+ const headers : { [ key : string ] : string } = {
53
+ Accept : 'application/vnd.github+json' ,
54
+ 'X-GitHub-Api-Version' : '2022-11-28' ,
55
+ } ;
56
+ if ( githubToken ) {
57
+ headers [ 'Authorization' ] = `Bearer ${ githubToken } ` ;
58
+ }
59
+ const response = await HTTP_CLIENT . getJson (
60
+ 'https://api.github.com/repos/ndmitchell/hlint/releases/latest' ,
61
+ headers ) ;
62
+ return ( response . result as { tag_name : string } ) . tag_name . replace ( / ^ v / , '' ) ;
63
+ }
64
+
65
+ async function mkHlintReleaseConfig ( nodeOsPlatform : string , nodeArch : string , requestedVersion : string , githubToken : string ) : Promise < HLintReleaseConfig > {
49
66
const config = HLINT_PLATFORM_ARCHIVE_CONFIG [ nodeOsPlatform ] ;
50
67
if ( ! config ) {
51
68
throw Error ( `Invalid platform for hlint: ${ nodeOsPlatform } ` ) ;
@@ -57,6 +74,11 @@ function mkHlintReleaseConfig(nodeOsPlatform: string, nodeArch: string, hlintVer
57
74
58
75
const { toolType : { pkgPlatform, ext : exeExt } , archiveType : { ext : archiveExt , extract} } = config ;
59
76
77
+ let hlintVersion = requestedVersion ;
78
+ if ( hlintVersion === HLINT_DEFAULT_VERSION ) {
79
+ hlintVersion = await getLatestHlintVersion ( githubToken ) ;
80
+ }
81
+
60
82
const toolName = 'hlint' ;
61
83
const releaseName = `${ toolName } -${ hlintVersion } ` ;
62
84
const archiveName = `${ releaseName } -${ pkgArch } -${ pkgPlatform } .${ archiveExt } ` ;
@@ -108,17 +130,19 @@ async function findOrDownloadHlint(hlintReleaseConfig: HLintReleaseConfig): Prom
108
130
}
109
131
}
110
132
111
- const HLINT_DEFAULT_VERSION = '3.1.6 ' ;
133
+ const HLINT_DEFAULT_VERSION = 'latest ' ;
112
134
113
135
const INPUT_KEY_HLINT_VERSION = 'version' ;
136
+ const INPUT_KEY_GITHUB_TOKEN = 'token' ;
114
137
const OUTPUT_KEY_HLINT_DIR = 'hlint-dir' ;
115
138
const OUTPUT_KEY_HLINT_PATH = 'hlint-bin' ;
116
139
const OUTPUT_KEY_HLINT_VERSION = 'version' ;
117
140
118
141
async function run ( ) {
119
142
try {
120
143
const hlintVersion = core . getInput ( INPUT_KEY_HLINT_VERSION ) || HLINT_DEFAULT_VERSION ;
121
- const config = mkHlintReleaseConfig ( process . platform , os . arch ( ) , hlintVersion ) ;
144
+ const githubToken = core . getInput ( INPUT_KEY_GITHUB_TOKEN ) ;
145
+ const config = await mkHlintReleaseConfig ( process . platform , os . arch ( ) , hlintVersion , githubToken ) ;
122
146
const hlintDir = await findOrDownloadHlint ( config ) ;
123
147
core . addPath ( hlintDir ) ;
124
148
core . info ( `hlint ${ config . tool . version } is now set up at ${ hlintDir } ` ) ;
0 commit comments