Skip to content

Commit

Permalink
fix: migration
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jun 27, 2024
1 parent e6e389b commit 7f29a02
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
26 changes: 16 additions & 10 deletions src/commands/binary/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { getInput } from '@actions/core';
import is from '@sindresorhus/is';
import {
isNonEmptyArray,
isNonEmptyString,
isObject,
isString,
} from '@sindresorhus/is';
import { getDefaultVersioning } from 'renovate/dist/modules/datasource/common';
import { getArch, getArg, getDistro, isDryRun, readJson } from '../../util';
import { readDockerConfig } from '../../utils/config';
import {
DockerArch,
DockerPlatform,
dockerBuildx,
dockerRun,
Expand All @@ -16,15 +22,15 @@ export async function getConfig(): Promise<BinaryBuilderConfig> {

const cfg = await readJson<ConfigFile>(configFile);

if (!is.object(cfg)) {
if (!isObject(cfg)) {
throw new Error('missing-config');
}

if (!is.string(cfg.image)) {
if (!isString(cfg.image)) {
cfg.image = getInput('image', { required: true });
}

if (!is.string(cfg.buildArg)) {
if (!isString(cfg.buildArg)) {
cfg.buildArg = cfg.image.toUpperCase() + '_VERSION';
}

Expand All @@ -48,13 +54,13 @@ export async function createBuilderImage(
const args = ['build', '--load', '-t', 'builder'];
const distro = getDistro();
const arch = getArch();
if (is.nonEmptyString(distro)) {
if (isNonEmptyString(distro)) {
args.push('--build-arg', `DISTRO=${distro}`);
}
if (is.nonEmptyString(arch)) {
args.push('--platform', DockerPlatform[arch]);
if (isNonEmptyString(arch)) {
args.push('--platform', DockerPlatform[arch as DockerArch]);
}
if (is.nonEmptyArray(buildArgs)) {
if (isNonEmptyArray(buildArgs)) {
args.push(...buildArgs.map((b) => `--build-arg=${b}`));
}
await dockerBuildx(...args, ws);
Expand All @@ -63,8 +69,8 @@ export async function createBuilderImage(
export async function runBuilder(ws: string, version: string): Promise<void> {
const args = ['--name', 'builder', '--volume', `${ws}/.cache:/cache`];
const arch = getArch();
if (is.nonEmptyString(arch)) {
args.push('--platform', DockerPlatform[arch]);
if (isNonEmptyString(arch)) {
args.push('--platform', DockerPlatform[arch as DockerArch]);
}
await dockerRun(...args, 'builder', version);
}
25 changes: 15 additions & 10 deletions src/commands/docker/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getInput, setFailed } from '@actions/core';
import is from '@sindresorhus/is';
import {
isNonEmptyString,
isNumber,
isObject,
isString,
} from '@sindresorhus/is';
import chalk from 'chalk';
import { getDefaultVersioning } from 'renovate/dist/modules/datasource/common';
import { get as getVersioning } from 'renovate/dist/modules/versioning';
Expand All @@ -14,7 +19,7 @@ import log from '../../utils/logger';
import type { ConfigFile, DockerBuilderConfig } from '../../utils/types';

function createTag(tagSuffix: string | undefined, version: string): string {
return is.nonEmptyString(tagSuffix) && tagSuffix !== 'latest'
return isNonEmptyString(tagSuffix) && tagSuffix !== 'latest'
? `${version}-${tagSuffix}`
: version;
}
Expand Down Expand Up @@ -51,14 +56,14 @@ async function buildAndPush(
const major = ver.getMajor(version);
const isStable = ver.isStable(version);

if (isStable && is.number(major) && `${major}` !== version) {
if (isStable && isNumber(major) && `${major}` !== version) {
versionsMap.set(`${major}`, version);
}

if (
isStable &&
is.number(major) &&
is.number(minor) &&
isNumber(major) &&
isNumber(minor) &&
`${major}.${minor}` !== version
) {
versionsMap.set(`${major}.${minor}`, version);
Expand Down Expand Up @@ -93,7 +98,7 @@ async function buildAndPush(
const cacheToTags: string[] = [];
const tags: string[] = [];

if (is.number(major)) {
if (isNumber(major)) {
const nTag = createTag(tagSuffix, `${major}`);
cacheFromTags.push(nTag);
if (versionsMap.get(`${major}`) === version) {
Expand All @@ -103,7 +108,7 @@ async function buildAndPush(
}
}
}
if (is.number(major) && is.number(minor)) {
if (isNumber(major) && isNumber(minor)) {
const nTag = createTag(tagSuffix, `${major}.${minor}`);
cacheFromTags.push(nTag);
if (versionsMap.get(`${major}.${minor}`) === version) {
Expand Down Expand Up @@ -198,16 +203,16 @@ export async function run(): Promise<void> {

const cfg = await readJson<ConfigFile>(configFile);

if (!is.object(cfg)) {
if (!isObject(cfg)) {
throw new Error('missing-config');
}

// TODO: validation
if (!is.string(cfg.image)) {
if (!isString(cfg.image)) {
cfg.image = getInput('image', { required: true });
}

if (!is.string(cfg.buildArg)) {
if (!isString(cfg.buildArg)) {
cfg.buildArg = cfg.image.toUpperCase() + '_VERSION';
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { endGroup, getInput, startGroup } from '@actions/core';
import { exec as _exec } from '@actions/exec';
import type { ExecOptions as _ExecOptions } from '@actions/exec/lib/interfaces';
import { which } from '@actions/io';
import is from '@sindresorhus/is';
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import findUp from 'find-up';
import type { DockerArch } from './utils/docker/common';
import { ExecError, ExecResult } from './utils/types';
Expand Down Expand Up @@ -130,7 +130,7 @@ export function getArg(
/* c8 ignore stop */
const val = getInput(name, opts);
return opts?.multi
? val.split(MultiArgsSplitRe).filter(is.nonEmptyStringAndNotWhitespace)
? val.split(MultiArgsSplitRe).filter(isNonEmptyStringAndNotWhitespace)
: val;
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/builds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import is from '@sindresorhus/is';
import { isNumber, isString } from '@sindresorhus/is';
import {
ReleaseResult,
getPkgReleases,
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function getBuildList({
allVersions = allVersions.filter((v) => ver.isStable(v));
}

if (is.string(allowedVersions)) {
if (isString(allowedVersions)) {
const isAllowedPred = getRegexPredicate(allowedVersions);
if (isAllowedPred) {
allVersions = allVersions.filter((version) => isAllowedPred(version));
Expand Down Expand Up @@ -142,7 +142,7 @@ export async function getBuildList({
const lastVersion = allVersions.at(-1)!;
log('Most recent version is', lastVersion);

if (is.number(maxVersions) && maxVersions > 0) {
if (isNumber(maxVersions) && maxVersions > 0) {
log(`Building last ${maxVersions} version only`);
allVersions = allVersions.slice(-maxVersions);
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import is from '@sindresorhus/is';
import { isNonEmptyString, isString } from '@sindresorhus/is';
import escapeStringRegexp from 'escape-string-regexp';
import { getArch, getDistro, readFile } from '../util';
import { BinaryBuilderConfig, ConfigFile, sumType } from './types';
Expand All @@ -17,7 +17,7 @@ function checkArgs(
groups: Record<string, string | undefined>,
): void {
for (const key of keys) {
if (!is.string(cfg[key]) && is.nonEmptyString(groups[key])) {
if (!isString(cfg[key]) && isNonEmptyString(groups[key])) {
cfg[key] = groups[key] as never;
}
}
Expand Down Expand Up @@ -46,10 +46,10 @@ export function getBinaryName(
const arch = getArch();
const ext = sum ? `.${sumType}` : '';
let image = `${cfg.image}-${version}`;
if (is.nonEmptyString(distro)) {
if (isNonEmptyString(distro)) {
image += `-${distro}`;
}
if (is.nonEmptyString(arch)) {
if (isNonEmptyString(arch)) {
image += `-${arch}`;
}
return `${image}.tar.xz${ext}`;
Expand Down
10 changes: 5 additions & 5 deletions src/utils/docker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setTimeout } from 'node:timers/promises';
import is from '@sindresorhus/is';
import { isNonEmptyArray, isString } from '@sindresorhus/is';
import chalk from 'chalk';
import { dockerBuildx } from './docker/common';
import log from './logger';
Expand Down Expand Up @@ -46,7 +46,7 @@ export async function build({
}: BuildOptions): Promise<void> {
const args = ['build'];

if (is.nonEmptyArray(buildArgs)) {
if (isNonEmptyArray(buildArgs)) {
args.push(...buildArgs.map((b) => `--build-arg=${b}`));
}

Expand All @@ -61,14 +61,14 @@ export async function build({
}
}

if (is.string(cache)) {
if (isString(cache)) {
const cachePrefix = cache.split('/')[0]?.match(/[.:]/)
? ''
: `${imagePrefix}/`;
const cacheImage = `${cachePrefix}${cache}:${image.replace(/\//g, '-')}`;
args.push(`--cache-from=${cacheImage}-${tag}`);

if (is.nonEmptyArray(cacheFromTags)) {
if (isNonEmptyArray(cacheFromTags)) {
for (const ctag of cacheFromTags) {
args.push(`--cache-from=${cacheImage}-${ctag}`);
}
Expand All @@ -78,7 +78,7 @@ export async function build({
args.push(
`--cache-to=type=registry,ref=${cacheImage}-${tag},mode=max,ignore-error=true`,
);
if (is.nonEmptyArray(cacheToTags)) {
if (isNonEmptyArray(cacheToTags)) {
for (const ctag of cacheToTags) {
args.push(
`--cache-to=type=registry,ref=${cacheImage}-${ctag},mode=max,ignore-error=true`,
Expand Down

0 comments on commit 7f29a02

Please sign in to comment.