如何使用 GitLab API 访问源代码文件和列出目录? [英] How to access source code files and list directories using GitLab API?

查看:28
本文介绍了如何使用 GitLab API 访问源代码文件和列出目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GitLab,并按照 Gitlab 中的说明完成了设置安装说明,我现在正在尝试访问它的 RestFul API 来执行创建/删除项目或用户等任务(我能够成功地做到这一点).

我现在必须列出 master 分支(或项目拥有的任何其他分支)中的所有目录,并希望访问这些目录中列出的源代码文件(例如 myproject->master->api、src->somefile.java 等),是否可以使用现有的 Gitlab 的 API 做类似的事情?

here 提到了可以调用的 API 的完整列表,我正在研究 Project Snippets API 并使用此 API GET/projects/:id/snippets 列出所有片段,但它返回一个空数组,我的项目中有文件但结果为空.

我假设项目片段是指源代码文件或项目中的任何其他文件,如果我错了,请纠正我,我在这里感到困惑,因为 Gitlab API 的作者未能解释使用背后的术语片段"这个词.

如何使用 API 访问完整的目录结构并获取我需要的文件?

解决方案

我假设项目片段引用源代码文件或项目中的任何其他文件

不,您可以在 GitLab 演示项目中看到片段:diaspora 片段.
它们位于与文件"选项卡不同的选项卡中.
它们相当于 GitHub gist.

看看 repository API,例如:

列出存储库树

<块引用>

获取项目中存储库文件和目录的列表.

GET/projects/:id/repository/tree

原始 blob 内容

<块引用>

获取文件的原始文件内容.

GET/projects/:id/repository/commits/:sha/blob

astratto 提到新的 API (6.x) 要求附加一个文件路径:

GET/projects/:id/repository/blob/:sha?filepath=:filepath

I am using GitLab and I completed setup as described in Gitlab installation instructions, I am now trying to access its RestFul API to perform tasks like create/delete Projects or Users(I am able to do this successfully).

I now have to list out all the directories within the master branch (or any other branch the project has) and want to access source code files listed in those directories(e.g. myproject->master->api, src->somefile.java etc), is it possible to do something like that using the existing Gitlab's API?

The whole list of API's that can be called are mentioned here and I was looking into Project Snippets API and used this API GET /projects/:id/snippets to list all snippets but it returns an empty array, I do have files in my project but the result is empty.

I am assuming that project snippets refer to source code files or any other files within the project, please correct me if I am wrong, I am confused here as the authors of the Gitlab API failed to explain the terminology behind the usage of the word 'snippets'.

How can I access full directory structure and get files I need using the API?

解决方案

I am assuming that project snippets refer to source code files or any other files within the project

No, you can see snippets in the GitLab demo project: diaspora snippets.
They are in a separate tabs than the "Files" tab.
They would be the equivalent of GitHub gist.

Have a look at the repository API, like:

List repository tree

Get a list of repository files and directories in a project.

GET /projects/:id/repository/tree

Raw blob content

Get the raw file contents for a file.

GET /projects/:id/repository/commits/:sha/blob

astratto mentions that the new API (6.x) requires that a filepath is appended:

GET /projects/:id/repository/blobs/:sha?filepath=:filepath

这篇关于如何使用 GitLab API 访问源代码文件和列出目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆