1

as the title says I want to read information of my git commit (user ID, message) and put it into a yaml file, which will be created in my current project folder and push the changes with the yaml file into git. I think I have to work with the GITLAB API but I'm not sure how.

It sounds complicated but the idea is to save yaml files with the information of every commit and these files will be added into a another change list.

This is my code so far, I just implemented the input option, but I want it automatically filled. Does someone know how?

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)
Shalomi90
  • 670
  • 1
  • 8
  • 30
  • Gitlab API [commits](https://docs.gitlab.com/ee/api/commits.html) let's you collect commits info directly from gitlab. But whole idea to get commit info and repack it to yaml seems fishy. I wonder if what you want to achieve is already there but we don't know what you actually want. – makozaki Sep 23 '20 at 09:26
  • Thanks, I saw this page. It will help me but I'm still not sure how to solve it – Shalomi90 Sep 23 '20 at 10:08

1 Answers1

0

You can use Gitlab commits API directly like this :

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)

which will write the list of commits in a yaml file like :

- 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
- .......

You will also need to create a personal access token in Gitlab and get your project id

The code above gives you the last 100 commits, in order to paginate through results, you need to check the Link header and parse it. But there is this python Gitlab library that does it for you with the docs here

The following will iterate through all commits and create the yaml file:

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)

Also checkout this

Bertrand Martel
  • 32,363
  • 15
  • 95
  • 118