I'm trying to list "snippets" but everything I've found so far only refers to listing repos.
In gitlab there's a submenu, "snippets", where we occasionally need to pull new code from. I'd like to obtain a list of all of the snippets, locate the most relevant with some scripting and clone it.
git ls-remote <URL>
isn't applicable here as that's only allowing me to list the repo, and the repo is essentially empty as it's not used.
Does anyone know the alternative command, or alternative method for ls-remote that'll allow me to list this snippets submenu?
I'm trying to list "snippets" but everything I've found so far only refers to listing repos.
In gitlab there's a submenu, "snippets", where we occasionally need to pull new code from. I'd like to obtain a list of all of the snippets, locate the most relevant with some scripting and clone it.
git ls-remote <URL>
isn't applicable here as that's only allowing me to list the repo, and the repo is essentially empty as it's not used.
Does anyone know the alternative command, or alternative method for ls-remote that'll allow me to list this snippets submenu?
Share Improve this question asked Mar 27 at 13:40 DanDan 1351 gold badge1 silver badge7 bronze badges 5- I believe this endpoint is what you're after? docs.gitlab/api/snippets/#list-all-snippets – Ionuț G. Stan Commented Mar 27 at 13:46
- Thankyou, I had already spotted the API pages but I'm trying to avoid having to parse the output and move away from simple git bash commands. Ultimately I may end up having to use this if there's no other options. – Dan Commented Mar 27 at 13:59
- 4 I see. Just to make sure, there's no Git-specific command for this. Snippets are a GitLab feature, so one way or another you'd still have to talk to their API. The concept of a snippet is not part of Git, even if GitLab exposes snippets as Git repositories. – Ionuț G. Stan Commented Mar 27 at 14:09
- Understood - in that case I'd better get parsing then. Thankyou for your time answering this – Dan Commented Mar 27 at 14:51
- There is at least one client for the GitLab API that can be used via commandline. – Ulrich Eckhardt Commented Mar 27 at 18:54
1 Answer
Reset to default 1Just for completeness, I'll leave here detailed steps on how to read the authenticated user's snippets using the GitLab REST API.
First, create a Personal Access Token. Bellow, I'll assume its value is stored in a shell variable called GITLAB_TOKEN
.
Then, you'll need curl
and jq
to drill into the
response. For example, here's how you can read just the title and the SSH clone
URL of the snippets belonging to the user:
curl https://gitlab/api/v4/snippets \
-sSL --fail-with-body \
-H "Authorization: Bearer $GITLAB_TOKEN" \
| jq -r '.[] | [.title, .ssh_url_to_repo] | @tsv'
The output would look something like this:
Test [email protected]:snippets/4829892.git
I've piped them through @tsv
to obtain an output that's more amenable to tools
like awk
or sed
if you need further processing.