GitLab API - 如何获取 repository/project 文件和元数据?
GitLab API - How to GET the repository/project files and metadata?
我是 GitLab 的新手,使用 API 调用,对如何调用以获取 repository/project 文件和元数据感到困惑。我目前的API调用如下:
https://gitlab.com/api/v3/projects?private_token=privateToken
上面一行末尾的 privateToken 被替换为我出于明显的安全原因而取出的私人令牌。
这将 return 对我来说 json 描述了我拥有的所有项目,但我想更深入地研究并查看有关存储在每个 project/repository。在 GitLab API 文档网站上,它列出了这个:
GET /projects/:id/repository/files/:file_path
但是,由于我是 GitLab 的新手并且通常 API 调用我很困惑如何编辑我的第一个 link 来检索此信息。
理想情况下,我希望能够深入到 python 中的 project/repository 文件和元数据,而不必编辑上面的第一个 link,但我不是确定这是否可能。 GitLab 如何 return json?作为散列 table 的散列 table,如果是这样,我该如何浏览它?
任何关于如何解析 json 并在其中进行更深入钻取的说明都将不胜感激!
我正在使用 Python 3.6.1。
谢谢!
您在评论中部分回答了您自己的问题(您可以post将其作为答案)。
至于:file_path
,看看API for repo files。
GET /projects/:id/repository/files/:file_path
卷曲:
curl --request GET --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' \
'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
你还没有给出你想要使用的file_path
,所以以gitlab-ce为例——文件"app/models/key.rb"
,被url编码为app%2Fmodels%2Fkey%2Erb
:
curl --request GET \
'https://gitlab.com/api/v3/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
project id
can be specified as the integer id
or the URL-encoded path。因此,而不是 13083
,项目命名空间 gitlab-org/gitlab-ce
url-encoded 可以用作:
curl --request GET \
'https://gitlab.com/api/v3/projects/gitlab-org%2Fgitlab-ce/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
您可能还想看看使用现有的 API,例如 pyapi-gitlab。
Python解决方案
关于 gitlab api 的非常有用的信息。
import gitlab
# private token or personal token authentication
gl = gitlab.Gitlab('https://gitlab.company.be', private_token='dklsfjksldjfkdsjf', api_version=4)
gl.auth()
project = gl.projects.get('path/to/project')
items = project.repository_tree()
print(items)
我是 GitLab 的新手,使用 API 调用,对如何调用以获取 repository/project 文件和元数据感到困惑。我目前的API调用如下:
https://gitlab.com/api/v3/projects?private_token=privateToken
上面一行末尾的 privateToken 被替换为我出于明显的安全原因而取出的私人令牌。
这将 return 对我来说 json 描述了我拥有的所有项目,但我想更深入地研究并查看有关存储在每个 project/repository。在 GitLab API 文档网站上,它列出了这个:
GET /projects/:id/repository/files/:file_path
但是,由于我是 GitLab 的新手并且通常 API 调用我很困惑如何编辑我的第一个 link 来检索此信息。
理想情况下,我希望能够深入到 python 中的 project/repository 文件和元数据,而不必编辑上面的第一个 link,但我不是确定这是否可能。 GitLab 如何 return json?作为散列 table 的散列 table,如果是这样,我该如何浏览它?
任何关于如何解析 json 并在其中进行更深入钻取的说明都将不胜感激!
我正在使用 Python 3.6.1。
谢谢!
您在评论中部分回答了您自己的问题(您可以post将其作为答案)。
至于:file_path
,看看API for repo files。
GET /projects/:id/repository/files/:file_path
卷曲:
curl --request GET --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' \ 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
你还没有给出你想要使用的file_path
,所以以gitlab-ce为例——文件"app/models/key.rb"
,被url编码为app%2Fmodels%2Fkey%2Erb
:
curl --request GET \
'https://gitlab.com/api/v3/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
project id
can be specified as the integer id
or the URL-encoded path。因此,而不是 13083
,项目命名空间 gitlab-org/gitlab-ce
url-encoded 可以用作:
curl --request GET \
'https://gitlab.com/api/v3/projects/gitlab-org%2Fgitlab-ce/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master'
您可能还想看看使用现有的 API,例如 pyapi-gitlab。
Python解决方案 关于 gitlab api 的非常有用的信息。
import gitlab
# private token or personal token authentication
gl = gitlab.Gitlab('https://gitlab.company.be', private_token='dklsfjksldjfkdsjf', api_version=4)
gl.auth()
project = gl.projects.get('path/to/project')
items = project.repository_tree()
print(items)