如何使用 API 列出 GitLab 存储库子文件夹中存在的所有文件并对其进行递归以逐个读取它们?

How to list all the files present within GitLab repository subfolder using API and recurse over it to read them one by one?

我正在使用 Nodejs 开发一个项目,根据要求,我想列出 GitLab repository subfolder 中存在的 files 使用 GitLab API.

子文件夹中有很多文件,所以我想列出所有文件名,然后一个一个地遍历文件读取它们的内容。

我无法列出子文件夹中的所有文件。但是,我可以通过提供特定文件名来读取特定文件的内容。

以下是我在 GitLab 存储库中的文件夹结构:

projectName
  - .FirstSubFolder
    - SecondSubFolder
      - ThirdSubFolder

以下是我正在尝试的 API,我想读取 ThirdSubFolder 中的所有文件:

https://{gitlabURL}/api/v4/projects/{id}/repository/files/%2EFirstSubFolder%2FSecondSubFolder%2FThirdSubFolder?ref=master&private_token={api}

这个returns我:

{
    "message": "404 File Not Found"
}

但是,如果我尝试读取 ThirdSubFolder 内的特定文件,那么我可以获得内容:

https://{gitlabURL}/api/v4/projects/{id}/repository/files/%2EFirstSubFolder%2FSecondSubFolder%2FThirdSubFolder%2FmyFile.feature/raw?ref=master&private_token={api}

有人可以帮助我如何使用 API 读取 GitLab 存储库子文件夹中存在的所有文件吗?

I am developing a project using Nodejs, as per the requirement I would like to list the files present within the subfolder of the GitLab repository using the GitLab API.

您可以使用 Gitlab 存储库 API https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree

GET /projects/:id/repository/tree

结合API和curl我们可以得到某个子目录下的文件

curl -s  --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/tree?ref=<target_branch>&path=path/to/subdirectory" | jq -r .[].path 

示例输出为

path/to/file1
path/to/file2

最后,为了阅读他们的内容,我建议使用以下脚本

curl -s  --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/tree?ref=master&path=path/to/subdirectory" | jq -r .[].path |
    while read -r path
    do
            f_path=$(echo $path | sed 's:/:%2F:')
            curl -s  --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/files/$f_path/raw?ref=master"
    done