如何以编程方式访问 Gitlab 中的完整代码段?
How to programmatically access the full snippet in Gitlab?
问题
我的最终目标是为我的代码片段(包括标题和描述)创建一个离线的、可更新的副本,以便我可以轻松地搜索和使用它们。我怎样才能将我所有的代码片段从 Gitlab 获取到我的本地机器?
我用的Gitlab版本是13.12.10-ee
.
我调查过的内容
克隆片段
可以在 Gitlab 中 ,但这只包括与代码片段关联的文件。标题和描述被排除。
例如当我这样做时 git clone git@company.gitlab.com:snippets/$snippet_id.git
我只收到与片段相关的文件,而不是标题和描述:
我已经检查了 documentation 但找不到任何提及通过 git 与描述交互的内容。
GitlabAPI
我发现 Gitlab API 有一个 snippets endpoint. However, when I use the python-gitlab CLI tool 并请求一个片段 gitlab snippet get --id 123
我只得到 ID 和标题。
当我这样做 gitlab snippet content --id 123
时,我只得到与代码段关联的文件的内容。
从 Unix shell,使用 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
使用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()
问题
我的最终目标是为我的代码片段(包括标题和描述)创建一个离线的、可更新的副本,以便我可以轻松地搜索和使用它们。我怎样才能将我所有的代码片段从 Gitlab 获取到我的本地机器?
我用的Gitlab版本是13.12.10-ee
.
我调查过的内容
克隆片段
可以在 Gitlab 中
例如当我这样做时 git clone git@company.gitlab.com:snippets/$snippet_id.git
我只收到与片段相关的文件,而不是标题和描述:
我已经检查了 documentation 但找不到任何提及通过 git 与描述交互的内容。
GitlabAPI
我发现 Gitlab API 有一个 snippets endpoint. However, when I use the python-gitlab CLI tool 并请求一个片段 gitlab snippet get --id 123
我只得到 ID 和标题。
当我这样做 gitlab snippet content --id 123
时,我只得到与代码段关联的文件的内容。
从 Unix shell,使用 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
使用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()