0

I need to upload using API whole project folder which contains multiple text files, image files, source files, etc.

I had gone through Gitlab Commits and upload API's but none of them matches requirement.

Is there any API to upload full project folder form API?

I used below API to create a project

POST : https://repo.**.com/api/v4/projects
{
    "name": "test",
    "default_branch": "master"
}
makozaki
  • 3,772
  • 4
  • 23
  • 47
Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56
  • Did you try this: https://docs.gitlab.com/ee/api/project_import_export.html#schedule-an-export – Ashwani Dec 23 '19 at 14:15
  • What do you mean by upload? You want to commit content of folder to project? Or you want to upload the content to some storage like s3? – makozaki Dec 24 '19 at 06:31
  • @makozaki I want to upload or commit fresh copy of all files present in source folder-like when we push code for very first time from gitbash – Vishwanath.M Dec 30 '19 at 05:41
  • You want to create a new project on gitlab for each such project or you already have existing project for it? – makozaki Dec 30 '19 at 07:48
  • @makozaki Empty project folder has been created already through another API, Now i need to upload or commit all source files like .Java files from API – Vishwanath.M Dec 30 '19 at 09:02
  • What did you do so far to accomplish your goal? It is still unclear to me what do you want to do and what is the problem. Are you familiar with Git? Do you know how to work with git project? I assume it is not an issue with `git add/git commit/git push` flow. From what you wrote you have result folder from some other tool with some files, which you want to upload somewhere. In general git repository should be used to store changes in source files, not results of some compilation or build process. – makozaki Dec 30 '19 at 10:27
  • @makozaki Am good with git through git bash, now i have requirement like using gitlab apis i need to create a project and push code exactly like how git bash commands works, Please check my updated question – Vishwanath.M Dec 31 '19 at 02:18

4 Answers4

0

After creating new project with gitlab api, e.g.:

curl --fail -d '{"name":"test","default_branch": "master"}' -H "Content-Type: application/json" -X POST https://repo.**.com/api/v4/projects?access_token=<your access token> --output project_details.json

You could assume your new project url based on post data name -> https://repo.**.com/<gitlab user or gitlab group>/test.git or you could parse response json with some tool, e.g. python:

http_url_to_repo=$(python -c "import json; print(json.load(open('project_details.json'))['http_url_to_repo'])")

Then you could follow new project instruction for existing folder:

#Push an existing folder
cd existing_folder
git init
git remote add origin $http_url_to_repo
git add .
git commit -m "Initial commit"
git push -u origin master

Where existing_folder is folder with your source files which you want to upload as a commit to new gitlab project.

makozaki
  • 3,772
  • 4
  • 23
  • 47
  • i knew git bash commands, i want commit and push code using API instead of git bash commands – Vishwanath.M Jan 03 '20 at 04:17
  • I think api is for listing commits, not for commiting files. Git commands are for this purpose. If you are familiar with git commands then what stops you from using them? Is your use case different than what I described? – makozaki Jan 03 '20 at 07:59
  • in my usecase i have to push or upload code files usig API's only – Vishwanath.M Jan 07 '20 at 06:50
0

You can use repository files API to create files.

https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository

POST /projects/:id/repository/files/:file_path
curl --request POST --header 'PRIVATE-TOKEN: <your_access_token>' --header "Content-Type: application/json" \
  --data '{"branch": "master", "author_email": "author@example.com", "author_name": "Firstname Lastname", \
    "content": "some content", "commit_message": "create a new file"}' \
  'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fproject%2Erb'
Shashank V
  • 10,007
  • 2
  • 25
  • 41
0

Had the same problem and ended up walking the folder like this:

import os

for (dirpath, dirnames, files) in os.walk(my_folder):
  for file in files:
    # do the api call to commit for dirpath, file 

It works nicely...

0

I don't think there's a way to upload a project folder using GitLab API. The API only deals with single files for the "../repository/files/:file_path" endpoint.

https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository

And even if you use the "../repository/commits" endpoint, you have to add information about individual files.

https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions

But when uploading a single file, if the folders in that file path are not present, they are created