Created
September 14, 2020 13:48
-
-
Save devoto13/e9f28bba17852bcee013ce20f3aa503f to your computer and use it in GitHub Desktop.
Cleanup all job artifacts from GitLab
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
const fetch = require("node-fetch"); | |
const domain = "YOUR-GITLAB-DOMAIN"; | |
const token = "YOUR-TOKEN"; | |
const forEachPage = async (url, callback) => { | |
const res = await fetch(url, { headers: { "PRIVATE-TOKEN": token } }); | |
const data = await res.json(); | |
const total = res.headers.get("x-total"); | |
for (let k = 0; k < data.length; k++) { | |
await callback(data[k], k + 1, total); | |
} | |
const totalPages = res.headers.get("x-total-pages"); | |
for (let i = 2; i <= totalPages; i++) { | |
const res = await fetch(url + `?page=${i}`, { | |
headers: { "PRIVATE-TOKEN": token }, | |
}); | |
const data = await res.json(); | |
for (let k = 0; k < data.length; k++) { | |
await callback(data[k], (i - 1) * 20 + k + 1, total); | |
} | |
} | |
}; | |
const run = async () => { | |
await forEachPage( | |
`https://${domain}/api/v4/projects`, | |
async (project, i, total) => { | |
console.log( | |
`Processing ${project.name_with_namespace} project (${i}/${total}).` | |
); | |
await forEachPage( | |
`https://${domain}/api/v4/projects/${project.id}/jobs`, | |
async (job, i, total) => { | |
if (job.artifacts.length > 0) { | |
await fetch( | |
`https://${domain}/api/v4/projects/${project.id}/jobs/${job.id}/artifacts`, | |
{ method: "DELETE", headers: { "PRIVATE-TOKEN": token } } | |
); | |
} | |
console.log(` Processing ${job.id} job (${i}/${total}).`); | |
} | |
); | |
} | |
); | |
}; | |
run().catch((err) => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment