Skip to content

Commit

Permalink
Merge pull request #52 from actions/keep-min-packages
Browse files Browse the repository at this point in the history
keep min package versions from deletion
  • Loading branch information
NamrataJha authored Nov 23, 2021
2 parents 0739f42 + 020712e commit b19f4fe
Show file tree
Hide file tree
Showing 12 changed files with 36,687 additions and 33,422 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ jobs:
name: Smoke Test Delete Package Versions Action
with:
package-name: 'com.github.actions.test-package'
num-old-versions-to-delete: 0
num-old-versions-to-delete: 0
min-versions-to-keep: 0
361 changes: 260 additions & 101 deletions README.md

Large diffs are not rendered by default.

46 changes: 27 additions & 19 deletions __tests__/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe.skip('index tests -- call graphql', () => {

getVersionIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(
ids => {
expect(ids.length).toBe(numVersions)
expect(ids.length).toBeLessThanOrEqual(numVersions)
done()
}
)
Expand All @@ -18,7 +18,7 @@ describe.skip('index tests -- call graphql', () => {

getVersionIds(getInput({numOldVersionsToDelete: numVersions})).subscribe(
ids => {
expect(ids.length).toBe(numVersions)
expect(ids.length).toBeLessThanOrEqual(numVersions)
done()
}
)
Expand Down Expand Up @@ -57,31 +57,39 @@ describe.skip('index tests -- call graphql', () => {
})
})

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

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

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

const defaultInput: InputParams = {
packageVersionIds: [],
owner: 'trent-j',
repo: 'actions-testing',
packageName: 'com.github.trent-j.actions-test',
owner: 'namratajha',
repo: 'only-pkg',
packageName: 'onlypkg.maven',
numOldVersionsToDelete: 1,
minVersionsToKeep: 1,
token: process.env.GITHUB_TOKEN as string
}

Expand Down
6 changes: 3 additions & 3 deletions __tests__/version/get-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ interface Params {
}

const defaultParams = {
owner: 'trent-j',
repo: 'actions-testing',
packageName: 'com.github.trent-j.actions-test',
owner: 'namratajha',
repo: 'only-pkg',
packageName: 'onlypkg.maven',
numVersions: 3,
token: process.env.GITHUB_TOKEN as string
}
Expand Down
22 changes: 22 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ inputs:
required: false
default: "1"

min-versions-to-keep:
description: >
Number of versions to keep starting with the latest version
Defaults to 0.
required: false
default: "0"

ignore-versions:
description: >
Regex pattern for package versions to ignore.
Defaults to delete all versions.
required: false
default: "^$"

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.
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
default: "false"

token:
description: >
Token with the necessary scopes to delete package versions.
Expand Down
69,606 changes: 36,316 additions & 33,290 deletions dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@types/jest": "^24.0.23",
"@types/node": "^12.7.12",
"@typescript-eslint/parser": "^2.8.0",
"@zeit/ncc": "^0.20.5",
"@vercel/ncc": "^0.31.1",
"eslint": "^5.16.0",
"eslint-plugin-github": "^2.0.0",
"eslint-plugin-jest": "^22.21.0",
Expand Down
25 changes: 23 additions & 2 deletions src/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ export function getVersionIds(input: Input): Observable<string[]> {
input.owner,
input.repo,
input.packageName,
input.numOldVersionsToDelete,
input.numOldVersionsToDelete + input.minVersionsToKeep,
input.token
).pipe(map(versionInfo => versionInfo.map(info => info.id)))
).pipe(
map(versionInfo => {
const numberVersionsToDelete =
versionInfo.length - input.minVersionsToKeep

if (input.minVersionsToKeep > 0) {
return numberVersionsToDelete <= 0
? []
: versionInfo
.filter(info => !input.ignoreVersions.test(info.version))
.map(info => info.id)
.slice(0, -input.minVersionsToKeep)
} else {
return numberVersionsToDelete <= 0
? []
: versionInfo
.filter(info => !input.ignoreVersions.test(info.version))
.map(info => info.id)
.slice(0, numberVersionsToDelete)
}
})
)
}

return throwError(
Expand Down
22 changes: 22 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export interface InputParams {
repo?: string
packageName?: string
numOldVersionsToDelete?: number
minVersionsToKeep?: number
ignoreVersions?: RegExp
token?: string
deletePreReleaseVersions?: string
}

const defaultParams = {
Expand All @@ -13,6 +16,9 @@ const defaultParams = {
repo: '',
packageName: '',
numOldVersionsToDelete: 0,
minVersionsToKeep: 0,
ignoreVersions: new RegExp(''),
deletePreReleaseVersions: '',
token: ''
}

Expand All @@ -22,6 +28,9 @@ export class Input {
repo: string
packageName: string
numOldVersionsToDelete: number
minVersionsToKeep: number
ignoreVersions: RegExp
deletePreReleaseVersions: string
token: string

constructor(params?: InputParams) {
Expand All @@ -32,7 +41,19 @@ export class Input {
this.repo = validatedParams.repo
this.packageName = validatedParams.packageName
this.numOldVersionsToDelete = validatedParams.numOldVersionsToDelete
this.minVersionsToKeep = validatedParams.minVersionsToKeep
this.ignoreVersions = validatedParams.ignoreVersions
this.deletePreReleaseVersions = validatedParams.deletePreReleaseVersions
this.token = validatedParams.token

if (this.minVersionsToKeep > 0) {
this.numOldVersionsToDelete = 100 - this.minVersionsToKeep
}

if (this.deletePreReleaseVersions == 'true') {
this.numOldVersionsToDelete = 100 - this.minVersionsToKeep
this.ignoreVersions = new RegExp('^(0|[1-9]\\d*)((\\.(0|[1-9]\\d*))*)$')
}
}

hasOldestVersionQueryInfo(): boolean {
Expand All @@ -41,6 +62,7 @@ export class Input {
this.repo &&
this.packageName &&
this.numOldVersionsToDelete > 0 &&
this.minVersionsToKeep >= 0 &&
this.token
)
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function getActionInput(): Input {
repo: getInput('repo') ? getInput('repo') : context.repo.repo,
packageName: getInput('package-name'),
numOldVersionsToDelete: Number(getInput('num-old-versions-to-delete')),
minVersionsToKeep: Number(getInput('min-versions-to-keep')),
ignoreVersions: RegExp(getInput('ignore-versions')),
deletePreReleaseVersions: getInput(
'delete-only-pre-release-versions'
).toLowerCase(),
token: getInput('token')
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/version/get-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ export function getOldestVersions(
).pipe(
map(result => {
if (result.repository.packages.edges.length < 1) {
throwError(
console.log(
`package: ${packageName} not found for owner: ${owner} in repo: ${repo}`
)
return []
}

const versions = result.repository.packages.edges[0].node.versions.edges
Expand Down

0 comments on commit b19f4fe

Please sign in to comment.