Skip to content

Instantly share code, notes, and snippets.

@razor-x
Created June 2, 2021 04:12
Show Gist options
  • Save razor-x/cabff4853d0538c6d3520f521eedc820 to your computer and use it in GitHub Desktop.
Save razor-x/cabff4853d0538c6d3520f521eedc820 to your computer and use it in GitHub Desktop.
Delete GitHub package versions
import { createHttpClient } from '@meltwater/mlabs-http'
export default ({ githubOrg, githubToken, log }) =>
async (packageName, packageType = 'container') => {
if (!packageName) throw new Error('Missing packageName')
const client = createPackagesClient({
packageName,
packageType,
org: githubOrg,
token: githubToken,
log
})
const versions = await getPackageVersions({ client, log })
const ciVersions = versions.filter(selectCiVersions)
await Promise.all(ciVersions.map(deleteVersion({ client, log })))
}
const getPackageVersions = ({ client }) => client.get('versions')
const deleteVersion =
({ client, log }) =>
async ({
name,
id,
metadata: {
container: { tags = [] }
}
}) => {
log.info({ id, tags }, 'delete')
await client.delete(`versions/${id}`)
}
const createPackagesClient = ({ packageName, packageType, org, token, log }) =>
createHttpClient({
origin: 'https://api.github.com',
path: `/orgs/${org}/packages/${packageType}/${packageName}`,
extend: {
headers: {
accept: 'application/vnd.github.v3+json',
authorization: `token ${token}`
}
}
})
const selectCiVersions = ({
metadata: {
container: { tags = [] }
}
}) => {
return tags.filter((tag) => tag.length !== 40).length === 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment