[英]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.我需要使用 API 上传整个项目文件夹,其中包含多个文本文件、图像文件、源文件等。
I had gone through Gitlab Commits and upload API's but none of them matches requirement.我已经完成了 Gitlab 提交和上传 API,但它们都不符合要求。
Is there any API to upload full project folder form API?是否有任何 API 上传完整的项目文件夹表格 API?
I used below API to create a project我用下面的API创建项目
POST : https://repo.**.com/api/v4/projects
{
"name": "test",
"default_branch": "master"
}
After creating new project with gitlab api, eg:使用 gitlab api 创建新项目后,例如:
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:您可以根据帖子数据
name
假设您的新项目 url -> https://repo.**.com/<gitlab user or gitlab group>/test.git
或者您可以使用一些工具解析响应 json,例如 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.其中
existing_folder
是包含源文件的文件夹,您要上传这些文件作为对新 gitlab 项目的提交。
You can use repository files API to create files.您可以使用存储库文件 API 来创建文件。
https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository 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...它工作得很好......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.