Skip to content

Commit

Permalink
Delete whole package if last version will be deleted
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
tisoft authored and rubengees committed Oct 28, 2023
1 parent 92ef68f commit ac21fa8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ export async function executeAction(input: Input, queryStrategy: QueryStrategy,
await group("Deleting packages", async () => {
await Promise.all(
processedPackages
.flatMap((pkg) => pkg.versions.map((version) => ({ name: pkg.name, version })))
.map(async ({ name, version }) => {
info(`Deleting version ${version.names.join(", ")} of package ${name}`)
.flatMap((pkg) =>
pkg.versions.map((version) => ({ name: pkg.name, version, totalVersions: pkg.totalVersions }))
)
.map(async ({ name, version, totalVersions }) => {
if (totalVersions > 1) {
info(`Deleting version ${version.names.join(", ")} of package ${name}`)

if (!input.dryRun) {
await deleteStrategy.deletePackageVersion(input, name, version.id)
if (!input.dryRun) {
await deleteStrategy.deletePackageVersion(input, name, version.id)
}
} else {
info(`Deleting package ${name} since ${version.names.join(", ")} is the last version`)

if (!input.dryRun) {
await deleteStrategy.deletePackage(input, name)
}
}
})
)
Expand Down
8 changes: 8 additions & 0 deletions src/delete/strategies/organization.delete.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class OrganizationDeleteStrategy implements DeleteStrategy {
org: input.organization,
})
}

async deletePackage(input: RestInput, name: string): Promise<void> {
await this.octokit.rest.packages.deletePackageForOrg({
package_name: name,
package_type: input.type,
org: input.organization,
})
}
}
8 changes: 8 additions & 0 deletions src/delete/strategies/user.delete.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class UserDeleteStrategy implements DeleteStrategy {
username: input.user,
})
}

async deletePackage(input: RestInput, name: string): Promise<void> {
await this.octokit.rest.packages.deletePackageForUser({
package_name: name,
package_type: input.type,
username: input.user,
})
}
}
10 changes: 8 additions & 2 deletions src/process/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ type RestVersion = components["schemas"]["package-version"]

export function processPackages(input: Input, packages: Package[]): Package[] {
return packages
.map(({ name, versions }) => ({ name, versions: findVersionsToDelete(input, versions).slice(input.keep) }))
.map(({ name, versions, totalVersions }) => ({
name,
versions: findVersionsToDelete(input, versions).slice(input.keep),
totalVersions,
}))
.filter((it) => it.versions.length >= 1)
}

Expand All @@ -35,9 +39,11 @@ export function findVersionsToDelete(input: Input, versions: PackageVersion[]):
}

export function processResponse(name: string, response: RestResponse): Package {
const versions = response.data.map((version) => processVersion(version)).filter((it) => it.names.length >= 1)
return {
name,
versions: response.data.map((version) => processVersion(version)).filter((it) => it.names.length >= 1),
versions: versions,
totalVersions: versions.length,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type RestInput = Input & {
export type Package = {
name: string
versions: PackageVersion[]
totalVersions: number
}

export type PackageVersion = {
Expand All @@ -42,4 +43,5 @@ export interface QueryStrategy {

export interface DeleteStrategy {
deletePackageVersion(input: Input, name: string, id: string): Promise<void>
deletePackage(input: Input, name: string): Promise<void>
}
30 changes: 30 additions & 0 deletions test/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ const packages = [
{ id: "a", names: ["1.0.0"] },
{ id: "b", names: ["docker-base-layer"] },
],
totalVersions: 2,
},
]

const packages_single = [
{
name: "test",
versions: [{ id: "a", names: ["1.0.0"] }],
totalVersions: 1,
},
]

Expand Down Expand Up @@ -76,6 +85,27 @@ test("filters by version-pattern", async () => {
expect(deleteStrategy.deletePackageVersion).toHaveBeenNthCalledWith(1, input, "test", "a")
})

test("deletes whole package if last version", async () => {
const input: Input = {
names: ["test", "test2"],
versionPattern: /^\d+\.\d+\.\d+$/,
keep: 0,
token: "token",
dryRun: false,
user: "user",
organization: "",
type: PackageType.Npm,
}

const queryStrategy = mock<QueryStrategy>({ queryPackages: () => Promise.resolve(packages_single) })
const deleteStrategy = mock<DeleteStrategy>()

await executeAction(input, queryStrategy, deleteStrategy)

expect(deleteStrategy.deletePackage).toHaveBeenCalledTimes(1)
expect(deleteStrategy.deletePackage).toHaveBeenNthCalledWith(1, input, "test")
})

test("Does nothing when empty packages are returned", async () => {
const input: Input = {
names: ["test", "test2"],
Expand Down
18 changes: 18 additions & 0 deletions test/process/process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ test("filters correctly", () => {
{ id: "a", names: ["2"] },
{ id: "b", names: ["1"] },
],
totalVersions: 2,
},
{
name: "test2",
versions: [
{ id: "c", names: ["2"] },
{ id: "d", names: ["1"] },
],
totalVersions: 2,
},
]
)

expect(result).toHaveLength(2)
expect(result[0].name).toEqual("test")
expect(result[0].versions.map((it) => it.id)).toEqual(["a", "b"])
expect(result[0].totalVersions).toEqual(2)
expect(result[1].name).toEqual("test2")
expect(result[1].versions.map((it) => it.id)).toEqual(["c", "d"])
expect(result[1].totalVersions).toEqual(2)
})

test("filters based on semver", () => {
Expand All @@ -61,12 +65,14 @@ test("filters based on semver", () => {
{ id: "c", names: ["3.0.1-alpha01"] },
{ id: "d", names: ["v3.10.2"] },
],
totalVersions: 4,
},
]
)

expect(result[0].versions).toHaveLength(3)
expect(result[0].versions.map((it) => it.id)).toEqual(["a", "c", "d"])
expect(result[0].totalVersions).toEqual(4)
})

test("filters based on regex", () => {
Expand All @@ -88,22 +94,26 @@ test("filters based on regex", () => {
{ id: "a", names: ["2-test"] },
{ id: "b", names: ["1"] },
],
totalVersions: 2,
},
{
name: "test2",
versions: [
{ id: "c", names: ["2-test"] },
{ id: "d", names: ["1-test"] },
],
totalVersions: 2,
},
]
)

expect(result).toHaveLength(2)
expect(result[0].name).toEqual("test")
expect(result[0].versions.map((it) => it.id)).toEqual(["a"])
expect(result[0].totalVersions).toEqual(2)
expect(result[1].name).toEqual("test2")
expect(result[1].versions.map((it) => it.id)).toEqual(["c", "d"])
expect(result[1].totalVersions).toEqual(2)
})

test("respects keep", () => {
Expand All @@ -126,20 +136,23 @@ test("respects keep", () => {
{ id: "b", names: ["2"] },
{ id: "c", names: ["1"] },
],
totalVersions: 3,
},
{
name: "test2",
versions: [
{ id: "d", names: ["1"] },
{ id: "e", names: ["1"] },
],
totalVersions: 2,
},
]
)

expect(result).toHaveLength(1)
expect(result[0].name).toEqual("test")
expect(result[0].versions.map((it) => it.id)).toEqual(["c"])
expect(result[0].totalVersions).toEqual(3)
})

test("filters with multiple names", () => {
Expand All @@ -161,20 +174,23 @@ test("filters with multiple names", () => {
{ id: "a", names: ["2"] },
{ id: "b", names: ["1", "1-test"] },
],
totalVersions: 2,
},
{
name: "test2",
versions: [
{ id: "c", names: ["2"] },
{ id: "d", names: ["1"] },
],
totalVersions: 2,
},
]
)

expect(result).toHaveLength(1)
expect(result[0].name).toEqual("test")
expect(result[0].versions.map((it) => it.id)).toEqual(["b"])
expect(result[0].totalVersions).toEqual(2)
})

const containerTestResponse: RestEndpointMethodTypes["packages"]["getAllPackageVersionsForPackageOwnedByOrg"]["response"] =
Expand Down Expand Up @@ -314,6 +330,7 @@ test("process rest container response", () => {
names: ["1.0.0-RC1", "1.0.0-beta1"],
},
],
totalVersions: 3,
})
})

Expand All @@ -340,5 +357,6 @@ test("process rest npm response", () => {
names: ["1.0.0"],
},
],
totalVersions: 4,
})
})

0 comments on commit ac21fa8

Please sign in to comment.