Skip to content

Commit

Permalink
warn on 404 package delete calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanmars committed Dec 29, 2024
1 parent 123416d commit 8f34cb4
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ jobs:
- title: '7b - Single: Untagging All Images'
purpose: 'Untagging all multi tagged images (single arch)'
folder: '7b_single-untagging-all'
delete-tags: tag1,tag2,tag3
delete-tags: '*tag*'

- title: '7c - Multi: Untagging Images'
purpose: 'Untagging multi tagged images (multi arch)'
Expand Down
74 changes: 48 additions & 26 deletions citester/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39107,43 +39107,65 @@ class PackageRepo {
* @param label Additional label to display
*/
async deletePackageVersion(targetPackage, id, digest, tags, label) {
if (tags && tags.length > 0) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest} tag: ${tags}`);
}
else if (label) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest} ${label}`);
}
else {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest}`);
}
if (!this.config.dryRun) {
if (this.config.repoType === 'User') {
if (this.config.isPrivateRepo) {
await this.config.octokit.rest.packages.deletePackageVersionForAuthenticatedUser({
package_type: 'container',
package_name: targetPackage,
package_version_id: id
});
try {
if (tags && tags.length > 0) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest} tag: ${tags}`);
}
else if (label) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest} ${label}`);
}
else {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(` deleting package id: ${id} digest: ${digest}`);
}
if (!this.config.dryRun) {
if (this.config.repoType === 'User') {
if (this.config.isPrivateRepo) {
await this.config.octokit.rest.packages.deletePackageVersionForAuthenticatedUser({
package_type: 'container',
package_name: targetPackage,
package_version_id: id
});
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForUser({
package_type: 'container',
package_name: targetPackage,
username: this.config.owner,
package_version_id: id
});
}
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForUser({
await this.config.octokit.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
package_name: targetPackage,
username: this.config.owner,
org: this.config.owner,
package_version_id: id
});
}
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
package_name: targetPackage,
org: this.config.owner,
package_version_id: id
});
}
catch (error) {
let ignoreError = false;
if (error instanceof _octokit_request_error__WEBPACK_IMPORTED_MODULE_2__.RequestError) {
if (error.status) {
// ignore 404's, seen these after a 502 error. whereby the first delete causes a 502 but it really
// deleted the package version, the retry then tries again and gets a 404
if (error.status === 404) {
ignoreError = true;
_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`The package "${targetPackage}" version:${id} wasn't found while trying to delete it, something went wrong and ignoring this error.`);
}
}
}
if (!ignoreError) {
throw error;
}
}
}
/**
* Get list of the packages in the GitHub account
* @returns Array of package names
*/
async getPackageList() {
const packages = [];
let listFunc;
Expand Down
2 changes: 1 addition & 1 deletion citester/index.js.map

Large diffs are not rendered by default.

88 changes: 55 additions & 33 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38230,43 +38230,65 @@ class PackageRepo {
* @param label Additional label to display
*/
async deletePackageVersion(targetPackage, id, digest, tags, label) {
if (tags && tags.length > 0) {
core.info(` deleting package id: ${id} digest: ${digest} tag: ${tags}`);
}
else if (label) {
core.info(` deleting package id: ${id} digest: ${digest} ${label}`);
}
else {
core.info(` deleting package id: ${id} digest: ${digest}`);
}
if (!this.config.dryRun) {
if (this.config.repoType === 'User') {
if (this.config.isPrivateRepo) {
await this.config.octokit.rest.packages.deletePackageVersionForAuthenticatedUser({
package_type: 'container',
package_name: targetPackage,
package_version_id: id
});
try {
if (tags && tags.length > 0) {
core.info(` deleting package id: ${id} digest: ${digest} tag: ${tags}`);
}
else if (label) {
core.info(` deleting package id: ${id} digest: ${digest} ${label}`);
}
else {
core.info(` deleting package id: ${id} digest: ${digest}`);
}
if (!this.config.dryRun) {
if (this.config.repoType === 'User') {
if (this.config.isPrivateRepo) {
await this.config.octokit.rest.packages.deletePackageVersionForAuthenticatedUser({
package_type: 'container',
package_name: targetPackage,
package_version_id: id
});
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForUser({
package_type: 'container',
package_name: targetPackage,
username: this.config.owner,
package_version_id: id
});
}
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForUser({
await this.config.octokit.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
package_name: targetPackage,
username: this.config.owner,
org: this.config.owner,
package_version_id: id
});
}
}
else {
await this.config.octokit.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
package_name: targetPackage,
org: this.config.owner,
package_version_id: id
});
}
catch (error) {
let ignoreError = false;
if (error instanceof request_error_dist_node.RequestError) {
if (error.status) {
// ignore 404's, seen these after a 502 error. whereby the first delete causes a 502 but it really
// deleted the package version, the retry then tries again and gets a 404
if (error.status === 404) {
ignoreError = true;
core.warning(`The package "${targetPackage}" version:${id} wasn't found while trying to delete it, something went wrong and ignoring this error.`);
}
}
}
if (!ignoreError) {
throw error;
}
}
}
/**
* Get list of the packages in the GitHub account
* @returns Array of package names
*/
async getPackageList() {
const packages = [];
let listFunc;
Expand Down Expand Up @@ -44453,23 +44475,23 @@ class CleanupTask {
}
async deleteByTag() {
if (this.config.deleteTags) {
const matchTags = [];
const matchTags = new Set();
if (this.config.useRegex) {
const regex = new RegExp(this.config.deleteTags);
// build match list from filterSet
for (const digest of this.filterSet) {
const ghPackage = this.packageRepo.getPackageByDigest(digest);
for (const tag of ghPackage.metadata.container.tags) {
if (regex.test(tag)) {
matchTags.push(tag);
matchTags.add(tag);
}
}
}
// now check for digest based format matches - only check on filterset
for (const digest of this.filterSet) {
if (regex.test(digest)) {
// delete the tag from the filterSet
matchTags.push(digest);
matchTags.add(digest);
}
}
}
Expand All @@ -44481,18 +44503,18 @@ class CleanupTask {
const ghPackage = this.packageRepo.getPackageByDigest(digest);
for (const tag of ghPackage.metadata.container.tags) {
if (isTagMatch(tag)) {
matchTags.push(tag);
matchTags.add(tag);
}
}
}
// now check for digest based format matches
for (const digest of this.filterSet) {
if (isTagMatch(digest)) {
matchTags.push(digest);
matchTags.add(digest);
}
}
}
if (matchTags.length > 0) {
if (matchTags.size > 0) {
// build seperate sets for the untagging events and the standard deletions
const untaggingTags = new Set();
const standardTags = new Set();
Expand Down Expand Up @@ -44532,7 +44554,7 @@ class CleanupTask {
else {
core.info(`${tag}`);
// get the package
const manifest = await this.registry.getManifestByTag(tag);
const manifest = await this.registry.getManifestByDigest(manifestDigest);
// preform a "ghcr.io" image deletion
// as the registry doesn't support manifest deletion directly
// we instead assign the tag to a different manifest first
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions src/cleanup-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,23 +529,23 @@ export class CleanupTask {

async deleteByTag(): Promise<void> {
if (this.config.deleteTags) {
const matchTags = []
const matchTags = new Set<string>()
if (this.config.useRegex) {
const regex = new RegExp(this.config.deleteTags)
// build match list from filterSet
for (const digest of this.filterSet) {
const ghPackage = this.packageRepo.getPackageByDigest(digest)
for (const tag of ghPackage.metadata.container.tags) {
if (regex.test(tag)) {
matchTags.push(tag)
matchTags.add(tag)
}
}
}
// now check for digest based format matches - only check on filterset
for (const digest of this.filterSet) {
if (regex.test(digest)) {
// delete the tag from the filterSet
matchTags.push(digest)
matchTags.add(digest)
}
}
} else {
Expand All @@ -556,18 +556,18 @@ export class CleanupTask {
const ghPackage = this.packageRepo.getPackageByDigest(digest)
for (const tag of ghPackage.metadata.container.tags) {
if (isTagMatch(tag)) {
matchTags.push(tag)
matchTags.add(tag)
}
}
}
// now check for digest based format matches
for (const digest of this.filterSet) {
if (isTagMatch(digest)) {
matchTags.push(digest)
matchTags.add(digest)
}
}
}
if (matchTags.length > 0) {
if (matchTags.size > 0) {
// build seperate sets for the untagging events and the standard deletions
const untaggingTags = new Set<string>()
const standardTags = new Set<string>()
Expand Down Expand Up @@ -609,8 +609,10 @@ export class CleanupTask {
standardTags.add(tag)
} else {
core.info(`${tag}`)

// get the package
const manifest = await this.registry.getManifestByTag(tag)
const manifest =
await this.registry.getManifestByDigest(manifestDigest)

// preform a "ghcr.io" image deletion
// as the registry doesn't support manifest deletion directly
Expand Down
Loading

0 comments on commit 8f34cb4

Please sign in to comment.