简体   繁体   中英

Gitlab API to upload whole project from local file system

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"
}

After creating new project with gitlab api, eg:

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, eg 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.

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'

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM