Skip to content

Instantly share code, notes, and snippets.

@douglasduteil
Created October 18, 2019 09:43
Show Gist options
  • Save douglasduteil/16f634b240c749b2a8ee7c09cf829171 to your computer and use it in GitHub Desktop.
Save douglasduteil/16f634b240c749b2a8ee7c09cf829171 to your computer and use it in GitHub Desktop.
(async()=>{
const now = Date.now();
const lastPage = 57;
const repository = 31;
const delta = 1000 * 60 * 60 * 24 * 31;
const host = "https://gitlab.factory.social.gouv.fr"
const registry = "https://gitlab.factory.social.gouv.fr/SocialGouv/emjpm/registry"
// 1 mouth
const isTooOld = ({created_at})=>now - Number(new Date(created_at)) > delta
async function page(pageNumber) {
const response = await fetch(`${registry}/repository/${repository}/tags?format=json&page=${pageNumber}`,{credentials:"include",headers:{accept:"application/json, text/plain, */*","accept-language":"fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7","cache-control":"no-cache",pragma:"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-csrf-token":"wligQt5vW1wI2sk7GhskFSX7qYj5DbVCyqQKFvWSN2qsruC/t8Le1wztxqo0v/WWVIVCynIuslyuyTviMDU8kQ==","x-requested-with":"XMLHttpRequest"},referrer:"https://gitlab.factory.social.gouv.fr/SocialGouv/emjpm/container_registry",referrerPolicy:"origin-when-cross-origin",body:null,method:"GET",mode:"cors"});
const images = await response.json();
return images.filter(isTooOld)
}
async function removeImage(path) {
return fetch(path,{"credentials":"include","headers":{"accept":"application/json, text/plain, */*","accept-language":"fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7","cache-control":"no-cache","pragma":"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-csrf-token":"wligQt5vW1wI2sk7GhskFSX7qYj5DbVCyqQKFvWSN2qsruC/t8Le1wztxqo0v/WWVIVCynIuslyuyTviMDU8kQ==","x-requested-with":"XMLHttpRequest"},"referrer":"https://gitlab.factory.social.gouv.fr/SocialGouv/emjpm/container_registry","referrerPolicy":"origin-when-cross-origin","body":null,"method":"DELETE","mode":"cors"})
.catch(console.error)
}
//
Promise.all(
Array.from({
length: lastPage
}).map(async(_,index)=>{
const images = await page(index)
console.log(`Page ${index} : remove ${images.length} images`);
await Promise.all(
images.map( async ({location,destroy_path}) => {
await removeImage(`${host}${destroy_path}`);
console.log(`${location} removed`)
}))
})
).then(() =>
console.log("====== DONE ==========")
);
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment