Compare commits
26 Commits
v2
...
ng-fix-deletion
| Author | SHA1 | Date | |
|---|---|---|---|
| eaa1f7c20f | |||
| ce1d329288 | |||
| 9dbafb83da | |||
| 64305dab94 | |||
| e95cd87648 | |||
| 7f4037254e | |||
| e521d9e753 | |||
| cc7196aa1d | |||
| 6ce7577c5e | |||
| 92bd8a3cd0 | |||
| 280935cb1a | |||
| 272ad859dc | |||
| c5e64443ea | |||
| 9b1e76ea45 | |||
| 37b7aca7eb | |||
| f87b4a0b81 | |||
| 6961202f36 | |||
| 4eab40be13 | |||
| 8701e46f41 | |||
| 565ec2459a | |||
| ec4518c513 | |||
| 33300d7916 | |||
| e048663cdf | |||
| 0e86ee6891 | |||
| a31a43afa6 | |||
| 56849b848a |
@@ -24,10 +24,10 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Set Node.js 12.x
|
||||
- name: Set Node.js 16.x
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 12.x
|
||||
node-version: 16.x
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
Generated
+1
-1
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: "@actions/core"
|
||||
version: 1.6.0
|
||||
version: 1.9.1
|
||||
type: npm
|
||||
summary: Actions core lib
|
||||
homepage: https://github.com/actions/toolkit/tree/master/packages/core
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Delete Package Versions
|
||||
|
||||
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.
|
||||
This action deletes versions of a package from [GitHub Packages](https://github.com/features/packages). This action will only delete a maximum of 99 versions in one run.
|
||||
|
||||
### What It Can Do
|
||||
|
||||
@@ -17,7 +17,7 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
# Usage
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
# Can be a single package version id, or a comma separated list of package version ids.
|
||||
# Defaults to an empty string.
|
||||
@@ -28,16 +28,14 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
# Required if deleting a version from a package hosted in a different repo than the one executing the workflow.
|
||||
owner:
|
||||
|
||||
# Repo hosting the package.
|
||||
# Defaults to the repo executing the workflow.
|
||||
# Required if deleting a version from a package hosted in a different repo than the one executing the workflow.
|
||||
repo:
|
||||
|
||||
# Name of the package.
|
||||
# Defaults to an empty string.
|
||||
# Required if `package-version-ids` input is not given.
|
||||
# Required
|
||||
package-name:
|
||||
|
||||
# Type of the package.
|
||||
# Required
|
||||
package-type:
|
||||
|
||||
# The number of old versions to delete starting from the oldest version.
|
||||
# Defaults to 1.
|
||||
num-old-versions-to-delete:
|
||||
@@ -70,7 +68,7 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
|
||||
# Valid Input Combinations
|
||||
|
||||
`owner`, `repo`, `package-name` and `token` can be used with the following combinations in a workflow -
|
||||
`owner`, `package-name`, `package-type` and `token` can be used with the following combinations in a workflow -
|
||||
|
||||
- `num-old-versions-to-delete`
|
||||
- `min-versions-to-keep`
|
||||
@@ -101,13 +99,14 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all pre-release package versions except latest 10 in the same repo as the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
min-versions-to-keep: 10
|
||||
delete-only-pre-release-versions: "true"
|
||||
```
|
||||
To delete all pre release versions except y latest pre-release package versions in a different repo than the workflow the __owner__, __repo__, __package-name__, __token__, __min-versions-to-keep__ and __delete-only-pre-release-versions__ inputs are required.
|
||||
To delete all pre release versions except y latest pre-release package versions in a different repo than the workflow the __owner__, __package-name__, __token__, __min-versions-to-keep__ and __delete-only-pre-release-versions__ inputs are required.
|
||||
|
||||
The [token][token] needs the delete packages and read packages scope. It is recommended [to store the token as a secret][secret]. In this example the [token][token] was stored as a secret named __GITHUB_PAT__.
|
||||
|
||||
@@ -116,11 +115,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all pre-release package versions except latest 10 in a different repo than the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.GITHUB_PAT }}
|
||||
min-versions-to-keep: 10
|
||||
delete-only-pre-release-versions: "true"
|
||||
@@ -137,14 +136,15 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all except latest 3 package versions excluding major versions as per semver in the same repo as the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-packae'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
min-versions-to-keep: 3
|
||||
ignore-versions: '^(0|[1-9]\\d*)\\.0\\.0$'
|
||||
```
|
||||
|
||||
To delete all except y latest versions while ignoring particular package versions in a different repo than the workflow the __owner__, __repo__, __package-name__, __token__, __min-versions-to-keep__ and __ignore-versions__ inputs are required.
|
||||
To delete all except y latest versions while ignoring particular package versions in a different repo than the workflow the __owner__, __package-name__, __token__, __min-versions-to-keep__ and __ignore-versions__ inputs are required.
|
||||
|
||||
The [token][token] needs the delete packages and read packages scope. It is recommended [to store the token as a secret][secret]. In this example the [token][token] was stored as a secret named __GITHUB_PAT__.
|
||||
|
||||
@@ -153,11 +153,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all except latest 3 package versions excluding major versions as per semver in a different repo than the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-packae'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.GITHUB_PAT }}
|
||||
min-versions-to-keep: 3
|
||||
ignore-versions: '^(0|[1-9]\\d*)\\.0\\.0$'
|
||||
@@ -176,14 +176,15 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete 3 oldest versions excluding major versions as per semver is the same repo as the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-packae'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
num-old-versions-to-delete: 3
|
||||
ignore-versions: '^(0|[1-9]\\d*)\\.0\\.0$'
|
||||
```
|
||||
|
||||
To delete oldest x number of versions while ignoring all the major package versions in a different repo than the workflow the __owner__, __repo__, __package-name__, __token__, __num-oldest-versions-to-delete__ and __ignore-versions__ inputs are required.
|
||||
To delete oldest x number of versions while ignoring all the major package versions in a different repo than the workflow the __owner__, __package-name__, __token__, __num-oldest-versions-to-delete__ and __ignore-versions__ inputs are required.
|
||||
|
||||
There is a possibility if the oldest x number of versions contain ignored package versions, actual package versions to get deleted will be less than x.
|
||||
|
||||
@@ -191,14 +192,14 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
|
||||
__Example__
|
||||
|
||||
Delete 3 oldest versions excluding major versions as per semver is a differernt repo than the workflow
|
||||
Delete 3 oldest versions excluding major versions as per semver is a different repo than the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-packae'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.PAT }}
|
||||
num-old-versions-to-delete: 3
|
||||
ignore-versions: '^(0|[1-9]\\d*)\\.0\\.0$'
|
||||
@@ -215,13 +216,14 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all except latest 2 versions of a package hosted in the same repo as the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
min-versions-to-keep: 2
|
||||
```
|
||||
|
||||
To delete all except y latest versions of a package hosted in a repo other than the workflow the __owner__, __repo__, __package-name__, __token__ and __min-versions-to-keep__ inputs are required.
|
||||
To delete all except y latest versions of a package hosted in a repo other than the workflow the __owner__, __package-name__, __token__ and __min-versions-to-keep__ inputs are required.
|
||||
|
||||
The [token][token] needs the delete packages and read packages scope. It is recommended [to store the token as a secret][secret]. In this example the [token][token] was stored as a secret named __GITHUB_PAT__.
|
||||
|
||||
@@ -230,11 +232,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete all except latest 2 versions of a package hosted in a repo other than the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.PAT }}
|
||||
min-versions-to-keep: 2
|
||||
```
|
||||
@@ -250,13 +252,14 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete the oldest 3 version of a package hosted in the same repo as the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
num-old-versions-to-delete: 3
|
||||
```
|
||||
|
||||
To delete the oldest x number of versions of a package hosted in a different repo than the workflow the __owner__, __repo__, __package-name__, __token__ and __num-old-versions-to-delete__ inputs are required.
|
||||
To delete the oldest x number of versions of a package hosted in a different repo than the workflow the __owner__, __package-name__, __token__ and __num-old-versions-to-delete__ inputs are required.
|
||||
|
||||
The [token][token] needs the delete packages and read packages scope. It is recommended [to store the token as a secret][secret]. In this example the [token][token] was stored as a secret named __GITHUB_PAT__.
|
||||
|
||||
@@ -265,11 +268,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
Delete the oldest 3 version of a package hosted in a different repo than the one executing the workflow
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
num-old-versions-to-delete: 3
|
||||
token: ${{ secrets.GITHUB_PAT }}
|
||||
```
|
||||
@@ -283,23 +286,24 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
```
|
||||
|
||||
To delete the oldest version of a package that is hosted in a different repo than the workflow the __owner__, __repo__, __package-name__, __token__ inputs are required.
|
||||
To delete the oldest version of a package that is hosted in a different repo than the workflow the __owner__, __package-name__, __token__ inputs are required.
|
||||
|
||||
The [token][token] needs the delete packages and read packages scope. It is recommended [to store the token as a secret][secret]. In this example the [token][token] was stored as a secret named __GITHUB_PAT__.
|
||||
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
owner: 'github'
|
||||
repo: 'packages'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.PAT }}
|
||||
```
|
||||
|
||||
@@ -314,9 +318,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-version-ids: 'MDE0OlBhY2thZ2VWZXJzaW9uOTcyMDY3'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
```
|
||||
|
||||
To delete a specific version of a package that is hosted in a different repo than the workflow the __package-version-ids__ and __token__ inputs are required.
|
||||
@@ -328,9 +334,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-version-ids: 'MDE0OlBhY2thZ2VWZXJzaW9uOTcyMDY3'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.PAT }}
|
||||
```
|
||||
|
||||
@@ -338,19 +346,21 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
|
||||
### Delete multiple specific versions of a package
|
||||
|
||||
To delete multiple specifc versions of a package that is hosted in the same repo as the workflow the __package-version-ids__ input is required.
|
||||
To delete multiple specific versions of a package that is hosted in the same repo as the workflow the __package-version-ids__ input is required.
|
||||
|
||||
The __package-version-ids__ input should be a comma separated string of package version ids. Package version ids can be retrieved via the [GitHub GraphQL API][api].
|
||||
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-version-ids: 'MDE0OlBhY2thZ2VWZXJzaW9uOTcyMDY3, MDE0OlBhY2thZ2VWZXJzaW9uOTcyMzQ5, MDE0OlBhY2thZ2VWZXJzaW9uOTcyMzUw'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
```
|
||||
|
||||
To delete multiple specifc versions of a package that is hosted in a repo other than the workflow the __package-version-ids__, __token__ inputs are required.
|
||||
To delete multiple specific versions of a package that is hosted in a repo other than the workflow the __package-version-ids__, __token__ inputs are required.
|
||||
|
||||
The __package-version-ids__ input should be a comma separated string of package version ids. Package version ids can be retrieved via the [GitHub GraphQL API][api].
|
||||
|
||||
@@ -359,9 +369,11 @@ This action deletes versions of a package from [GitHub Packages](https://github.
|
||||
__Example__
|
||||
|
||||
```yaml
|
||||
- uses: actions/delete-package-versions@v2
|
||||
- uses: actions/delete-package-versions@v3
|
||||
with:
|
||||
package-version-ids: 'MDE0OlBhY2thZ2VWZXJzaW9uOTcyMDY3, MDE0OlBhY2thZ2VWZXJzaW9uOTcyMzQ5, MDE0OlBhY2thZ2VWZXJzaW9uOTcyMzUw'
|
||||
package-name: 'test-package'
|
||||
package-type: 'npm'
|
||||
token: ${{ secrets.PAT }}
|
||||
```
|
||||
|
||||
@@ -372,3 +384,4 @@ The scripts and documentation in this project are released under the [MIT Licens
|
||||
[api]: https://developer.github.com/v4/previews/#github-packages
|
||||
[token]: https://help.github.com/en/packages/publishing-and-managing-packages/about-github-packages#about-tokens
|
||||
[secret]: https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
|
||||
|
||||
|
||||
@@ -89,7 +89,6 @@ describe.skip('index tests -- call graphql', () => {
|
||||
const defaultInput: InputParams = {
|
||||
packageVersionIds: [],
|
||||
owner: 'namratajha',
|
||||
repo: 'only-pkg',
|
||||
packageName: 'only-pkg',
|
||||
numOldVersionsToDelete: 1,
|
||||
minVersionsToKeep: -1,
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import {deletePackageVersion, deletePackageVersions} from '../../src/version'
|
||||
|
||||
const githubToken = process.env.GITHUB_TOKEN as string
|
||||
|
||||
describe.skip('delete tests', () => {
|
||||
it('deletePackageVersion', async () => {
|
||||
const response = await deletePackageVersion(
|
||||
'PV_lADOGReZt84AEI7FzgDSHEI',
|
||||
githubToken
|
||||
).toPromise()
|
||||
expect(response).toBe(true)
|
||||
})
|
||||
|
||||
it('deletePackageVersions', async () => {
|
||||
const response = await deletePackageVersions(
|
||||
[
|
||||
'PV_lADOGReZt84AEI7FzgDSHDs',
|
||||
'PV_lADOGReZt84AEI7FzgDSHDY',
|
||||
'PV_lADOGReZt84AEI7FzgDSHC8'
|
||||
],
|
||||
githubToken
|
||||
).toPromise()
|
||||
expect(response).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -1,68 +0,0 @@
|
||||
import {mockOldestQueryResponse} from './graphql.mock'
|
||||
import {
|
||||
getOldestVersions as _getOldestVersions,
|
||||
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(result => {
|
||||
expect(result.versions.length).toBe(numVersions)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('getOldestVersions -- fails for invalid repo', done => {
|
||||
getOldestVersions({repo: 'actions-testin'}).subscribe({
|
||||
error: err => {
|
||||
expect(err).toBeTruthy()
|
||||
done()
|
||||
},
|
||||
complete: async () => done.fail('no error thrown')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('get versions tests -- mock graphql', () => {
|
||||
it('getOldestVersions -- success', done => {
|
||||
const numVersions = 5
|
||||
mockOldestQueryResponse(numVersions)
|
||||
|
||||
getOldestVersions({numVersions}).subscribe(result => {
|
||||
expect(result.versions.length).toBe(numVersions)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
interface Params {
|
||||
owner?: string
|
||||
repo?: string
|
||||
packageName?: string
|
||||
numVersions?: number
|
||||
startCursor?: string
|
||||
token?: string
|
||||
}
|
||||
|
||||
const defaultParams = {
|
||||
owner: 'namratajha',
|
||||
repo: 'test-repo',
|
||||
packageName: 'test-repo',
|
||||
numVersions: 1,
|
||||
startCursor: '',
|
||||
token: process.env.GITHUB_TOKEN as string
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
+7
-3
@@ -24,8 +24,12 @@ inputs:
|
||||
package-name:
|
||||
description: >
|
||||
Name of the package containing the version to delete.
|
||||
Required if dynamically deleting oldest versions.
|
||||
required: false
|
||||
required: true
|
||||
|
||||
package-type:
|
||||
description: >
|
||||
Type of the package containing the version to delete.
|
||||
required: true
|
||||
|
||||
num-old-versions-to-delete:
|
||||
description: >
|
||||
@@ -67,7 +71,7 @@ inputs:
|
||||
default: ${{ github.token }}
|
||||
|
||||
runs:
|
||||
using: node12
|
||||
using: node16
|
||||
main: dist/index.js
|
||||
|
||||
branding:
|
||||
|
||||
Vendored
+6385
-2546
File diff suppressed because one or more lines are too long
Generated
+11605
-117
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -25,8 +25,9 @@
|
||||
"author": "YourNameOrOrganization",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.6.0",
|
||||
"@actions/core": "^1.9.1",
|
||||
"@actions/github": "^2.1.1",
|
||||
"@octokit/rest": "^19.0.5",
|
||||
"rxjs": "^6.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
+62
-86
@@ -1,6 +1,13 @@
|
||||
/* eslint-disable i18n-text/no-en */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import {Input} from './input'
|
||||
import {EMPTY, Observable, of, throwError} from 'rxjs'
|
||||
import {deletePackageVersions, getOldestVersions, VersionInfo} from './version'
|
||||
import {reduce} from 'rxjs/operators'
|
||||
import {
|
||||
deletePackageVersions,
|
||||
getOldestVersions,
|
||||
RestVersionInfo
|
||||
} from './version'
|
||||
import {concatMap, map, expand, tap} from 'rxjs/operators'
|
||||
|
||||
const RATE_LIMIT = 99
|
||||
@@ -8,36 +15,34 @@ let totalCount = 0
|
||||
|
||||
export function getVersionIds(
|
||||
owner: string,
|
||||
repo: string,
|
||||
packageName: string,
|
||||
packageType: string,
|
||||
numVersions: number,
|
||||
cursor: string,
|
||||
page: number,
|
||||
token: string
|
||||
): Observable<VersionInfo[]> {
|
||||
): Observable<RestVersionInfo[]> {
|
||||
return getOldestVersions(
|
||||
owner,
|
||||
repo,
|
||||
packageName,
|
||||
packageType,
|
||||
numVersions,
|
||||
cursor,
|
||||
page,
|
||||
token
|
||||
).pipe(
|
||||
expand(value =>
|
||||
value.paginate
|
||||
? getOldestVersions(
|
||||
owner,
|
||||
repo,
|
||||
packageName,
|
||||
packageType,
|
||||
numVersions,
|
||||
value.cursor,
|
||||
value.page + 1,
|
||||
token
|
||||
)
|
||||
: EMPTY
|
||||
),
|
||||
tap(
|
||||
value => (totalCount = totalCount === 0 ? value.totalCount : totalCount)
|
||||
),
|
||||
map(value => value.versions)
|
||||
tap(value => (totalCount = totalCount + value.totalCount)),
|
||||
reduce((acc, value) => acc.concat(value.versions), [] as RestVersionInfo[])
|
||||
)
|
||||
}
|
||||
|
||||
@@ -46,82 +51,43 @@ export function finalIds(input: Input): Observable<string[]> {
|
||||
return of(input.packageVersionIds)
|
||||
}
|
||||
if (input.hasOldestVersionQueryInfo()) {
|
||||
if (input.minVersionsToKeep < 0) {
|
||||
// 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
|
||||
input.numOldVersionsToDelete =
|
||||
input.numOldVersionsToDelete < RATE_LIMIT
|
||||
? input.numOldVersionsToDelete
|
||||
: RATE_LIMIT
|
||||
return getVersionIds(
|
||||
input.owner,
|
||||
input.repo,
|
||||
input.packageName,
|
||||
RATE_LIMIT,
|
||||
'',
|
||||
input.token
|
||||
).pipe(
|
||||
// This code block executes on batches of 100 versions starting from oldest
|
||||
map(value => {
|
||||
/*
|
||||
return getVersionIds(
|
||||
input.owner,
|
||||
input.packageName,
|
||||
input.packageType,
|
||||
RATE_LIMIT,
|
||||
1,
|
||||
input.token
|
||||
).pipe(
|
||||
// This code block executes on all versions of a package starting from oldest
|
||||
map(value => {
|
||||
// we need to delete oldest versions first
|
||||
value.sort((a, b) => {
|
||||
return (
|
||||
new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
|
||||
)
|
||||
})
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
value = value.filter(info => !input.ignoreVersions.test(info.version))
|
||||
const temp = input.numOldVersionsToDelete
|
||||
input.numOldVersionsToDelete =
|
||||
input.numOldVersionsToDelete - value.length <= 0
|
||||
? 0
|
||||
: input.numOldVersionsToDelete - value.length
|
||||
return value.map(info => info.id).slice(0, temp)
|
||||
})
|
||||
)
|
||||
} else {
|
||||
// This code block is when min-versions-to-keep is specified.
|
||||
return getVersionIds(
|
||||
input.owner,
|
||||
input.repo,
|
||||
input.packageName,
|
||||
RATE_LIMIT,
|
||||
'',
|
||||
input.token
|
||||
).pipe(
|
||||
// This code block executes on batches of 100 versions starting from oldest
|
||||
map(value => {
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
totalCount =
|
||||
totalCount -
|
||||
value.filter(info => input.ignoreVersions.test(info.version)).length
|
||||
value = value.filter(info => !input.ignoreVersions.test(info.version))
|
||||
let toDelete = totalCount - input.minVersionsToKeep - input.numDeleted
|
||||
toDelete = toDelete > value.length ? value.length : toDelete
|
||||
//Checking here if we have any versions to delete and whether we are within the RATE_LIMIT.
|
||||
if (toDelete > 0 && input.numDeleted < RATE_LIMIT) {
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
if (input.numDeleted + toDelete > RATE_LIMIT) {
|
||||
toDelete = RATE_LIMIT - input.numDeleted
|
||||
input.numDeleted = RATE_LIMIT
|
||||
} else {
|
||||
input.numDeleted = input.numDeleted + toDelete
|
||||
}
|
||||
return value.map(info => info.id).slice(0, toDelete)
|
||||
} else return []
|
||||
})
|
||||
)
|
||||
}
|
||||
value = value.filter(info => !input.ignoreVersions.test(info.version))
|
||||
let toDelete = 0
|
||||
if (input.minVersionsToKeep < 0) {
|
||||
toDelete = Math.min(
|
||||
value.length,
|
||||
Math.min(input.numOldVersionsToDelete, RATE_LIMIT)
|
||||
)
|
||||
} else {
|
||||
toDelete = Math.min(
|
||||
value.length - input.minVersionsToKeep,
|
||||
RATE_LIMIT
|
||||
)
|
||||
}
|
||||
if (toDelete < 0) return []
|
||||
return value.map(info => info.id.toString()).slice(0, toDelete)
|
||||
})
|
||||
)
|
||||
}
|
||||
return throwError(
|
||||
"Could not get packageVersionIds. Explicitly specify using the 'package-version-ids' input"
|
||||
@@ -146,5 +112,15 @@ export function deleteVersions(input: Input): Observable<boolean> {
|
||||
|
||||
const result = finalIds(input)
|
||||
|
||||
return result.pipe(concatMap(ids => deletePackageVersions(ids, input.token)))
|
||||
return result.pipe(
|
||||
concatMap(ids =>
|
||||
deletePackageVersions(
|
||||
ids,
|
||||
input.owner,
|
||||
input.packageName,
|
||||
input.packageType,
|
||||
input.token
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
+8
-5
@@ -1,8 +1,8 @@
|
||||
export interface InputParams {
|
||||
packageVersionIds?: string[]
|
||||
owner?: string
|
||||
repo?: string
|
||||
packageName?: string
|
||||
packageType?: string
|
||||
numOldVersionsToDelete?: number
|
||||
minVersionsToKeep?: number
|
||||
ignoreVersions?: RegExp
|
||||
@@ -13,8 +13,8 @@ export interface InputParams {
|
||||
const defaultParams = {
|
||||
packageVersionIds: [],
|
||||
owner: '',
|
||||
repo: '',
|
||||
packageName: '',
|
||||
packageType: '',
|
||||
numOldVersionsToDelete: 0,
|
||||
minVersionsToKeep: 0,
|
||||
ignoreVersions: new RegExp(''),
|
||||
@@ -25,8 +25,8 @@ const defaultParams = {
|
||||
export class Input {
|
||||
packageVersionIds: string[]
|
||||
owner: string
|
||||
repo: string
|
||||
packageName: string
|
||||
packageType: string
|
||||
numOldVersionsToDelete: number
|
||||
minVersionsToKeep: number
|
||||
ignoreVersions: RegExp
|
||||
@@ -39,8 +39,8 @@ export class Input {
|
||||
|
||||
this.packageVersionIds = validatedParams.packageVersionIds
|
||||
this.owner = validatedParams.owner
|
||||
this.repo = validatedParams.repo
|
||||
this.packageName = validatedParams.packageName
|
||||
this.packageType = validatedParams.packageType
|
||||
this.numOldVersionsToDelete = validatedParams.numOldVersionsToDelete
|
||||
this.minVersionsToKeep = validatedParams.minVersionsToKeep
|
||||
this.ignoreVersions = validatedParams.ignoreVersions
|
||||
@@ -52,7 +52,6 @@ export class Input {
|
||||
hasOldestVersionQueryInfo(): boolean {
|
||||
return !!(
|
||||
this.owner &&
|
||||
this.repo &&
|
||||
this.packageName &&
|
||||
this.numOldVersionsToDelete >= 0 &&
|
||||
this.token
|
||||
@@ -67,6 +66,10 @@ export class Input {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.packageType === '' || this.packageName === '') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.deletePreReleaseVersions === 'true') {
|
||||
this.minVersionsToKeep =
|
||||
this.minVersionsToKeep > 0 ? this.minVersionsToKeep : 0
|
||||
|
||||
+6
-2
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import {getInput, setFailed} from '@actions/core'
|
||||
import {context} from '@actions/github'
|
||||
import {Input} from './input'
|
||||
@@ -11,8 +12,8 @@ function getActionInput(): Input {
|
||||
? getInput('package-version-ids').split(',')
|
||||
: [],
|
||||
owner: getInput('owner') ? getInput('owner') : context.repo.owner,
|
||||
repo: getInput('repo') ? getInput('repo') : context.repo.repo,
|
||||
packageName: getInput('package-name'),
|
||||
packageType: getInput('package-type'),
|
||||
numOldVersionsToDelete: Number(getInput('num-old-versions-to-delete')),
|
||||
minVersionsToKeep: Number(getInput('min-versions-to-keep')),
|
||||
ignoreVersions: RegExp(getInput('ignore-versions')),
|
||||
@@ -29,7 +30,10 @@ function run(): Observable<boolean> {
|
||||
catchError(err => throwError(err))
|
||||
)
|
||||
} catch (error) {
|
||||
return throwError(error.message)
|
||||
if (error instanceof Error) {
|
||||
return throwError(error.message)
|
||||
}
|
||||
return throwError(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import * as github from '@actions/github'
|
||||
|
||||
// Centralize all Octokit references by re-exporting
|
||||
export {Octokit} from '@octokit/rest'
|
||||
|
||||
export interface OctokitOptions {
|
||||
baseUrl?: string
|
||||
userAgent?: string
|
||||
}
|
||||
|
||||
export function getOctokit(
|
||||
authToken: string,
|
||||
opts: OctokitOptions
|
||||
): github.GitHub {
|
||||
const options: OctokitOptions = {
|
||||
baseUrl: 'https://api.github.com'
|
||||
}
|
||||
|
||||
if (opts.userAgent) {
|
||||
options.userAgent = opts.userAgent
|
||||
}
|
||||
|
||||
return new github.GitHub(authToken, options)
|
||||
}
|
||||
@@ -1,50 +1,51 @@
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import {from, Observable, merge, throwError, of} from 'rxjs'
|
||||
import {catchError, map, tap} from 'rxjs/operators'
|
||||
import {graphql} from './graphql'
|
||||
import {Octokit} from '@octokit/rest'
|
||||
import {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types'
|
||||
|
||||
let deleted = 0
|
||||
|
||||
export interface DeletePackageVersionMutationResponse {
|
||||
deletePackageVersion: {
|
||||
success: boolean
|
||||
}
|
||||
}
|
||||
|
||||
const mutation = `
|
||||
mutation deletePackageVersion($packageVersionId: ID!) {
|
||||
deletePackageVersion(input: {packageVersionId: $packageVersionId}) {
|
||||
success
|
||||
}
|
||||
}`
|
||||
type PackageType = RestEndpointMethodTypes['packages']['getAllPackageVersionsForPackageOwnedByUser']['parameters']['package_type']
|
||||
|
||||
export function deletePackageVersion(
|
||||
packageVersionId: string,
|
||||
owner: string,
|
||||
packageName: string,
|
||||
packageType: string,
|
||||
token: string
|
||||
): Observable<boolean> {
|
||||
const octokit = new Octokit({
|
||||
auth: token
|
||||
})
|
||||
const package_version_id = +packageVersionId
|
||||
const package_type: PackageType = packageType as PackageType
|
||||
deleted += 1
|
||||
|
||||
return from(
|
||||
graphql(token, mutation, {
|
||||
packageVersionId,
|
||||
headers: {
|
||||
Accept: 'application/vnd.github.package-deletes-preview+json'
|
||||
}
|
||||
}) as Promise<DeletePackageVersionMutationResponse>
|
||||
octokit.rest.packages.deletePackageVersionForUser({
|
||||
package_type,
|
||||
package_name: packageName,
|
||||
username: owner,
|
||||
package_version_id
|
||||
})
|
||||
).pipe(
|
||||
catchError(err => {
|
||||
const msg = 'delete version mutation failed.'
|
||||
const msg = 'delete version API failed.'
|
||||
return throwError(
|
||||
err.errors && err.errors.length > 0
|
||||
? `${msg} ${err.errors[0].message}`
|
||||
: `${msg} ${err.message} \n${deleted - 1} versions deleted till now.`
|
||||
)
|
||||
}),
|
||||
map(response => response.deletePackageVersion.success)
|
||||
map(response => response.status === 204)
|
||||
)
|
||||
}
|
||||
|
||||
export function deletePackageVersions(
|
||||
packageVersionIds: string[],
|
||||
owner: string,
|
||||
packageName: string,
|
||||
packageType: string,
|
||||
token: string
|
||||
): Observable<boolean> {
|
||||
if (packageVersionIds.length === 0) {
|
||||
@@ -52,7 +53,7 @@ export function deletePackageVersions(
|
||||
}
|
||||
|
||||
const deletes = packageVersionIds.map(id =>
|
||||
deletePackageVersion(id, token).pipe(
|
||||
deletePackageVersion(id, owner, packageName, packageType, token).pipe(
|
||||
tap(result => {
|
||||
if (!result) {
|
||||
console.log(`version with id: ${id}, not deleted`)
|
||||
|
||||
+49
-173
@@ -1,193 +1,69 @@
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
import {GraphQlQueryResponse} from '@octokit/graphql/dist-types/types'
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
import {Observable, from, throwError} from 'rxjs'
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import {from, Observable, merge, throwError, of} from 'rxjs'
|
||||
import {catchError, map} from 'rxjs/operators'
|
||||
import {graphql} from './graphql'
|
||||
import {Octokit} from '@octokit/rest'
|
||||
import {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types'
|
||||
|
||||
export interface VersionInfo {
|
||||
id: string
|
||||
export interface RestVersionInfo {
|
||||
id: number
|
||||
version: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface QueryInfo {
|
||||
versions: VersionInfo[]
|
||||
cursor: string
|
||||
export interface RestQueryInfo {
|
||||
versions: RestVersionInfo[]
|
||||
page: number
|
||||
paginate: boolean
|
||||
totalCount: number
|
||||
}
|
||||
|
||||
export interface GetVersionsQueryResponse {
|
||||
repository: {
|
||||
packages: {
|
||||
edges: {
|
||||
node: {
|
||||
name: string
|
||||
versions: {
|
||||
totalCount: number
|
||||
edges: {node: VersionInfo}[]
|
||||
pageInfo: {
|
||||
startCursor: string
|
||||
hasPreviousPage: boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
}[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
query getVersions($owner: String!, $repo: String!, $package: String!, $last: Int!) {
|
||||
repository(owner: $owner, name: $repo) {
|
||||
packages(first: 1, names: [$package]) {
|
||||
edges {
|
||||
node {
|
||||
name
|
||||
versions(last: $last) {
|
||||
totalCount
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
version
|
||||
}
|
||||
}
|
||||
pageInfo {
|
||||
startCursor
|
||||
hasPreviousPage
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
const Paginatequery = `
|
||||
query getVersions($owner: String!, $repo: String!, $package: String!, $last: Int!, $before: String!) {
|
||||
repository(owner: $owner, name: $repo) {
|
||||
packages(first: 1, names: [$package]) {
|
||||
edges {
|
||||
node {
|
||||
name
|
||||
versions(last: $last, before: $before) {
|
||||
totalCount
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
version
|
||||
}
|
||||
}
|
||||
pageInfo{
|
||||
startCursor
|
||||
hasPreviousPage
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
export function queryForOldestVersions(
|
||||
owner: string,
|
||||
repo: string,
|
||||
packageName: string,
|
||||
numVersions: number,
|
||||
startCursor: string,
|
||||
token: string
|
||||
): Observable<GetVersionsQueryResponse> {
|
||||
if (startCursor === '') {
|
||||
return from(
|
||||
graphql(token, query, {
|
||||
owner,
|
||||
repo,
|
||||
package: packageName,
|
||||
last: numVersions,
|
||||
headers: {
|
||||
Accept: 'application/vnd.github.packages-preview+json'
|
||||
}
|
||||
}) as Promise<GetVersionsQueryResponse>
|
||||
).pipe(
|
||||
catchError((err: GraphQlQueryResponse<unknown>) => {
|
||||
const msg = 'query for oldest version failed.'
|
||||
return throwError(
|
||||
err.errors && err.errors.length > 0
|
||||
? `${msg} ${err.errors[0].message}`
|
||||
: `${msg} verify input parameters are correct`
|
||||
)
|
||||
})
|
||||
)
|
||||
} else {
|
||||
return from(
|
||||
graphql(token, Paginatequery, {
|
||||
owner,
|
||||
repo,
|
||||
package: packageName,
|
||||
last: numVersions,
|
||||
before: startCursor,
|
||||
headers: {
|
||||
Accept: 'application/vnd.github.packages-preview+json'
|
||||
}
|
||||
}) as Promise<GetVersionsQueryResponse>
|
||||
).pipe(
|
||||
catchError((err: GraphQlQueryResponse<unknown>) => {
|
||||
const msg = 'query for oldest version failed.'
|
||||
return throwError(
|
||||
err.errors && err.errors.length > 0
|
||||
? `${msg} ${err.errors[0].message}`
|
||||
: `${msg} verify input parameters are correct`
|
||||
)
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
type PackageType = RestEndpointMethodTypes['packages']['getAllPackageVersionsForPackageOwnedByUser']['parameters']['package_type']
|
||||
type GetVersionsResponse = RestEndpointMethodTypes['packages']['getAllPackageVersionsForPackageOwnedByUser']['response']['data']
|
||||
|
||||
export function getOldestVersions(
|
||||
owner: string,
|
||||
repo: string,
|
||||
packageName: string,
|
||||
packageType: string,
|
||||
numVersions: number,
|
||||
startCursor: string,
|
||||
page: number,
|
||||
token: string
|
||||
): Observable<QueryInfo> {
|
||||
return queryForOldestVersions(
|
||||
owner,
|
||||
repo,
|
||||
packageName,
|
||||
numVersions,
|
||||
startCursor,
|
||||
token
|
||||
): Observable<RestQueryInfo> {
|
||||
const octokit = new Octokit({
|
||||
auth: token
|
||||
})
|
||||
const package_type: PackageType = packageType as PackageType
|
||||
|
||||
return from(
|
||||
octokit.rest.packages.getAllPackageVersionsForPackageOwnedByUser({
|
||||
package_type,
|
||||
package_name: packageName,
|
||||
username: owner,
|
||||
per_page: numVersions,
|
||||
page
|
||||
})
|
||||
).pipe(
|
||||
map(result => {
|
||||
let r: QueryInfo
|
||||
if (result.repository.packages.edges.length < 1) {
|
||||
console.log(
|
||||
`package: ${packageName} not found for owner: ${owner} in repo: ${repo}`
|
||||
)
|
||||
r = {
|
||||
versions: [] as VersionInfo[],
|
||||
cursor: '',
|
||||
paginate: false,
|
||||
totalCount: 0
|
||||
}
|
||||
return r
|
||||
catchError(err => {
|
||||
const msg = 'get versions API failed.'
|
||||
return throwError(
|
||||
err.errors && err.errors.length > 0
|
||||
? `${msg} ${err.errors[0].message}`
|
||||
: `${msg} ${err.message}`
|
||||
)
|
||||
}),
|
||||
map(response => {
|
||||
const resp = {
|
||||
versions: response.data.map((version: GetVersionsResponse[0]) => {
|
||||
return {
|
||||
id: version.id,
|
||||
version: version.name,
|
||||
created_at: version.created_at
|
||||
}
|
||||
}),
|
||||
page,
|
||||
paginate: response.data.length === numVersions,
|
||||
totalCount: response.data.length
|
||||
}
|
||||
|
||||
const versions = result.repository.packages.edges[0].node.versions.edges
|
||||
const pages = result.repository.packages.edges[0].node.versions.pageInfo
|
||||
const count = result.repository.packages.edges[0].node.versions.totalCount
|
||||
|
||||
r = {
|
||||
versions: versions
|
||||
.map(value => ({id: value.node.id, version: value.node.version}))
|
||||
.reverse(),
|
||||
cursor: pages.startCursor,
|
||||
paginate: pages.hasPreviousPage,
|
||||
totalCount: count
|
||||
}
|
||||
|
||||
return r
|
||||
return resp
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user