从 Git 提交中获取数据并将其放入 YAML(Gitlab)
Get data from Git commit and put it into a YAML (Gitlab)
正如标题所说,我想读取我的 git 提交信息(用户 ID、消息)并将其放入一个 yaml 文件中,该文件将在我当前的项目文件夹中创建并推送更改将 yaml 文件放入 git。我想我必须使用 GITLAB API 但我不确定如何。
这听起来很复杂,但想法是将每次提交的信息保存在 yaml 文件中,这些文件将被添加到另一个更改列表中。
到目前为止,这是我的代码,我刚刚实现了输入选项,但我希望它自动填充。
有人知道怎么做吗?
import yaml
data = dict(
gittag='',
gittagdate='',
userID=str(input('autor: ')),
change_id=str(input('change_id: ')),
message=str(input('description: ')),
)
with open('test.yml', 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False)
你可以像这样直接使用 Gitlab 提交 API :
import yaml
import requests
access_token = "YOUR_ACCESS_TOKEN"
gitlab_host = "your.gitlab.host" #gitlab.com or your.host for gitlab CE
project_id = "24" #change this with your project id
r = requests.get(f"https://{gitlab_host}/api/v4/projects/{project_id}/repository/commits",
params = {
"per_page": 100
},
headers= {
"Authorization": f"Bearer {access_token}"
})
commits = r.json()
print(len(commits))
with open('commits.yml', 'w') as outfile:
yaml.dump(commits, outfile, default_flow_style=False)
这将在 yaml 文件中写入提交列表,例如:
- author_email: user@acme.com
author_name: user123
authored_date: '2020-09-16T15:03:20.000+02:00'
committed_date: '2020-09-16T15:03:20.000+02:00'
committer_email: user@acme.com
committer_name: user123
created_at: '2020-09-16T15:03:20.000+02:00'
id: b076af18c1db5b15714cd78714a674d4cc97605f
message: 'version 1.5.1
'
parent_ids:
- 53aa56f53aadf3eb709d799cdda2ad852c50aa3a
short_id: b076af17
title: version 1.5.1
- .......
您还需要create a personal access token in Gitlab and get your project id
上面的代码为您提供了最后 100 次提交,为了对结果进行分页,您需要检查 Link
header 并解析它。但是有this python Gitlab library that does it for you with the docs here
以下将遍历所有提交并创建 yaml 文件:
import yaml
import gitlab
gl = gitlab.Gitlab('https://your.gitlab.host', private_token='YOUR_ACCESS_TOKEN')
commits_obj = gl.projects.get(24).commits.list(all=True)
commits = [t.attributes for t in commits_obj]
print(len(commits))
with open('commits.yml', 'w') as outfile:
yaml.dump(commits, outfile, default_flow_style=False)
也结帐this
正如标题所说,我想读取我的 git 提交信息(用户 ID、消息)并将其放入一个 yaml 文件中,该文件将在我当前的项目文件夹中创建并推送更改将 yaml 文件放入 git。我想我必须使用 GITLAB API 但我不确定如何。
这听起来很复杂,但想法是将每次提交的信息保存在 yaml 文件中,这些文件将被添加到另一个更改列表中。
到目前为止,这是我的代码,我刚刚实现了输入选项,但我希望它自动填充。 有人知道怎么做吗?
import yaml
data = dict(
gittag='',
gittagdate='',
userID=str(input('autor: ')),
change_id=str(input('change_id: ')),
message=str(input('description: ')),
)
with open('test.yml', 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False)
你可以像这样直接使用 Gitlab 提交 API :
import yaml
import requests
access_token = "YOUR_ACCESS_TOKEN"
gitlab_host = "your.gitlab.host" #gitlab.com or your.host for gitlab CE
project_id = "24" #change this with your project id
r = requests.get(f"https://{gitlab_host}/api/v4/projects/{project_id}/repository/commits",
params = {
"per_page": 100
},
headers= {
"Authorization": f"Bearer {access_token}"
})
commits = r.json()
print(len(commits))
with open('commits.yml', 'w') as outfile:
yaml.dump(commits, outfile, default_flow_style=False)
这将在 yaml 文件中写入提交列表,例如:
- author_email: user@acme.com
author_name: user123
authored_date: '2020-09-16T15:03:20.000+02:00'
committed_date: '2020-09-16T15:03:20.000+02:00'
committer_email: user@acme.com
committer_name: user123
created_at: '2020-09-16T15:03:20.000+02:00'
id: b076af18c1db5b15714cd78714a674d4cc97605f
message: 'version 1.5.1
'
parent_ids:
- 53aa56f53aadf3eb709d799cdda2ad852c50aa3a
short_id: b076af17
title: version 1.5.1
- .......
您还需要create a personal access token in Gitlab and get your project id
上面的代码为您提供了最后 100 次提交,为了对结果进行分页,您需要检查 Link
header 并解析它。但是有this python Gitlab library that does it for you with the docs here
以下将遍历所有提交并创建 yaml 文件:
import yaml
import gitlab
gl = gitlab.Gitlab('https://your.gitlab.host', private_token='YOUR_ACCESS_TOKEN')
commits_obj = gl.projects.get(24).commits.list(all=True)
commits = [t.attributes for t in commits_obj]
print(len(commits))
with open('commits.yml', 'w') as outfile:
yaml.dump(commits, outfile, default_flow_style=False)
也结帐this