Arif Nadeem
Arif Nadeem

Reputation: 8604

How to access source code files and list directories using GitLab API?

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?

Upvotes: 4

Views: 15512

Answers (1)

VonC
VonC

Reputation: 1323793

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

Upvotes: 6

Related Questions