Skip to content

Make default version "latest" #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
name: 'Install HLint to be used with hlint-run'
description: 'Cached download of hlint binary release to be used with hlint-run'
inputs:
token:
description: The token to use when communicating with GitHub's API.
required: false
default: ${{ github.token }}
version:
description: 'hlint version'
required: false
default: '3.8'
default: latest
outputs:
hlint-dir:
description: 'Directory containing the hlint executable'
Expand Down
27 changes: 23 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 27 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as os from 'os'
import * as path from 'path'
import * as httpClient from '@actions/http-client'

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

const HTTP_CLIENT = new httpClient.HttpClient('haskell-actions/hlint-setup');

interface PlatformArchiveConfig {
toolType: {pkgPlatform: string, ext: string},
archiveType: {ext: string, extract: (archivePath: string, toDir: string) => Promise<string>},
Expand Down Expand Up @@ -45,7 +48,21 @@ interface HLintReleaseConfig {
archive: ArchiveConfig,
};

function mkHlintReleaseConfig(nodeOsPlatform: string, nodeArch: string, hlintVersion: string): HLintReleaseConfig {
async function getLatestHlintVersion(githubToken: string): Promise<string> {
const headers: { [key: string]: string } = {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
};
if (githubToken) {
headers['Authorization'] = `Bearer ${githubToken}`;
}
const response = await HTTP_CLIENT.getJson(
'https://api.github.com/repos/ndmitchell/hlint/releases/latest',
headers);
return (response.result as { tag_name: string }).tag_name.replace(/^v/, '');
}

async function mkHlintReleaseConfig(nodeOsPlatform: string, nodeArch: string, requestedVersion: string, githubToken: string): Promise<HLintReleaseConfig> {
const config = HLINT_PLATFORM_ARCHIVE_CONFIG[nodeOsPlatform];
if (!config) {
throw Error(`Invalid platform for hlint: ${nodeOsPlatform}`);
Expand All @@ -57,6 +74,11 @@ function mkHlintReleaseConfig(nodeOsPlatform: string, nodeArch: string, hlintVer

const {toolType: {pkgPlatform, ext: exeExt}, archiveType: {ext: archiveExt, extract}} = config;

let hlintVersion = requestedVersion;
if (hlintVersion === HLINT_DEFAULT_VERSION) {
hlintVersion = await getLatestHlintVersion(githubToken);
}

const toolName = 'hlint';
const releaseName = `${toolName}-${hlintVersion}`;
const archiveName = `${releaseName}-${pkgArch}-${pkgPlatform}.${archiveExt}`;
Expand Down Expand Up @@ -108,17 +130,19 @@ async function findOrDownloadHlint(hlintReleaseConfig: HLintReleaseConfig): Prom
}
}

const HLINT_DEFAULT_VERSION = '3.1.6';
const HLINT_DEFAULT_VERSION = 'latest';

const INPUT_KEY_HLINT_VERSION = 'version';
const INPUT_KEY_GITHUB_TOKEN = 'token';
const OUTPUT_KEY_HLINT_DIR = 'hlint-dir';
const OUTPUT_KEY_HLINT_PATH = 'hlint-bin';
const OUTPUT_KEY_HLINT_VERSION = 'version';

async function run() {
try {
const hlintVersion = core.getInput(INPUT_KEY_HLINT_VERSION) || HLINT_DEFAULT_VERSION;
const config = mkHlintReleaseConfig(process.platform, os.arch(), hlintVersion);
const githubToken = core.getInput(INPUT_KEY_GITHUB_TOKEN);
const config = await mkHlintReleaseConfig(process.platform, os.arch(), hlintVersion, githubToken);
const hlintDir = await findOrDownloadHlint(config);
core.addPath(hlintDir);
core.info(`hlint ${config.tool.version} is now set up at ${hlintDir}`);
Expand Down