Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dry-run param #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
run: npm run build

- name: Compare the expected and actual dist/ directories
id: diff
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
Expand Down
44 changes: 43 additions & 1 deletion __tests__/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,47 @@ describe('index tests -- call rest', () => {
})
})

it('deleteVersions test - dryRun', done => {
const numVersions = 10
let getApiCalled = 0
let deleteApiCalled = 0

const versions = getMockedVersionsResponse(numVersions)
const versionsDeleted: string[] = []

server.use(
rest.get(
'https://api.github.com/users/test-owner/packages/npm/test-package/versions',
(req, res, ctx) => {
getApiCalled++
return res(ctx.status(200), ctx.json(versions))
}
)
)

server.use(
rest.delete(
'https://api.github.com/users/test-owner/packages/npm/test-package/versions/:versionId',
(req, res, ctx) => {
deleteApiCalled++
versionsDeleted.push(req.params.versionId as string)
return res(ctx.status(204))
}
)
)

deleteVersions(getInput({dryRun: true}))
.subscribe(result => {
expect(result).toBe(true)
})
.add(() => {
expect(getApiCalled).toBe(1)
expect(deleteApiCalled).toBe(0)
expect(versionsDeleted.length).toBe(0)
done()
})
})

it('deleteVersions test - success complete flow - GHES', done => {
process.env.GITHUB_API_URL = 'https://github.someghesinstance.com/api/v3'

Expand Down Expand Up @@ -477,7 +518,8 @@ const defaultInput: InputParams = {
numOldVersionsToDelete: RATE_LIMIT,
minVersionsToKeep: -1,
ignoreVersions: RegExp('^$'),
token: 'test-token'
token: 'test-token',
dryRun: false
}

function getInput(params?: InputParams): Input {
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ inputs:
By default this is set to false
required: false
default: "false"

dry-run:
description: >
If true, action deletes nothing and only logs the versions that would be deleted.
By default this is set to false
required: false
default: "false"

token:
description: >
Expand Down
20 changes: 16 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function deleteVersions(input) {
return (0, rxjs_1.of)(true);
}
const result = finalIds(input);
return result.pipe((0, operators_1.concatMap)(ids => (0, version_1.deletePackageVersions)(ids, input.owner, input.packageName, input.packageType, input.token)));
return result.pipe((0, operators_1.concatMap)(ids => (0, version_1.deletePackageVersions)(ids, input.owner, input.packageName, input.packageType, input.token, input.dryRun)));
}
exports.deleteVersions = deleteVersions;

Expand All @@ -95,7 +95,8 @@ const defaultParams = {
ignoreVersions: new RegExp(''),
deletePreReleaseVersions: '',
token: '',
deleteUntaggedVersions: ''
deleteUntaggedVersions: '',
dryRun: false
};
class Input {
constructor(params) {
Expand All @@ -111,6 +112,7 @@ class Input {
this.token = validatedParams.token;
this.numDeleted = 0;
this.deleteUntaggedVersions = validatedParams.deleteUntaggedVersions;
this.dryRun = validatedParams.dryRun;
}
hasOldestVersionQueryInfo() {
return !!(this.owner &&
Expand Down Expand Up @@ -183,14 +185,23 @@ function deletePackageVersion(packageVersionId, owner, packageName, packageType,
}), (0, operators_1.map)(response => response.status === 204));
}
exports.deletePackageVersion = deletePackageVersion;
function deletePackageVersions(packageVersionIds, owner, packageName, packageType, token) {
function deletePackageVersions(packageVersionIds, owner, packageName, packageType, token, dryRun = false) {
if (packageVersionIds.length === 0) {
return (0, rxjs_1.of)(true);
}
if (dryRun) {
for (const id of packageVersionIds) {
console.log(`version with id: ${id} to be deleted by setting dry-run: false`);
}
return (0, rxjs_1.of)(true);
}
const deletes = packageVersionIds.map(id => deletePackageVersion(id, owner, packageName, packageType, token).pipe((0, operators_1.tap)(result => {
if (!result) {
console.log(`version with id: ${id}, not deleted`);
}
else {
console.debug(`version with id: ${id} deleted`);
}
})));
console.log(`Total versions deleted till now: ${deleted}`);
return (0, rxjs_1.merge)(...deletes);
Expand Down Expand Up @@ -43941,7 +43952,8 @@ function getActionInput() {
ignoreVersions: RegExp((0, core_1.getInput)('ignore-versions')),
deletePreReleaseVersions: (0, core_1.getInput)('delete-only-pre-release-versions').toLowerCase(),
token: (0, core_1.getInput)('token'),
deleteUntaggedVersions: (0, core_1.getInput)('delete-only-untagged-versions').toLowerCase()
deleteUntaggedVersions: (0, core_1.getInput)('delete-only-untagged-versions').toLowerCase(),
dryRun: (0, core_1.getBooleanInput)('dry-run')
});
}
function run() {
Expand Down
3 changes: 2 additions & 1 deletion src/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export function deleteVersions(input: Input): Observable<boolean> {
input.owner,
input.packageName,
input.packageType,
input.token
input.token,
input.dryRun
)
)
)
Expand Down
6 changes: 5 additions & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface InputParams {
token?: string
deletePreReleaseVersions?: string
deleteUntaggedVersions?: string
dryRun?: boolean
}

const defaultParams = {
Expand All @@ -21,7 +22,8 @@ const defaultParams = {
ignoreVersions: new RegExp(''),
deletePreReleaseVersions: '',
token: '',
deleteUntaggedVersions: ''
deleteUntaggedVersions: '',
dryRun: false
}

export class Input {
Expand All @@ -36,6 +38,7 @@ export class Input {
token: string
numDeleted: number
deleteUntaggedVersions: string
dryRun: boolean

constructor(params?: InputParams) {
const validatedParams: Required<InputParams> = {...defaultParams, ...params}
Expand All @@ -51,6 +54,7 @@ export class Input {
this.token = validatedParams.token
this.numDeleted = 0
this.deleteUntaggedVersions = validatedParams.deleteUntaggedVersions
this.dryRun = validatedParams.dryRun
}

hasOldestVersionQueryInfo(): boolean {
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getInput, setFailed} from '@actions/core'
import {getBooleanInput, getInput, setFailed} from '@actions/core'
import {context} from '@actions/github'
import {Input} from './input'
import {Observable, throwError} from 'rxjs'
Expand All @@ -22,7 +22,8 @@ function getActionInput(): Input {
token: getInput('token'),
deleteUntaggedVersions: getInput(
'delete-only-untagged-versions'
).toLowerCase()
).toLowerCase(),
dryRun: getBooleanInput('dry-run')
})
}

Expand Down
13 changes: 12 additions & 1 deletion src/version/delete-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,28 @@ export function deletePackageVersions(
owner: string,
packageName: string,
packageType: string,
token: string
token: string,
dryRun = false
): Observable<boolean> {
if (packageVersionIds.length === 0) {
return of(true)
}
if (dryRun) {
for (const id of packageVersionIds) {
console.log(
`version with id: ${id} to be deleted by setting dry-run: false`
)
}
return of(true)
}

const deletes = packageVersionIds.map(id =>
deletePackageVersion(id, owner, packageName, packageType, token).pipe(
tap(result => {
if (!result) {
console.log(`version with id: ${id}, not deleted`)
} else {
console.debug(`version with id: ${id} deleted`)
}
})
)
Expand Down