最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Using a bash terminal to list gitlab "snippets" - Stack Overflow

programmeradmin6浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Just 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.

发布评论

评论列表(0)

  1. 暂无评论