2022-12-28 14:43:50 +00:00
|
|
|
/* eslint-disable i18n-text/no-en */
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2020-02-29 19:33:20 -06:00
|
|
|
import {Input} from './input'
|
2021-12-14 15:51:31 +00:00
|
|
|
import {EMPTY, Observable, of, throwError} from 'rxjs'
|
2022-12-29 14:43:48 +05:30
|
|
|
import {reduce} from 'rxjs/operators'
|
2022-12-28 14:43:50 +00:00
|
|
|
import {
|
|
|
|
|
deletePackageVersions,
|
|
|
|
|
getOldestVersions,
|
|
|
|
|
RestVersionInfo
|
|
|
|
|
} from './version'
|
2021-12-14 15:51:31 +00:00
|
|
|
import {concatMap, map, expand, tap} from 'rxjs/operators'
|
2020-02-29 19:33:20 -06:00
|
|
|
|
2022-12-29 14:43:48 +05:30
|
|
|
const RATE_LIMIT = 1
|
2021-12-22 19:09:58 +00:00
|
|
|
let totalCount = 0
|
2021-12-14 15:51:31 +00:00
|
|
|
|
|
|
|
|
export function getVersionIds(
|
|
|
|
|
owner: string,
|
|
|
|
|
packageName: string,
|
2022-12-28 14:43:50 +00:00
|
|
|
packageType: string,
|
2021-12-14 15:51:31 +00:00
|
|
|
numVersions: number,
|
2022-12-28 14:43:50 +00:00
|
|
|
page: number,
|
2021-12-14 15:51:31 +00:00
|
|
|
token: string
|
2022-12-28 14:43:50 +00:00
|
|
|
): Observable<RestVersionInfo[]> {
|
2021-12-14 15:51:31 +00:00
|
|
|
return getOldestVersions(
|
|
|
|
|
owner,
|
|
|
|
|
packageName,
|
2022-12-28 14:43:50 +00:00
|
|
|
packageType,
|
2021-12-14 15:51:31 +00:00
|
|
|
numVersions,
|
2022-12-28 14:43:50 +00:00
|
|
|
page,
|
2021-12-14 15:51:31 +00:00
|
|
|
token
|
|
|
|
|
).pipe(
|
|
|
|
|
expand(value =>
|
|
|
|
|
value.paginate
|
|
|
|
|
? getOldestVersions(
|
|
|
|
|
owner,
|
|
|
|
|
packageName,
|
2022-12-28 14:43:50 +00:00
|
|
|
packageType,
|
2021-12-14 15:51:31 +00:00
|
|
|
numVersions,
|
2022-12-28 14:43:50 +00:00
|
|
|
value.page,
|
2021-12-14 15:51:31 +00:00
|
|
|
token
|
|
|
|
|
)
|
|
|
|
|
: EMPTY
|
|
|
|
|
),
|
2022-12-29 14:43:48 +05:30
|
|
|
tap(value => (totalCount = totalCount + value.totalCount)),
|
|
|
|
|
reduce((acc, value) => acc.concat(value.versions), [] as RestVersionInfo[])
|
2021-12-14 15:51:31 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function finalIds(input: Input): Observable<string[]> {
|
2020-02-29 19:33:20 -06:00
|
|
|
if (input.packageVersionIds.length > 0) {
|
|
|
|
|
return of(input.packageVersionIds)
|
|
|
|
|
}
|
|
|
|
|
if (input.hasOldestVersionQueryInfo()) {
|
2021-12-14 15:51:31 +00:00
|
|
|
if (input.minVersionsToKeep < 0) {
|
2021-12-23 13:01:35 +00:00
|
|
|
// This code block is when num-old-versions-to-delete is specified.
|
|
|
|
|
// Setting input.numOldVersionsToDelete is set as minimum of input.numOldVersionsToDelete and RATE_LIMIT
|
2021-12-21 17:34:02 +00:00
|
|
|
input.numOldVersionsToDelete =
|
2021-12-21 18:52:10 +00:00
|
|
|
input.numOldVersionsToDelete < RATE_LIMIT
|
|
|
|
|
? input.numOldVersionsToDelete
|
|
|
|
|
: RATE_LIMIT
|
2021-12-14 15:51:31 +00:00
|
|
|
return getVersionIds(
|
|
|
|
|
input.owner,
|
|
|
|
|
input.packageName,
|
2022-12-28 14:43:50 +00:00
|
|
|
input.packageType,
|
2021-12-23 13:01:35 +00:00
|
|
|
RATE_LIMIT,
|
2022-12-28 14:43:50 +00:00
|
|
|
1,
|
2021-12-14 15:51:31 +00:00
|
|
|
input.token
|
|
|
|
|
).pipe(
|
2021-12-23 13:01:35 +00:00
|
|
|
// This code block executes on batches of 100 versions starting from oldest
|
2021-12-14 15:51:31 +00:00
|
|
|
map(value => {
|
2022-12-28 18:32:12 +00:00
|
|
|
console.log('If block')
|
|
|
|
|
console.log(`value: ${JSON.stringify(value)}`)
|
2022-12-29 14:43:48 +05:30
|
|
|
// we need to delete oldest versions first
|
|
|
|
|
value.sort((a, b) => {
|
|
|
|
|
return (
|
|
|
|
|
new Date(a.created_at).getTime() -
|
|
|
|
|
new Date(b.created_at).getTime()
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
console.log(`sorted value: ${JSON.stringify(value)}`)
|
2021-12-23 13:01:35 +00:00
|
|
|
/*
|
|
|
|
|
Here first filter out the versions that are to be ignored.
|
|
|
|
|
Then update input.numOldeVersionsToDelete to the no of versions deleted from the next 100 versions batch.
|
|
|
|
|
*/
|
2021-12-22 19:09:58 +00:00
|
|
|
value = value.filter(info => !input.ignoreVersions.test(info.version))
|
2021-12-14 15:51:31 +00:00
|
|
|
const temp = input.numOldVersionsToDelete
|
|
|
|
|
input.numOldVersionsToDelete =
|
|
|
|
|
input.numOldVersionsToDelete - value.length <= 0
|
|
|
|
|
? 0
|
|
|
|
|
: input.numOldVersionsToDelete - value.length
|
2022-12-28 14:43:50 +00:00
|
|
|
return value.map(info => info.id.toString()).slice(0, temp)
|
2021-12-14 15:51:31 +00:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
} else {
|
2021-12-23 13:01:35 +00:00
|
|
|
// This code block is when min-versions-to-keep is specified.
|
2021-12-14 15:51:31 +00:00
|
|
|
return getVersionIds(
|
|
|
|
|
input.owner,
|
|
|
|
|
input.packageName,
|
2022-12-28 14:43:50 +00:00
|
|
|
input.packageType,
|
2021-12-22 19:21:20 +00:00
|
|
|
RATE_LIMIT,
|
2022-12-28 14:43:50 +00:00
|
|
|
1,
|
2021-12-14 15:51:31 +00:00
|
|
|
input.token
|
|
|
|
|
).pipe(
|
2021-12-23 13:01:35 +00:00
|
|
|
// This code block executes on batches of 100 versions starting from oldest
|
2021-12-14 15:51:31 +00:00
|
|
|
map(value => {
|
2022-12-28 18:32:12 +00:00
|
|
|
console.log('Else block')
|
|
|
|
|
console.log(`value: ${JSON.stringify(value)}`)
|
2022-12-29 14:43:48 +05:30
|
|
|
// we need to delete oldest versions first
|
|
|
|
|
value.sort((a, b) => {
|
|
|
|
|
return (
|
|
|
|
|
new Date(a.created_at).getTime() -
|
|
|
|
|
new Date(b.created_at).getTime()
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
console.log(`sorted value: ${JSON.stringify(value)}`)
|
2021-12-23 13:01:35 +00:00
|
|
|
/*
|
|
|
|
|
Here totalCount is the total no of versions in the package.
|
|
|
|
|
First we update totalCount by removing no of ignored versions from it and also filter them out from value.
|
|
|
|
|
toDelete is the no of versions that need to be deleted and input.numDeleted is the total no of versions deleted before this batch.
|
|
|
|
|
We calculate this from total no of versions in the package, the min no of versions to keep and the no of versions we have deleted in earlier batch.
|
|
|
|
|
Then we update toDelete to not exceed the length of current batch of versions.
|
|
|
|
|
Now toDelete holds the no of versions to be deleted from the current batch of versions.
|
|
|
|
|
*/
|
2021-12-21 17:34:02 +00:00
|
|
|
totalCount =
|
2021-12-14 15:51:31 +00:00
|
|
|
totalCount -
|
2021-12-21 17:34:02 +00:00
|
|
|
value.filter(info => input.ignoreVersions.test(info.version)).length
|
2021-12-14 16:15:58 +00:00
|
|
|
value = value.filter(info => !input.ignoreVersions.test(info.version))
|
2021-12-21 17:34:02 +00:00
|
|
|
let toDelete = totalCount - input.minVersionsToKeep - input.numDeleted
|
|
|
|
|
toDelete = toDelete > value.length ? value.length : toDelete
|
2021-12-23 13:01:35 +00:00
|
|
|
//Checking here if we have any versions to delete and whether we are within the RATE_LIMIT.
|
2021-12-21 18:52:10 +00:00
|
|
|
if (toDelete > 0 && input.numDeleted < RATE_LIMIT) {
|
2021-12-23 13:01:35 +00:00
|
|
|
/*
|
|
|
|
|
Checking here if we can delete all the versions left in the current batch.
|
|
|
|
|
input.numDeleted + toDelete should not exceed RATE_LIMIT.
|
|
|
|
|
If it is exceeding we only delete the no of versions from this batch that are allowed within the RATE_LIMIT.
|
|
|
|
|
i.e. diff between RATE_LIMIT and versions deleted till now (input.numDeleted)
|
|
|
|
|
input.numDeleted is updated accordingly.
|
|
|
|
|
*/
|
2021-12-22 19:09:58 +00:00
|
|
|
if (input.numDeleted + toDelete > RATE_LIMIT) {
|
2021-12-21 18:52:10 +00:00
|
|
|
toDelete = RATE_LIMIT - input.numDeleted
|
|
|
|
|
input.numDeleted = RATE_LIMIT
|
2021-12-21 17:34:02 +00:00
|
|
|
} else {
|
|
|
|
|
input.numDeleted = input.numDeleted + toDelete
|
|
|
|
|
}
|
2022-12-28 14:43:50 +00:00
|
|
|
return value.map(info => info.id.toString()).slice(0, toDelete)
|
2021-12-14 15:51:31 +00:00
|
|
|
} else return []
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-02-29 19:33:20 -06:00
|
|
|
}
|
2021-12-14 15:57:21 +00:00
|
|
|
return throwError(
|
|
|
|
|
"Could not get packageVersionIds. Explicitly specify using the 'package-version-ids' input"
|
|
|
|
|
)
|
2020-02-29 19:33:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteVersions(input: Input): Observable<boolean> {
|
|
|
|
|
if (!input.token) {
|
|
|
|
|
return throwError('No token found')
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 12:34:03 +00:00
|
|
|
if (!input.checkInput()) {
|
2021-12-24 10:21:02 +00:00
|
|
|
return throwError('Invalid input combination')
|
2021-12-14 12:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 15:51:31 +00:00
|
|
|
if (input.numOldVersionsToDelete <= 0 && input.minVersionsToKeep < 0) {
|
2020-02-29 19:33:20 -06:00
|
|
|
console.log(
|
|
|
|
|
'Number of old versions to delete input is 0 or less, no versions will be deleted'
|
|
|
|
|
)
|
|
|
|
|
return of(true)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 08:54:07 +00:00
|
|
|
const result = finalIds(input)
|
|
|
|
|
|
2022-12-28 12:56:30 +00:00
|
|
|
return result.pipe(
|
|
|
|
|
concatMap(ids =>
|
|
|
|
|
deletePackageVersions(
|
|
|
|
|
ids,
|
|
|
|
|
input.owner,
|
|
|
|
|
input.packageName,
|
|
|
|
|
input.packageType,
|
|
|
|
|
input.token
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-02-29 19:33:20 -06:00
|
|
|
}
|