Skip to content

Commit

Permalink
Merge pull request #58 from actions/improve-existing-action
Browse files Browse the repository at this point in the history
Allow deletion beyond last 100 packages
  • Loading branch information
NamrataJha authored Dec 24, 2021
2 parents 8e78cd9 + 1979f24 commit 5aa567b
Show file tree
Hide file tree
Showing 12 changed files with 550 additions and 224 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Delete Package Versions

This action deletes versions of a package from [GitHub Packages](https://github.com/features/packages).
This action deletes versions of a package from [GitHub Packages](https://github.com/features/packages) except ghcr packages. This action will only delete a maximum of 99 versions in one run.

### What It Can Do

Expand All @@ -12,8 +12,6 @@ This action deletes versions of a package from [GitHub Packages](https://github.
* Delete version(s) of a package that is hosted in a different repo than the one executing the workflow
* Delete a single version
* Delete multiple versions


* Delete specific version(s)

# Usage
Expand Down Expand Up @@ -42,14 +40,12 @@ This action deletes versions of a package from [GitHub Packages](https://github.

# The number of old versions to delete starting from the oldest version.
# Defaults to 1.
# Cannot be more than 100.
num-old-versions-to-delete:

# The number of latest versions to not delete.
# Defaults to 0.
# When this is set greater than 0 it will delete all deletable package versions except the specified no.
# This takes precedence over `num-old-versions-to-delete`.
# Cannot be more than 100.
# The number of latest versions to keep.
# This cannot be specified with `num-old-versions-to-delete`. By default, `num-old-versions-to-delete` takes precedence over `min-versions-to-keep`.
# When set to 0, all deletable versions will be deleted.
# When set greater than 0, all deletable package versions except the specified number will be deleted.
min-versions-to-keep:

# The package versions to exclude from deletion.
Expand All @@ -61,6 +57,7 @@ This action deletes versions of a package from [GitHub Packages](https://github.
# The number of pre-release versions to keep can be set by using `min-versions-to-keep` value with this.
# When `min-versions-to-keep` is 0, all pre-release versions get deleted.
# Defaults to false.
# Cannot be used with `num-old-versions-to-delete` and `ignore-versions`.
delete-only-pre-release-versions:

# The token used to authenticate with GitHub Packages.
Expand All @@ -71,6 +68,18 @@ This action deletes versions of a package from [GitHub Packages](https://github.
token:
```
# Valid Input Combinations
`owner`, `repo`, `package-name` and `token` can be used with the following combinations in a workflow -

- `num-old-versions-to-delete`
- `min-versions-to-keep`
- `delete-only-pre-release-versions`
- `ignore-versions`
- `num-old-versions-to-delete` + `ignore-versions`
- `min-versions-to-keep` + `ignore-versions`
- `min-versions-to-keep` + `delete-only-pre-release-versions`

# Scenarios

- [Delete all pre-release versions except y latest pre-release package versions](#delete-all-pre-release-versions-except-y-latest-pre-release-package-versions)
Expand Down
74 changes: 39 additions & 35 deletions __tests__/delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import {Input, InputParams} from '../src/input'
import {deleteVersions, getVersionIds} from '../src/delete'
import {deleteVersions, finalIds} from '../src/delete'

describe.skip('index tests -- call graphql', () => {
it('getVersionIds test -- get oldest version', done => {
it('finalIds test -- get oldest version', done => {
const numVersions = 1

getVersionIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(
ids => {
expect(ids.length).toBeLessThanOrEqual(numVersions)
done()
}
)
finalIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(ids => {
expect(ids.length).toBe(numVersions)
done()
})
})

it('getVersionIds test -- get oldest 3 versions', done => {
it.skip('finalIds test -- get oldest 3 versions', done => {
const numVersions = 3
finalIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(ids => {
expect(ids.length).toBe(numVersions)
done()
})
})

getVersionIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(
ids => {
expect(ids.length).toBeLessThanOrEqual(numVersions)
done()
}
)
it.skip('finalIds test -- get oldest 110 versions', done => {
const numVersions = 110

finalIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(ids => {
expect(ids.length).toBe(99), async () => done()
})
})

it('getVersionIds test -- supplied package version id', done => {
it('finalIds test -- supplied package version id', done => {
const suppliedIds = [
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'
]

getVersionIds(getInput({packageVersionIds: suppliedIds})).subscribe(ids => {
finalIds(getInput({packageVersionIds: suppliedIds})).subscribe(ids => {
expect(ids).toBe(suppliedIds)
done()
})
Expand All @@ -58,38 +61,39 @@ describe.skip('index tests -- call graphql', () => {
})

it.skip('deleteVersions test -- delete oldest version', done => {
deleteVersions(
getInput({numOldVersionsToDelete: 2, minVersionsToKeep: 1})
).subscribe(isSuccess => {
expect(isSuccess).toBe(true)
done()
})
deleteVersions(getInput({numOldVersionsToDelete: 1})).subscribe(
isSuccess => {
expect(isSuccess)
},
async () => done()
)
})

it.skip('deleteVersions test -- delete 3 oldest versions', done => {
deleteVersions(
getInput({numOldVersionsToDelete: 3, minVersionsToKeep: 1})
).subscribe(isSuccess => {
expect(isSuccess).toBe(true)
done()
})
deleteVersions(getInput({numOldVersionsToDelete: 3})).subscribe(
isSuccess => {
expect(isSuccess)
},
async () => done()
)
})

it('deleteVersions test -- keep 5 versions', done => {
deleteVersions(getInput({minVersionsToKeep: 5})).subscribe(isSuccess => {
it.skip('deleteVersions test -- keep 5 versions', done => {
deleteVersions(getInput({minVersionsToKeep: 100})).subscribe(isSuccess => {
expect(isSuccess).toBe(true)
done()
})
}),
async () => done()
})
})

const defaultInput: InputParams = {
packageVersionIds: [],
owner: 'namratajha',
repo: 'only-pkg',
packageName: 'onlypkg.maven',
packageName: 'only-pkg',
numOldVersionsToDelete: 1,
minVersionsToKeep: 1,
minVersionsToKeep: -1,
ignoreVersions: RegExp('^$'),
token: process.env.GITHUB_TOKEN as string
}

Expand Down
8 changes: 4 additions & 4 deletions __tests__/version/delete-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const githubToken = process.env.GITHUB_TOKEN as string
describe.skip('delete tests', () => {
it('deletePackageVersion', async () => {
const response = await deletePackageVersion(
'MDE0OlBhY2thZ2VWZXJzaW9uNjg5OTU1',
'PV_lADOGReZt84AEI7FzgDSHEI',
githubToken
).toPromise()
expect(response).toBe(true)
Expand All @@ -14,9 +14,9 @@ describe.skip('delete tests', () => {
it('deletePackageVersions', async () => {
const response = await deletePackageVersions(
[
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4Mjc0',
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4Mjcx',
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4MjY3'
'PV_lADOGReZt84AEI7FzgDSHDs',
'PV_lADOGReZt84AEI7FzgDSHDY',
'PV_lADOGReZt84AEI7FzgDSHC8'
],
githubToken
).toPromise()
Expand Down
24 changes: 12 additions & 12 deletions __tests__/version/get-version.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import {mockOldestQueryResponse} from './graphql.mock'
import {
getOldestVersions as _getOldestVersions,
VersionInfo
QueryInfo
} from '../../src/version'
import {Observable} from 'rxjs'

describe.skip('get versions tests -- call graphql', () => {
it('getOldestVersions -- succeeds', done => {
const numVersions = 1

getOldestVersions({numVersions}).subscribe(versions => {
expect(versions.length).toBe(numVersions)
getOldestVersions({numVersions}).subscribe(result => {
expect(result.versions.length).toBe(numVersions)
done()
})
})
Expand All @@ -33,8 +30,8 @@ describe('get versions tests -- mock graphql', () => {
const numVersions = 5
mockOldestQueryResponse(numVersions)

getOldestVersions({numVersions}).subscribe(versions => {
expect(versions.length).toBe(numVersions)
getOldestVersions({numVersions}).subscribe(result => {
expect(result.versions.length).toBe(numVersions)
done()
})
})
Expand All @@ -45,24 +42,27 @@ interface Params {
repo?: string
packageName?: string
numVersions?: number
startCursor?: string
token?: string
}

const defaultParams = {
owner: 'namratajha',
repo: 'only-pkg',
packageName: 'onlypkg.maven',
numVersions: 3,
repo: 'test-repo',
packageName: 'test-repo',
numVersions: 1,
startCursor: '',
token: process.env.GITHUB_TOKEN as string
}

function getOldestVersions(params?: Params): Observable<VersionInfo[]> {
function getOldestVersions(params?: Params): Observable<QueryInfo> {
const p: Required<Params> = {...defaultParams, ...params}
return _getOldestVersions(
p.owner,
p.repo,
p.packageName,
p.numVersions,
p.startCursor,
p.token
)
}
22 changes: 14 additions & 8 deletions __tests__/version/graphql.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getMockedOldestQueryResponse(
numVersions: number
): GetVersionsQueryResponse {
const versions = []

numVersions = numVersions < 100 ? numVersions : numVersions
for (let i = 1; i <= numVersions; ++i) {
versions.push({
node: {
Expand All @@ -28,7 +28,12 @@ export function getMockedOldestQueryResponse(
node: {
name: 'test',
versions: {
edges: versions.reverse()
totalCount: 200,
edges: versions.reverse(),
pageInfo: {
startCursor: 'AAA',
hasPreviousPage: false
}
}
}
}
Expand All @@ -38,12 +43,13 @@ export function getMockedOldestQueryResponse(
}
}

export function mockOldestQueryResponse(
numVersions: number
) {
const response = new Promise((resolve) => {
export function mockOldestQueryResponse(numVersions: number): void {
const response = new Promise<GetVersionsQueryResponse>(resolve => {
resolve(getMockedOldestQueryResponse(numVersions))
}) as Promise<GraphQlQueryResponseData>
jest.spyOn(Graphql, 'graphql').mockImplementation(
(token: string, query: string, parameters: RequestParameters) => response)
jest
.spyOn(Graphql, 'graphql')
.mockImplementation(
(token: string, query: string, parameters: RequestParameters) => response
)
}
7 changes: 4 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ inputs:
min-versions-to-keep:
description: >
Number of versions to keep starting with the latest version
Defaults to 0.
By default keeps no version.
To delete all versions set this as 0.
required: false
default: "0"
default: "-1"

ignore-versions:
description: >
Expand All @@ -50,7 +51,7 @@ inputs:

delete-only-pre-release-versions:
description: >
Deletes only pre-release versions upto. The number of pre-release versions to keep can be specified by min-versions-to-keep.
Deletes only pre-release versions. The number of pre-release versions to keep can be specified by min-versions-to-keep.
When this is set num-old-versions-to-delete and ignore-versions will not be taken into account.
By default this is set to false
required: false
Expand Down
Loading

0 comments on commit 5aa567b

Please sign in to comment.