Created
February 17, 2020 18:57
-
-
Save PetarKirov/1dccbe8b4dfab9f30368adb715ea5873 to your computer and use it in GitHub Desktop.
List container registries of all GitLab projects of a given group
This file contains 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 axios = require('axios'); | |
(async () => { | |
const token = '<INSERT YOUR PERSONAL ACCESS TOKEN HERE>'; | |
const group = '<INSERT GITLAB GROUP NAME HERE>'; | |
console.log(await main(group, token)); | |
})(); | |
async function main(group, token) { | |
const projects = await getGitlabProjects(group, token); | |
const result = await Promise.all( | |
projects.map(async project => { | |
console.log(`Getting container registries for '${project.fullPath}'`); | |
return await getProjectContainerRegistries(project.id, token); | |
}) | |
); | |
return result.filter(registries => registries.length > 0); | |
} | |
async function getGitlabProjects(group, token) { | |
const query = | |
`{ | |
group(fullPath: "${group}") { | |
projects(includeSubgroups: true) { | |
nodes { | |
fullPath | |
id | |
} | |
} | |
} | |
}`; | |
const response = await axios({ | |
url: 'https://gitlab.com/api/graphql', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${token}`, | |
'Accept': 'application/json', | |
}, | |
data: JSON.stringify({ query }) | |
}); | |
return (await response.data).data.group.projects.nodes; | |
} | |
async function getProjectContainerRegistries(projectId, token) { | |
const parts = projectId.split('/'); | |
const id = parts[parts.length - 1]; | |
const response = await axios({ | |
url: `https://gitlab.com/api/v4/projects/${id}/registry/repositories`, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${token}`, | |
'Accept': 'application/json', | |
} | |
}); | |
return (await response).data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment