From c26e09cbb40d0158ddbfbdff5df0834c1b2501c8 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 29 Jan 2023 16:03:29 +0100 Subject: [PATCH] check latest and tagged releases using releases-json --- __tests__/github.test.ts | 16 ---------------- __tests__/installer.test.ts | 24 ++++++++++++++++++++++++ src/github.ts | 12 ------------ src/installer.ts | 32 +++++++++++++++++++++++++------- 4 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 __tests__/github.test.ts delete mode 100644 src/github.ts diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts deleted file mode 100644 index fd98452..0000000 --- a/__tests__/github.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {describe, expect, it} from '@jest/globals'; -import * as github from '../src/github'; - -describe('github', () => { - it('returns latest UPX GitHub release', async () => { - const release = await github.getRelease('latest'); - expect(release).not.toBeNull(); - expect(release?.tag_name).not.toEqual(''); - }); - - it('returns v3.96 GoReleaser GitHub release', async () => { - const release = await github.getRelease('v3.96'); - expect(release).not.toBeNull(); - expect(release?.tag_name).toEqual('v3.96'); - }); -}); diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 2253241..863bbd2 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -2,6 +2,30 @@ import {describe, expect, it} from '@jest/globals'; import * as fs from 'fs'; import * as installer from '../src/installer'; +describe('getRelease', () => { + it('returns latest UPX GitHub release', async () => { + const release = await installer.getRelease('latest'); + expect(release).not.toBeNull(); + expect(release?.tag_name).not.toEqual(''); + }); + + it('returns v3.95 UPX GitHub release', async () => { + const release = await installer.getRelease('v3.95'); + expect(release).not.toBeNull(); + expect(release?.id).toEqual(12577195); + expect(release?.tag_name).toEqual('v3.95'); + expect(release?.html_url).toEqual('https://github.com/upx/upx/releases/tag/v3.95'); + }); + + it('unknown release', async () => { + await expect(installer.getRelease('foo')).rejects.toThrowError( + new Error( + 'Cannot find UPX release foo in https://raw.githubusercontent.com/crazy-max/ghaction-upx/master/.github/upx-releases.json' + ) + ); + }); +}); + describe('installer', () => { it('acquires v3.95 version of UPX', async () => { const upx = await installer.getUPX('v3.95'); diff --git a/src/github.ts b/src/github.ts deleted file mode 100644 index fa0bc4a..0000000 --- a/src/github.ts +++ /dev/null @@ -1,12 +0,0 @@ -import * as httpm from '@actions/http-client'; - -export interface GitHubRelease { - id: number; - tag_name: string; -} - -export const getRelease = async (version: string): Promise => { - const url = `https://github.com/upx/upx/releases/${version}`; - const http: httpm.HttpClient = new httpm.HttpClient('ghaction-upx'); - return (await http.getJson(url)).result; -}; diff --git a/src/installer.ts b/src/installer.ts index 98133cb..7709a24 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -1,22 +1,40 @@ import * as os from 'os'; import * as path from 'path'; import * as util from 'util'; -import * as github from './github'; import * as core from '@actions/core'; +import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; const osPlat: string = os.platform(); const osArch: string = os.arch(); -export async function getUPX(version: string): Promise { - core.startGroup(`Checking UPX ${version} release...`); - const release: github.GitHubRelease | null = await github.getRelease(version); - if (!release) { - throw new Error(`Cannot find UPX ${version} release`); +export interface GitHubRelease { + id: number; + tag_name: string; + html_url: string; + assets: Array; +} + +export const getRelease = async (version: string): Promise => { + const url = `https://raw.githubusercontent.com/crazy-max/ghaction-upx/master/.github/upx-releases.json`; + const http: httpm.HttpClient = new httpm.HttpClient('ghaction-upx'); + const resp: httpm.HttpClientResponse = await http.get(url); + const body = await resp.readBody(); + const statusCode = resp.message.statusCode || 500; + if (statusCode >= 400) { + throw new Error(`Failed to get UPX release ${version} from ${url} with status code ${statusCode}: ${body}`); } + const releases = >JSON.parse(body); + if (!releases[version]) { + throw new Error(`Cannot find UPX release ${version} in ${url}`); + } + return releases[version]; +}; + +export async function getUPX(version: string): Promise { + const release: GitHubRelease = await getRelease(version); const semver: string = release.tag_name.replace(/^v/, ''); core.info(`UPX ${semver} found`); - core.endGroup(); const filename = util.format('%s.%s', getName(semver), osPlat == 'win32' ? 'zip' : 'tar.xz'); const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', semver, filename);