Skip to content

Commit aa1e929

Browse files
authored
Make default version "latest" (#60)
* Run `yarn package` * Automatically fetch the latest version
1 parent 7ea8002 commit aa1e929

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

action.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
name: 'Install HLint to be used with hlint-run'
22
description: 'Cached download of hlint binary release to be used with hlint-run'
33
inputs:
4+
token:
5+
description: The token to use when communicating with GitHub's API.
6+
required: false
7+
default: ${{ github.token }}
48
version:
59
description: 'hlint version'
610
required: false
7-
default: '3.8'
11+
default: latest
812
outputs:
913
hlint-dir:
1014
description: 'Directory containing the hlint executable'

dist/index.js

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

dist/index.js.map

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

src/index.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import * as core from '@actions/core'
22
import * as tc from '@actions/tool-cache'
33
import * as os from 'os'
44
import * as path from 'path'
5+
import * as httpClient from '@actions/http-client'
56

67
const TAR_ARCHIVE = {ext: 'tar.gz', extract: tc.extractTar};
78
const ZIP_ARCHIVE = {ext: 'zip', extract: tc.extractZip};
89

10+
const HTTP_CLIENT = new httpClient.HttpClient('haskell-actions/hlint-setup');
11+
912
interface 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

113135
const INPUT_KEY_HLINT_VERSION = 'version';
136+
const INPUT_KEY_GITHUB_TOKEN = 'token';
114137
const OUTPUT_KEY_HLINT_DIR = 'hlint-dir';
115138
const OUTPUT_KEY_HLINT_PATH = 'hlint-bin';
116139
const OUTPUT_KEY_HLINT_VERSION = 'version';
117140

118141
async 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

Comments
 (0)