Skip to content

Commit

Permalink
Merge pull request #261 from crazy-max/fix-dl
Browse files Browse the repository at this point in the history
check latest and tagged releases using releases-json
  • Loading branch information
crazy-max authored Jan 29, 2023
2 parents 331a39b + d15d64d commit 1a3a265
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
16 changes: 0 additions & 16 deletions __tests__/github.test.ts

This file was deleted.

20 changes: 20 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import {describe, expect, it} from '@jest/globals';
import * as fs from 'fs';
import * as installer from '../src/installer';

describe('getRelease', () => {
it('returns latest buildx GitHub release', async () => {
const release = await installer.getRelease('latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});

it('returns v0.24.0 buildx GitHub release', async () => {
const release = await installer.getRelease('v0.24.0');
expect(release).not.toBeNull();
expect(release?.id).toEqual(88251102);
expect(release?.tag_name).toEqual('v0.24.0');
expect(release?.html_url).toEqual('https://github.com/crazy-max/xgo/releases/tag/v0.24.0');
});

it('unknown release', async () => {
await expect(installer.getRelease('foo')).rejects.toThrowError(new Error('Cannot find Xgo release foo in https://raw.githubusercontent.com/crazy-max/ghaction-xgo/master/.github/xgo-releases.json'));
});
});

describe('installer', () => {
it('acquires v0.6.0 version of xgo', async () => {
const xgo = await installer.getXgo('v0.6.0');
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions src/github.ts

This file was deleted.

30 changes: 25 additions & 5 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as path from 'path';
import * as os from 'os';
import * as semver from 'semver';
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();
Expand All @@ -15,11 +15,31 @@ export interface Xgo {
version: string;
}

export async function getXgo(version: string): Promise<Xgo> {
const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) {
throw new Error(`Cannot find xgo ${version} release`);
export interface GitHubRelease {
id: number;
tag_name: string;
html_url: string;
assets: Array<string>;
}

export const getRelease = async (version: string): Promise<GitHubRelease> => {
const url = `https://raw.githubusercontent.com/crazy-max/ghaction-xgo/master/.github/xgo-releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('ghaction-xgo');
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 Xgo release ${version} from ${url} with status code ${statusCode}: ${body}`);
}
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
if (!releases[version]) {
throw new Error(`Cannot find Xgo release ${version} in ${url}`);
}
return releases[version];
};

export async function getXgo(version: string): Promise<Xgo> {
const release: GitHubRelease = await getRelease(version);
const fullversion: string = release.tag_name.replace(/^v/, '');
core.debug(`Release found: ${release.tag_name}`);

Expand Down

0 comments on commit 1a3a265

Please sign in to comment.