Skip to content

Commit

Permalink
Merge branch 'fix/exit-code-1-on-error' into release/cmd-1.1.1-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
vCaisim committed Oct 2, 2024
2 parents b995083 + d08bd60 commit 21b1be9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 89 deletions.
79 changes: 38 additions & 41 deletions packages/cmd/src/services/cache/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,58 @@ import { retrieveCache } from '../../api';
import { PRESETS } from '../../commands/cache/options';
import { getCacheCommandConfig } from '../../config/cache';
import { getCI } from '../../env/ciProvider';
import { warnWithNoTrace } from '../../logger';
import { unzipBuffer } from './fs';
import { MetaFile, warn } from './lib';
import { MetaFile } from './lib';
import { download } from './network';
import { handlePostLastRunPreset, handlePreLastRunPreset } from './presets';

export async function handleGetCache() {
try {
const config = getCacheCommandConfig();
if (config.type !== 'GET_COMMAND_CONFIG' || !config.values) {
throw new Error('Config is missing!');
}

const { recordKey, id, preset, matrixIndex, matrixTotal } = config.values;
const outputDir = config.values.outputDir;
const config = getCacheCommandConfig();
if (config.type !== 'GET_COMMAND_CONFIG' || !config.values) {
throw new Error('Config is missing!');
}

const ci = getCI();
const { recordKey, id, preset, matrixIndex, matrixTotal } = config.values;
const outputDir = config.values.outputDir;

if (preset === PRESETS.lastRun) {
await handlePreLastRunPreset(config.values, ci);
}
const ci = getCI();

const result = await retrieveCache({
recordKey,
ci,
id,
config: {
matrixIndex,
matrixTotal,
},
});
if (preset === PRESETS.lastRun) {
await handlePreLastRunPreset(config.values, ci);
}

try {
await handleArchiveDownload({
readUrl: result.readUrl,
outputDir,
});
const result = await retrieveCache({
recordKey,
ci,
id,
config: {
matrixIndex,
matrixTotal,
},
});

const meta = await handleMetaDownload(result.metaReadUrl);
try {
await handleArchiveDownload({
readUrl: result.readUrl,
outputDir,
});

if (preset === PRESETS.lastRun) {
await handlePostLastRunPreset(config.values, ci, meta);
}
} catch (e) {
if (isAxiosError(e)) {
if (e.response?.status && e.response?.status < 500) {
warnWithNoTrace(`Cache with ID "${result.cacheId}" not found`);
return;
}
}
const meta = await handleMetaDownload(result.metaReadUrl);

throw e;
if (preset === PRESETS.lastRun) {
await handlePostLastRunPreset(config.values, ci, meta);
}
} catch (e) {
warn(e, 'Failed to obtain cache');
if (isAxiosError(e)) {
if (
e.response?.status &&
(e.response?.status === 403 || e.response?.status === 404)
) {
throw new Error(`Cache with ID "${result.cacheId}" not found`);
}
}

throw e;
}
}

Expand Down
92 changes: 44 additions & 48 deletions packages/cmd/src/services/cache/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,68 @@ import { PRESETS } from '../../commands/cache/options';
import { getCacheCommandConfig } from '../../config/cache';
import { getCI } from '../../env/ciProvider';
import { filterPaths, zipFilesToBuffer } from './fs';
import { createMeta, getLastRunFilePath, warn } from './lib';
import { createMeta, getLastRunFilePath } from './lib';
import {
ContentType,
getDefautUploadProgressHandler,
sendBuffer,
} from './network';

export async function handleSetCache() {
try {
const config = getCacheCommandConfig();
if (config.type !== 'SET_COMMAND_CONFIG' || !config.values) {
throw new Error('Config is missing!');
}
const config = getCacheCommandConfig();
if (config.type !== 'SET_COMMAND_CONFIG' || !config.values) {
throw new Error('Config is missing!');
}

const { recordKey, id, preset, pwOutputDir, matrixIndex, matrixTotal } =
config.values;
const { recordKey, id, preset, pwOutputDir, matrixIndex, matrixTotal } =
config.values;

const paths = config.values.path ? filterPaths(config.values.path) : [];
const paths = config.values.path ? filterPaths(config.values.path) : [];

const uploadPaths: string[] = [];
const uploadPaths: string[] = [];

if (paths && paths.length > 0) {
uploadPaths.push(...paths);
}
if (paths && paths.length > 0) {
uploadPaths.push(...paths);
}

const ci = getCI();
const ci = getCI();

if (preset === PRESETS.lastRun) {
const lastRunPath = getLastRunFilePath(pwOutputDir);
uploadPaths.push(lastRunPath);
}
if (preset === PRESETS.lastRun) {
const lastRunPath = getLastRunFilePath(pwOutputDir);
uploadPaths.push(lastRunPath);
}

if (uploadPaths.length === 0) {
throw new Error('No paths available to upload');
}
if (uploadPaths.length === 0) {
throw new Error('No paths available to upload');
}

const result = await createCache({
recordKey,
ci,
id,
config: {
matrixIndex,
matrixTotal,
},
});
const result = await createCache({
recordKey,
ci,
id,
config: {
matrixIndex,
matrixTotal,
},
});

await handleArchiveUpload({
archive: await zipFilesToBuffer(uploadPaths),
cacheId: result.cacheId,
uploadUrl: result.uploadUrl,
});
await handleArchiveUpload({
archive: await zipFilesToBuffer(uploadPaths),
cacheId: result.cacheId,
uploadUrl: result.uploadUrl,
});

await handleMetaUpload({
meta: createMeta({
cacheId: result.cacheId,
config: config.values,
ci,
orgId: result.orgId,
path: uploadPaths,
}),
await handleMetaUpload({
meta: createMeta({
cacheId: result.cacheId,
uploadUrl: result.metaUploadUrl,
});
} catch (e) {
warn(e, 'Failed to save cache');
}
config: config.values,
ci,
orgId: result.orgId,
path: uploadPaths,
}),
cacheId: result.cacheId,
uploadUrl: result.metaUploadUrl,
});
}

async function handleArchiveUpload({
Expand Down

0 comments on commit 21b1be9

Please sign in to comment.