通过rest请求操作git的API接口

2 篇文章 0 订阅

gitApi操作需求

所需提供参数列表

参数名称描述
PRIVATE-TOKEN用于操作gitapi权限所需的token
projectId当前git项目的id
author_email提交人邮箱
author_name提交人名称
commit_message提交信息

1、搜索restful项目的projectId

https://xxx.xxx.com/api/v4/projects?private_token=xxxxxxxxxxxxxxxx&per_page=100&page=0&order_by=name

通过api返回结果查询当前项目id

2、获取文件内容

1、请求URL

https://xxx.xxx.com/regression/restapi/-/raw/{filePath}?inline=false

2、请求方式

POST

3、请求参数

请求参数必选参数类型说明
filePathtrueString带项目平台的文件全路径
a/b/c/d.txt

4、测试请求示例

curl -k --location --request GET 'https://xxx.xxx.com/regression/restapi/-/raw/a/b/c/d.txt ?inline=false'

5、返回结果

文件具体内容

3、提交新的文件

1、请求URL

https://xxx.xxx.com/api/v4/projcts/{projectId}/repository/files/{fileName}

2、请求方式

POST

3、请求参数

请求参数必选参数类型说明
projectIdtrueString当前项目编号
fileNametrueString需要存储的git目录
branchtrueString所需提交的分支
author_emailtrueString提交人邮箱
author_nametrueString提交人名称
contenttrueString文件内容
commit_messagetrueString提交描述

4、测试请求示例

curl -k --location --request POST 'https://xxx.xxx.com/api/v4/projects/280/repository/files/readme12?branch=master&author_email=ffeng@qq.com&author_name=ffeng&content=xxxyyy&commit_message=createfile' \
--header 'PRIVATE-TOKEN: xxxxxxxxxxxxxx'

4、更新文件内容

1、请求URL

https://xxx.xxx.com/api/v4/projcts/{projectId}/repository/files/{fileName}

2、请求方式

PUT

3、请求参数

请求参数必选参数类型说明
projectIdtrueString当前项目编号
fileNametrueString需要存储的git目录
branchtrueString所需提交的分支
author_emailtrueString提交人邮箱
author_nametrueString提交人名称
contenttrueString文件内容
commit_messagetrueString提交描述

4、测试请求示例

**TIP:**文件路径必须存在,否则报错

curl -k --location --request PUT 'https://xxx.xxx.com/api/v4/projects/280/repository/files/readme12?branch=master&author_email=ffeng@qq.com&author_name=ffeng&content=xxxyyy&commit_message=createfile' \
--header 'PRIVATE-TOKEN: xxxxxxxxxxxxxxx'

5、删除文件

1、请求URL

https://xxx.xxx.com/api/v4/projcts/{projectId}/repository/files/{fileName}

2、请求方式

DELETE

3、请求参数

请求参数必选参数类型说明
projectIdtrueString当前项目编号
fileNametrueString需要存储的git目录
branchtrueString所需提交的分支
author_emailtrueString提交人邮箱
author_nametrueString提交人名称
commit_messagetrueString提交描述

4、测试请求示例

**TIP:**文件路径必须存在,否则报错

curl -k --location --request DELETE 'https://xxx.xxx.com/api/v4/projects/280/repository/files/readme12?branch=master&author_email=ffeng@qq.com&author_name=ffeng&commit_message=createfile' \
--header 'PRIVATE-TOKEN: xxxxxxxxxxxxxxxxx'
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GitLab 提供了 REST API,可以使用该 API 来获取 Git 仓库的信息,包括代码行数。以下是获取 GitLab 中指定项目代码行数的 API 接口实例: 1. 首先,需要获取 GitLab 的访问令牌。访问令牌可在 GitLab 的用户设置中生成。 2. 发送 GET 请求,获取项目 ID。可以使用以下 URL: ``` https://gitlab.com/api/v4/projects?search={project_name}&private_token={access_token} ``` 其中,`{project_name}` 是项目名称,`{access_token}` 是访问令牌。 3. 获取指定项目的代码行数。可以使用以下 URL: ``` https://gitlab.com/api/v4/projects/{project_id}/repository/files?private_token={access_token}&ref={branch_name}&file_path={file_path} ``` 其中,`{project_id}` 是上一步获取的项目 ID,`{branch_name}` 是分支名称,`{file_path}` 是文件路径。 4. 解析 API 返回的 JSON 数据,获取代码行数。 以下是 Python 代码示例: ```python import requests import json # GitLab 访问令牌 access_token = 'your_access_token' # 项目名称 project_name = 'your_project_name' # 分支名称 branch_name = 'your_branch_name' # 文件路径 file_path = 'your_file_path' # 获取项目 ID url = f'https://gitlab.com/api/v4/projects?search={project_name}&private_token={access_token}' response = requests.get(url) project_id = json.loads(response.text)[0]['id'] # 获取代码行数 url = f'https://gitlab.com/api/v4/projects/{project_id}/repository/files?private_token={access_token}&ref={branch_name}&file_path={file_path}' response = requests.get(url) lines = json.loads(response.text)['lines'] print(f'代码行数: {lines}') ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值