2

Question

My end goal is to have an offline, updateable copy of my snippets (including title & description) such that I can search and use them easily. How can I get all my snippets from Gitlab to my local machine? I'm using Gitlab version is 13.12.10-ee.

What I've looked into

Cloning snippets

It's possible to clone snippet contents in Gitlab, but this only includes the file associated with the snippet. The title and description are excluded.

E.g. when I do git clone git@company.gitlab.com:snippets/$snippet_id.git I only receive the files associated with the snippet, not the title and the description:

enter image description here

I've checked the documentation but could not find any mention of interacting with the description through git.

Gitlab API

I found that the Gitlab API has a snippets endpoint. However, when I use the python-gitlab CLI tool and request a single snippet with gitlab snippet get --id 123 I only get the ID and the title.
When I do gitlab snippet content --id 123 I only get the contents of the file associated with the snippet.

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51
  • Soooo `curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/snippets/123"` ? The documentation comes with an example. `could not find any mention of interacting with the description` It's right in the answer: `"description": "Ruby test snippet",`. – KamilCuk Oct 21 '21 at 13:02
  • Soooo, no I didn't use that command as I use the python-gitlab CLI tool to interface with the gitlab API. It does return the description indeed, unfortunately I need to postprocess it to extract the description. If you want you can make it an answer though, thanks! – Saaru Lindestøkke Oct 21 '21 at 14:01
  • it can do that for you, just `gitlab -f description`, but it's better to use some json parser anyway – KamilCuk Oct 21 '21 at 14:24
  • Thanks, I didn't understand the use of the `-f` parameter as it only works in combination with the json or yaml output. Feel free to post that as an answer. – Saaru Lindestøkke Oct 21 '21 at 14:35
  • Would be great the snippet content is just a by convention file like `README.md` – Setop Oct 09 '22 at 16:00

1 Answers1

1

From a Unix shell, using CuRL and jq:

GITLAB_API_TOKEN=<your_api-scoped_token>
SNIPPET_ID=<snippet_id_number>

# Parse the JSON for the fields you want (.title,.description)
curl --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \
  "https://gitlab.example.com/api/v4/snippets/$SNIPPET_ID" \
  | jq '.title,.description'

# Use the /raw endpoint to get the full snippet content
curl --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \
  "https://gitlab.example.com/api/v4/snippets/$SNIPPET_ID/raw

Using python-gitlab:

import gitlab
GITLAB_API_TOKEN=<your_api-scoped_token>
SNIPPET_ID=<snippet_id_number>

gl = gitlab.Gitlab('https://gitlab.example.com', private_token=GITLAB_API_TOKEN)
snippet = gl.snippets.get(SNIPPET_ID)
title = snippet.title
descr = snippet.description
cont  = snippet.content()
mike
  • 1,854
  • 17
  • 23