![](/img/trans.png)
[英]How to retrieve raw file content from a repository commit with gitlab api?
[英]How to get the raw content of a file through GitLab REST API?
以下GitLab API的REST Url為我提供了項目的存儲庫樹。
獲取回購樹(WORKS)
https://gitlab.gspt.net/api/v3/projects/2931/repository/tree?private_token=XXXX
輸出:
[
{
"id": "a49d11794ed56db7f935abfd61002aef67159d10",
"name": "src",
"type": "tree",
"path": "src",
"mode": "040000"
},
{
"id": "0fbd98527d4b36e3d22c164293d8fd8eee4d18cd",
"name": ".gitignore",
"type": "blob",
"path": ".gitignore",
"mode": "100644"
},
{
"id": "0ef0da472176f2e6a24843ac9d4bb738c8310d23",
"name": "pom.xml",
"type": "blob",
"path": "pom.xml",
"mode": "100644"
}
]
但是我無法獲得文件pom.xml的原始內容。
獲取文件的原始內容(無效-提供404)
https://gitlab.gspt.net/api/v3/projects/2931/repository/files/pom%2Exml/raw?private_token=xxxx&ref_name=master
輸出:
{
"error": "404 Not Found"
}
根據此處的文檔( https://docs.gitlab.com/ee/api/repository_files.html#get-raw-file-from-repository ),我指定了正確的休息網址。 但是唯一不同的是在其余api端點中使用V4而不是V3。 我四處搜尋,但找不到v3 API的端點。
首先,以防萬一,不要對“。”進行百分比編碼:
.../files/pom.xml/raw?...
^^
其次,您可以在合並請求9637中檢查文件端點從v3到v4的影響方式,並且可以進行比較
v3:
GET /projects/:id/repository/raw_blobs/:sha
v4:
GET /projects/:id/repository/blobs/:sha/raw
您可以看到示例(在v3中)未對點進行百分比編碼。
curl --request GET --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' \
'https://gitlab.example.com/api/v3/projects/13083/repository/files?file_path=app/models/key.rb&ref=master'
但是,v3 API僅允許獲取原始blob,而不是原始文件。
參見合並請求16834 :
- 修改
/projects/:id/repository/files
到/projects/:id/repository/files/:filepath
(:filepath
應該用URL編碼)- 將
/projects/:id/repository/blobs/:sha
到/projects/:id/repository/files/:filepath/raw
僅v4 API允許使用:filepath
參數。
請參閱“ Git對象SHA-1是文件內容還是文件名? ”以解碼從API v3獲得的原始Blob。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.