don't depend on the GitHub API to check release

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-01-28 22:21:40 +01:00
parent 0648fd6fd6
commit 39322d9057
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
13 changed files with 60 additions and 261 deletions

View file

@ -4,9 +4,9 @@ import * as semver from 'semver';
import * as util from 'util';
import * as context from './context';
import * as git from './git';
import * as github from './github';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import * as tc from '@actions/tool-cache';
export type Builder = {
@ -25,6 +25,29 @@ export type Node = {
platforms?: string;
};
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/docker/buildx/master/.github/releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('setup-buildx');
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 Buildx 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 Buildx release ${version} in ${url}`);
}
return releases[version];
};
export async function getConfigInline(s: string): Promise<string> {
return getConfig(s, false);
}
@ -237,13 +260,8 @@ export async function build(inputBuildRef: string, dest: string, standalone: boo
return setPlugin(toolPath, dest);
}
export async function install(inputVersion: string, githubToken: string, dest: string, standalone: boolean): Promise<string> {
let release: github.Release;
if (inputVersion == 'latest') {
release = await github.getLatestRelease(githubToken);
} else {
release = await github.getReleaseTag(inputVersion, githubToken);
}
export async function install(inputVersion: string, dest: string, standalone: boolean): Promise<string> {
const release: GitHubRelease = await getRelease(inputVersion);
core.debug(`Release ${release.tag_name} found`);
const version = release.tag_name.replace(/^v+|v+$/g, '');

View file

@ -36,7 +36,6 @@ export interface Inputs {
config: string;
configInline: string;
append: string;
githubToken: string;
}
export async function getInputs(): Promise<Inputs> {
@ -52,8 +51,7 @@ export async function getInputs(): Promise<Inputs> {
endpoint: core.getInput('endpoint'),
config: core.getInput('config'),
configInline: core.getInput('config-inline'),
append: core.getInput('append'),
githubToken: core.getInput('github_token')
append: core.getInput('append')
};
}

View file

@ -1,41 +0,0 @@
import * as github from '@actions/github';
export interface Release {
id: number;
tag_name: string;
}
const [owner, repo] = 'docker/buildx'.split('/');
export const getReleaseTag = async (tag: string, githubToken: string): Promise<Release> => {
return (
await github
.getOctokit(githubToken, {
baseUrl: 'https://api.github.com'
})
.rest.repos.getReleaseByTag({
owner,
repo,
tag
})
.catch(error => {
throw new Error(`Cannot get release ${tag}: ${error}`);
})
).data as Release;
};
export const getLatestRelease = async (githubToken: string): Promise<Release> => {
return (
await github
.getOctokit(githubToken, {
baseUrl: 'https://api.github.com'
})
.rest.repos.getLatestRelease({
owner,
repo
})
.catch(error => {
throw new Error(`Cannot get latest release: ${error}`);
})
).data as Release;
};

View file

@ -42,7 +42,7 @@ async function run(): Promise<void> {
core.endGroup();
} else if (!(await buildx.isAvailable(standalone)) || inputs.version) {
core.startGroup(`Download and install buildx`);
await buildx.install(inputs.version || 'latest', inputs.githubToken, standalone ? context.tmpDir() : dockerConfigHome, standalone);
await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone);
core.endGroup();
}