我正在使用 開發一個專案,Nodejs根據我想files在subfolder使用GitLab repository .GitLab API
該子檔案夾包含許多檔案,因此我想列出所有檔案名,然后逐個遍歷檔案以讀取其內容。
我無法列出子檔案夾中存在的所有檔案。但是,我可以通過提供特定檔案名來讀取特定檔案的內容。
以下是我在 GitLab 存盤庫中的檔案夾結構:
projectName
- .FirstSubFolder
- SecondSubFolder
- ThirdSubFolder
以下是我正在嘗試的 API,我想讀取其中的所有檔案ThirdSubFolder:
https://{gitlabURL}/api/v4/projects/{id}/repository/files/.FirstSubFolder/SecondSubFolder/ThirdSubFolder?ref=master&private_token={api}
這讓我回想起:
{
"message": "404 File Not Found"
}
但是,如果我嘗試讀取其中的特定檔案,ThirdSubFolder則可以獲取內容:
https://{gitlabURL}/api/v4/projects/{id}/repository/files/.FirstSubFolder/SecondSubFolder/ThirdSubFolder/myFile.feature/raw?ref=master&private_token={api}
有人可以幫助我如何使用 API 讀取 GitLab 存盤庫的子檔案夾中存在的所有檔案嗎?
uj5u.com熱心網友回復:
我正在使用 Nodejs 開發一個專案,根據要求,我想使用 GitLab API 列出 GitLab 存盤庫的子檔案夾中存在的檔案。
您可以使用 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:/:/:')
curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/files/$f_path/raw?ref=master"
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426462.html
標籤:节点.js 休息 GitLab gitlab-api
