@@ -2,10 +2,13 @@ import * as core from '@actions/core'
22import * as tc from '@actions/tool-cache'
33import * as os from 'os'
44import * as path from 'path'
5+ import * as httpClient from '@actions/http-client'
56
67const TAR_ARCHIVE = { ext : 'tar.gz' , extract : tc . extractTar } ;
78const ZIP_ARCHIVE = { ext : 'zip' , extract : tc . extractZip } ;
89
10+ const HTTP_CLIENT = new httpClient . HttpClient ( 'haskell-actions/hlint-setup' ) ;
11+
912interface PlatformArchiveConfig {
1013 toolType : { pkgPlatform : string , ext : string } ,
1114 archiveType : { ext : string , extract : ( archivePath : string , toDir : string ) => Promise < string > } ,
@@ -45,7 +48,21 @@ interface HLintReleaseConfig {
4548 archive : ArchiveConfig ,
4649} ;
4750
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 > {
4966 const config = HLINT_PLATFORM_ARCHIVE_CONFIG [ nodeOsPlatform ] ;
5067 if ( ! config ) {
5168 throw Error ( `Invalid platform for hlint: ${ nodeOsPlatform } ` ) ;
@@ -57,6 +74,11 @@ function mkHlintReleaseConfig(nodeOsPlatform: string, nodeArch: string, hlintVer
5774
5875 const { toolType : { pkgPlatform, ext : exeExt } , archiveType : { ext : archiveExt , extract} } = config ;
5976
77+ let hlintVersion = requestedVersion ;
78+ if ( hlintVersion === HLINT_DEFAULT_VERSION ) {
79+ hlintVersion = await getLatestHlintVersion ( githubToken ) ;
80+ }
81+
6082 const toolName = 'hlint' ;
6183 const releaseName = `${ toolName } -${ hlintVersion } ` ;
6284 const archiveName = `${ releaseName } -${ pkgArch } -${ pkgPlatform } .${ archiveExt } ` ;
@@ -108,17 +130,19 @@ async function findOrDownloadHlint(hlintReleaseConfig: HLintReleaseConfig): Prom
108130 }
109131}
110132
111- const HLINT_DEFAULT_VERSION = '3.1.6 ' ;
133+ const HLINT_DEFAULT_VERSION = 'latest ' ;
112134
113135const INPUT_KEY_HLINT_VERSION = 'version' ;
136+ const INPUT_KEY_GITHUB_TOKEN = 'token' ;
114137const OUTPUT_KEY_HLINT_DIR = 'hlint-dir' ;
115138const OUTPUT_KEY_HLINT_PATH = 'hlint-bin' ;
116139const OUTPUT_KEY_HLINT_VERSION = 'version' ;
117140
118141async function run ( ) {
119142 try {
120143 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 ) ;
122146 const hlintDir = await findOrDownloadHlint ( config ) ;
123147 core . addPath ( hlintDir ) ;
124148 core . info ( `hlint ${ config . tool . version } is now set up at ${ hlintDir } ` ) ;
0 commit comments