2017-06-30 106 views
1

GitLab API的以下REST URL爲我提供了項目的存儲庫樹。如何通過GitLab REST API獲取文件的原始內容?

獲取回購樹(工程)

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的端點。

回答

1

首先,爲了以防萬一,不要%的編碼了。「」:

.../files/pom.xml/raw?... 
      ^^ 

其次,你可以檢查文件終點如何影響從V3在merge request 9637this comparison

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只允許獲得原料的斑點,而不是原始文件。
merge request 16834

  • 修改/projects/:id/repository/files/projects/:id/repository/files/:filepath:filepath應該是URL編碼)
  • 移動/projects/:id/repository/blobs/:sha/projects/:id/repository/files/:filepath/raw

只有4版API允許:filepath參數。

請參閱「Git objects SHA-1 are file contents or file names?」以解碼您從API v3獲得的原始blob。

+0

非常感謝馮,我使用這個URL獲得了文件的內容:'https://gitlab.gspt.net/api/v3/projects/2931/repository/files?file_path = pom.xml&ref = master' 。但是,文件的內容是編碼的。如何獲取原始內容? – user2325154

+0

@ user2325154通過使用'/ projects /:id/repository/raw_blobs /:sha'語法,我在答案中提到:請參閱https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9637/diffs# 4d37e90ff02b5014b4a2fda7e01b5c67fb686c11_96_96 – VonC

+0

不確定在':sha'中指定什麼。 ':sha'是文件的commit-id嗎?我查看了您提供的鏈接,但沒有提及如何獲取文件的原始內容。我從上述URL獲得的內容是'base64'編碼的。我總是可以解碼並獲取文件的實際原始內容。但是有沒有辦法直接獲取文件的原始內容? – user2325154

相關問題