Created
August 7, 2023 07:25
-
-
Save Elanis/cedb3d5cb9025b28e60302f7dbeec29b to your computer and use it in GitHub Desktop.
Github clean old package versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Cleans old container versions, keeps 5 most recent ones | |
| const GITHUB_TOKEN = ''; | |
| const ORGS = [ | |
| 'EXAMPLE' | |
| ]; | |
| const package_type = 'container'; | |
| for(const ORG of ORGS) { | |
| const packagesRes = await fetch(`https://api.github.com/orgs/${ORG}/packages?package_type=${package_type}&per_page=100`, { | |
| headers: { | |
| 'Authorization': 'Bearer ' + GITHUB_TOKEN | |
| } | |
| }); | |
| const packages = await packagesRes.json(); | |
| for(const packageObj of packages) { | |
| const packageVersionsRes = await fetch(`https://api.github.com/orgs/${ORG}/packages/${package_type}/${packageObj.name}/versions?per_page=100`, { | |
| headers: { | |
| 'Authorization': 'Bearer ' + GITHUB_TOKEN | |
| } | |
| }); | |
| const packageVersions = await packageVersionsRes.json(); | |
| packageVersions.sort((a, b) => (new Date(b.updated_at)).getTime() - (new Date(a.updated_at)).getTime()); | |
| for(let i = 5; i < packageVersions.length; i++) { | |
| const id = packageVersions[i].id; | |
| const from = packageVersions[i].updated_at; | |
| console.log(`DELETE ${ORG}/${package_type}/${packageObj.name}/${id} from ${from}`); | |
| await fetch(`https://api.github.com/orgs/${ORG}/packages/${package_type}/${packageObj.name}/versions/${id}`, { | |
| method: 'DELETE', | |
| headers: { | |
| 'Authorization': 'Bearer ' + GITHUB_TOKEN | |
| } | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment