@actions/artifact
Version:
Actions artifact lib
78 lines • 4.16 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { info, debug } from '@actions/core';
import { getOctokit } from '@actions/github';
import { getUserAgentString } from '../shared/user-agent.js';
import { getRetryOptions } from '../find/retry-options.js';
import { defaults as defaultGitHubOptions } from '@actions/github/lib/utils';
import { requestLog } from '@octokit/plugin-request-log';
import { retry } from '@octokit/plugin-retry';
import { internalArtifactTwirpClient } from '../shared/artifact-twirp-client.js';
import { getBackendIdsFromToken } from '../shared/util.js';
import { StringValue } from '../../generated/index.js';
import { getArtifactPublic } from '../find/get-artifact.js';
import { ArtifactNotFoundError, InvalidResponseError } from '../shared/errors.js';
export function deleteArtifactPublic(artifactName, workflowRunId, repositoryOwner, repositoryName, token) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const [retryOpts, requestOpts] = getRetryOptions(defaultGitHubOptions);
const opts = {
log: undefined,
userAgent: getUserAgentString(),
previews: undefined,
retry: retryOpts,
request: requestOpts
};
const github = getOctokit(token, opts, retry, requestLog);
const getArtifactResp = yield getArtifactPublic(artifactName, workflowRunId, repositoryOwner, repositoryName, token);
const deleteArtifactResp = yield github.rest.actions.deleteArtifact({
owner: repositoryOwner,
repo: repositoryName,
artifact_id: getArtifactResp.artifact.id
});
if (deleteArtifactResp.status !== 204) {
throw new InvalidResponseError(`Invalid response from GitHub API: ${deleteArtifactResp.status} (${(_a = deleteArtifactResp === null || deleteArtifactResp === void 0 ? void 0 : deleteArtifactResp.headers) === null || _a === void 0 ? void 0 : _a['x-github-request-id']})`);
}
return {
id: getArtifactResp.artifact.id
};
});
}
export function deleteArtifactInternal(artifactName) {
return __awaiter(this, void 0, void 0, function* () {
const artifactClient = internalArtifactTwirpClient();
const { workflowRunBackendId, workflowJobRunBackendId } = getBackendIdsFromToken();
const listReq = {
workflowRunBackendId,
workflowJobRunBackendId,
nameFilter: StringValue.create({ value: artifactName })
};
const listRes = yield artifactClient.ListArtifacts(listReq);
if (listRes.artifacts.length === 0) {
throw new ArtifactNotFoundError(`Artifact not found for name: ${artifactName}`);
}
let artifact = listRes.artifacts[0];
if (listRes.artifacts.length > 1) {
artifact = listRes.artifacts.sort((a, b) => Number(b.databaseId) - Number(a.databaseId))[0];
debug(`More than one artifact found for a single name, returning newest (id: ${artifact.databaseId})`);
}
const req = {
workflowRunBackendId: artifact.workflowRunBackendId,
workflowJobRunBackendId: artifact.workflowJobRunBackendId,
name: artifact.name
};
const res = yield artifactClient.DeleteArtifact(req);
info(`Artifact '${artifactName}' (ID: ${res.artifactId}) deleted`);
return {
id: Number(res.artifactId)
};
});
}
//# sourceMappingURL=delete-artifact.js.map