Skip to content

Instantly share code, notes, and snippets.

@windix
Created December 21, 2021 04:08
Show Gist options
  • Save windix/1e04664494ac23ac18d8bb6370d51f70 to your computer and use it in GitHub Desktop.
Save windix/1e04664494ac23ac18d8bb6370d51f70 to your computer and use it in GitHub Desktop.
Clean up github package by version
const { Octokit } = require('@octokit/rest')
const main = async () => {
const ORG = 'kmartau'
const PACKAGE_PREFIX = 'ko-cdk-'
// if VERSION_TO_CLEAN_UP=13, will delete any version <=13, e.g. '13.0.0', '13.1.1', '1.2.3' etc
const VERSION_TO_CLEAN_UP = 13
const octokit = new Octokit({
auth: 'UPDATE-WITH-PERSONAL-ACCESS-TOKEN'
})
try {
const packages = (await octokit.packages.listPackagesForOrganization({
package_type: 'npm',
org: ORG,
visibility: 'private',
per_page: 100
})).data.filter(({ name }) => name.startsWith(PACKAGE_PREFIX))
for await (const package of packages) {
const packageInfo = {
package_name: package.name,
package_type: 'npm',
org: ORG,
}
const result = await octokit.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
...packageInfo,
per_page: 100,
state: 'active'
})
await Promise.all(result.data
.filter(({ name }) => Number.parseInt(name.split('.')[0]) <= VERSION_TO_CLEAN_UP)
.map(async ({ id, name }) => {
console.log(`Deleting ${package.name} - ${name}`)
await octokit.rest.packages.deletePackageVersionForOrg({
...packageInfo,
package_version_id: id
})
})
)
}
} catch (e) {
console.error(e)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment