Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ This action deletes versions of a package from [GitHub Packages](https://github.
# If `package-version-ids` is given the token only needs the delete packages scope.
# If `package-version-ids` is not given the token needs the delete packages scope and the read packages scope
token:

# Perform a dry-run: only print out actions to be performed without actually deleting anything.
# Defaults to false.
dry-run:
```

# Scenarios
Expand Down
10 changes: 9 additions & 1 deletion __tests__/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe.skip('index tests -- call graphql', () => {
}
)
})

it('deleteVersions test -- dry run', done => {
deleteVersions(getInput({dryRun: true})).subscribe(isSuccess => {
expect(isSuccess).toBe(true)
done()
})
})
})

const defaultInput: InputParams = {
Expand All @@ -82,7 +89,8 @@ const defaultInput: InputParams = {
repo: 'actions-testing',
packageName: 'com.github.trent-j.actions-test',
numOldVersionsToDelete: 1,
token: process.env.GITHUB_TOKEN as string
token: process.env.GITHUB_TOKEN as string,
dryRun: false
}

function getInput(params?: InputParams): Input {
Expand Down
22 changes: 22 additions & 0 deletions __tests__/version/delete-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ describe.skip('delete tests', () => {
expect(response).toBe(true)
})

it('deletePackageVersion (dry-run)', async () => {
const response = await deletePackageVersion(
'MDE0OlBhY2thZ2VWZXJzaW9uNjg5OTU1',
githubToken,
true
).toPromise()
expect(response).toBe(true)
})

it('deletePackageVersions', async () => {
const response = await deletePackageVersions(
[
Expand All @@ -22,4 +31,17 @@ describe.skip('delete tests', () => {
).toPromise()
expect(response).toBe(true)
})

it('deletePackageVersions', async () => {
const response = await deletePackageVersions(
[
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4Mjc0',
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4Mjcx',
'MDE0OlBhY2thZ2VWZXJzaW9uNjk4MjY3'
],
githubToken,
true
).toPromise()
expect(response).toBe(true)
})
})
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ inputs:
required: false
default: ${{ github.token }}

dry-run:
description: >
Only print the versions that will be deleted without actually deleting them.
required: false
default: "false"

runs:
using: node12
main: dist/index.js
Expand Down
2 changes: 1 addition & 1 deletion src/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export function deleteVersions(input: Input): Observable<boolean> {
}

return getVersionIds(input).pipe(
concatMap(ids => deletePackageVersions(ids, input.token))
concatMap(ids => deletePackageVersions(ids, input.token, input.dryRun))
)
}
6 changes: 5 additions & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface InputParams {
packageName?: string
numOldVersionsToDelete?: number
token?: string
dryRun?: boolean
}

const defaultParams = {
Expand All @@ -13,7 +14,8 @@ const defaultParams = {
repo: '',
packageName: '',
numOldVersionsToDelete: 0,
token: ''
token: '',
dryRun: false
}

export class Input {
Expand All @@ -23,6 +25,7 @@ export class Input {
packageName: string
numOldVersionsToDelete: number
token: string
dryRun: boolean

constructor(params?: InputParams) {
const validatedParams: Required<InputParams> = {...defaultParams, ...params}
Expand All @@ -33,6 +36,7 @@ export class Input {
this.packageName = validatedParams.packageName
this.numOldVersionsToDelete = validatedParams.numOldVersionsToDelete
this.token = validatedParams.token
this.dryRun = validatedParams.dryRun
}

hasOldestVersionQueryInfo(): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function getActionInput(): Input {
repo: getInput('repo') ? getInput('repo') : context.repo.repo,
packageName: getInput('package-name'),
numOldVersionsToDelete: Number(getInput('num-old-versions-to-delete')),
token: getInput('token')
token: getInput('token'),
dryRun: getInput('dry-run') === 'true'
})
}

Expand Down
19 changes: 14 additions & 5 deletions src/version/delete-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ const mutation = `

export function deletePackageVersion(
packageVersionId: string,
token: string
token: string,
dryRun = false
): Observable<boolean> {
if (dryRun) {
return of(true)
}
return from(
graphql(mutation, {
packageVersionId,
Expand All @@ -43,20 +47,25 @@ export function deletePackageVersion(

export function deletePackageVersions(
packageVersionIds: string[],
token: string
token: string,
dryRun = false
): Observable<boolean> {
if (packageVersionIds.length === 0) {
console.log('no package version ids found, no versions will be deleted')
return of(true)
}

const deletes = packageVersionIds.map(id =>
deletePackageVersion(id, token).pipe(
deletePackageVersion(id, token, dryRun).pipe(
tap(result => {
if (result) {
console.log(`version with id: ${id}, deleted`)
console.log(
`version with id: ${id}, deleted ${dryRun ? '(dry-run)' : ''}`
)
} else {
console.log(`version with id: ${id}, not deleted`)
console.log(
`version with id: ${id}, not deleted ${dryRun ? '(dry-run)' : ''}`
)
}
})
)
Expand Down