已复制
全屏展示
复制代码

gitlab 高效的 rest api 接口实践


· 3 min read

在公司内部如果有项目是基于 gitlab 的,如果想基于gitlab的版本管理进行二次开发,可能会用到 gitlab 的 rest api 接口,本文展示主要的 api 接口用法。

实际开发中,不会使用 curl 命令来操作 rest api,而是使用 Python 或者 JAVA 的网络请求库进行操作。

  • Bearer xxxxxx 其中 xxxxxx 表示创建的 gitlab token
  • 552 表示项目 id ,在项目详情里面可以查看
# gitlab 查看文件列表:
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/tree?recursive=true&per_page=100&page=2" 
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/tree?path=yuchaoshui"
# 返回 200


# gitlab rest api全文搜索:
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/search?scope=blobs&search=user_active" 
# 返回 200


# gitlab rest api文件名搜索
# 通过查看全部文件列表的方式搜索


# gitlab rest api查看所有的commits:
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/commits?with_stats=true" 
# 返回 200


# gitlab rest api查看单个commits、与diff(一个文件一个json结果):
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/commits/dc2d4b5999c33313e5684f4262fbec53f8226b6e" 
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/commits/dc2d4b5999c33313e5684f4262fbec53f8226b6e/diff" 
# 返回 200


# gitlab rest api查看指定文件的commits、与diff(一个文件一个json结果):
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/commits?path=yuchaoshui%2Ftest2.sql" 
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/commits/dc2d4b59/diff?path=yuchaoshui%2Ftest2.sql" 
# 返回 200


# gitlab rest api查看文件内容,ref为指定分支或者指定commit:
curl --include --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2F%E5%85%8D%E8%B4%B9%E7%A4%BC%E7%89%A9.sql?ref=master"
# 返回 200


# gitlab rest api查看文件内容(raw):
curl --include --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2F%E5%85%8D%E8%B4%B9%E7%A4%BC%E7%89%A9.sql/raw?ref=master"
# 返回 200


# gitlab rest api查看文件最后的修改用户:
curl --header "Authorization: Bearer xxxxxx" "https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2Ftest2.sql/blame?ref=master" 
# 返回 200


# gitlab rest api创建文件:
curl --include --request POST --header "Authorization: Bearer xxxxxx" --header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "yuchaoshui@xxx.com", "author_name": "yuchaoshui", "content": "select 1 as age", "commit_message": "create a new file"}' \
"https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2Ftest2.sql"
# 返回 201 与body{"file_path":"yuchaoshui/test2.sql","branch":"master"}


# gitlab rest api创建空目录:
curl --include --request POST --header "Authorization: Bearer xxxxxx" --header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "yuchaoshui@xxx.com", "author_name": "yuchaoshui", "content": "", "commit_message": "create a directory"}' \
"https://gitlab.xxx.com/api/v4/projects/552/repository/files/liuqiang%2F.gitkeep"
# 返回 201


# gitlab rest api更新文件:
curl --include --request PUT --header "Authorization: Bearer xxxxxx" --header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "yuchaoshui@xxx.com", "author_name": "yuchaoshui", "content": "select 1 as age, 'wang' as name", "commit_message": "create a new file"}' \
"https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2Ftest2.sql"
# 返回 200 与body {"file_path":"yuchaoshui/test2.sql","branch":"master"}


# gitlab rest api删除文件:
curl --include --request DELETE --header "Authorization: Bearer xxxxxx" --header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "yuchaoshui@xxx.com", "author_name": "yuchaoshui", "commit_message": "delete a file"}' \
"https://gitlab.xxx.com/api/v4/projects/552/repository/files/yuchaoshui%2Ftest3.sql"
# 返回 204

参考资料

  • 部署完gitlab后根据你的域名得到 https://gitlab.xxx.com/help/api/README.md
🔗

文章推荐