Last active
August 19, 2022 23:05
-
-
Save Gerst20051/07ed55f37c8dfc0f74af2bcdde324960 to your computer and use it in GitHub Desktop.
GitLab GraphQL Console Script
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
(async () => { | |
const gitlabHost = window.location.host; | |
const gitlabUser = 'andrew'; | |
const workingDirPath = '~/Desktop'; | |
const snippetDirName = 'gitlab-snippets'; | |
const getSnippetInfoForIds = async ids => { | |
const rawResponse = await fetch(`https://${gitlabHost}/api/graphql`, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'), | |
}, | |
body: JSON.stringify({ | |
query: 'query GetSnippetQuery($ids:[SnippetID!]){snippets(ids:$ids){nodes{sshUrlToRepo author{username}}}}', | |
variables: { ids }, | |
}), | |
}); | |
const content = await rawResponse.json(); | |
return content.data.snippets.nodes; | |
}; | |
const getSnippetIds = start => Array(100).fill().map((_, i) => `gid://gitlab/PersonalSnippet/${i + start + 1}`); | |
const snippets = (await Promise.all([ | |
getSnippetInfoForIds(getSnippetIds(0)), | |
getSnippetInfoForIds(getSnippetIds(100)), | |
getSnippetInfoForIds(getSnippetIds(200)), | |
])).flat(); | |
const userSnippets = snippets.filter(snippet => snippet.author.username === gitlabUser); | |
const snippetIds = userSnippets.map(snippet => +snippet.sshUrlToRepo.split('/')[1].split('.')[0]).sort((a, b) => a - b).filter(Boolean); | |
const shellCommands = [ | |
`mkdir -p ${workingDirPath}/${snippetDirName}`, | |
'cd $_', | |
[ | |
`echo '${JSON.stringify(snippetIds)}'`, | |
`jq -r 'map("git@${gitlabHost}:snippets/\\(.).git") | .[]'`, | |
'while read repo; do { git clone $repo } done', | |
].join(' | '), | |
`cd ${workingDirPath}`, | |
`tar -czf ${snippetDirName}.tar.gz ${snippetDirName}`, | |
`rm -rf ${snippetDirName}`, | |
]; | |
console.log(`[$]> ${shellCommands.join(' && ')}`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment