繁体   English   中英

Gitlab API 从本地文件系统上传整个项目

[英]Gitlab API to upload whole project from local file system

我需要使用 API 上传整个项目文件夹,其中包含多个文本文件、图像文件、源文件等。

我已经完成了 Gitlab 提交和上传 API,但它们都不符合要求。

是否有任何 API 上传完整的项目文件夹表格 API?

我用下面的API创建项目

POST : https://repo.**.com/api/v4/projects
{
    "name": "test",
    "default_branch": "master"
}

使用 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

您可以根据帖子数据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'])")

然后您可以按照现有文件夹的新项目说明进行操作:

#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

其中existing_folder是包含源文件的文件夹,您要上传这些文件作为对新 gitlab 项目的提交。

您可以使用存储库文件 API 来创建文件。

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'

遇到了同样的问题,最终像这样遍历文件夹:

import os

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

它工作得很好......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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