Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Last active August 19, 2022 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gerst20051/07ed55f37c8dfc0f74af2bcdde324960 to your computer and use it in GitHub Desktop.
Save Gerst20051/07ed55f37c8dfc0f74af2bcdde324960 to your computer and use it in GitHub Desktop.
GitLab GraphQL Console Script
(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