Call the python-gitlab module to manage project information and carry out CI practice

Installation and introduction

$ conda activate env
$ (env) pip install python-gitlab

# test.py
import gitlab

getting Started

gitlab.Gitlabis the primary class, handling the HTTP requests. It holds the GitLab URL and authentication information.
Before using the API, it is recommended to be familiar with the concepts of project, group, user, commit, CICD, etc.

Start from demo

# git账户的token
private_token = "onlyidiotwilltrythistoken"  # https://my.oschina.net/u/4308934/blog/3365888
# git地址
private_host = 'https://gitlab.com'

class Projects:
    # 获取基本信息
    def __init__(self, private_token):
        self.gl = gitlab.Gitlab(private_host, private_token=private_token)
        # 获取该用户的所有
        self.projects = self.gl.projects.list(
            membership=True,
            all=True,
        )
        self.groups = self.gl.groups.list(
            membership=True,
            all=True,
        )

Detailed

project

1. Properties

{
    
    'id': 223753, 
'description': '', 
'name': 'test_project1', 
'name_with_namespace': 'sjtu-biggie / test_project1', 
'path': 'test_project1', 
'path_with_namespace': 'sjtu-biggie/test_project1', 
'created_at': '2021-01-20T03:30:44.015Z',
'default_branch': 'master', 
'tag_list': [], 
'ssh_url_to_repo': '[email protected]:sjtu-biggie/test_project1.git', 
'http_url_to_repo': 'https://gitlab.com/sjtu-biggie/test_project1.git', 'web_url': 'https://gitlab.com/sjtu-biggie/test_project1', 'readme_url': 'https://gitlab.com/sjtu-biggie/test_project1/-/blob/master/README.md', 
'last_activity_at': '2021-01-20T03:30:44.015Z', 
'namespace': {
    
    'id': 6122086, 'name': 'sjtu-biggie', 'path': 'sjtu-biggie', 'kind': 'user', 'full_path': 'sjtu-biggie', 'parent_id': None, 'avatar_url': 'https://secure.gravatar.com/avatar/6aasdsd5a758cc3e3f80a716ad8cb?s=80&d=identicon', 'web_url':'https://gitlab.com/sjtu-biggie'}, 
'empty_repo': False, 
'archived': False, 
'visibility': 'public', 
'owner': {
    
    'id': 4661627, 'name': 'sjtu-biggie', 'username': 'sjtu-biggie', 'state': 'active', 'avatar_url': 'https://secure.gravatar.com/avatar/6ac582asdds58cc3e3f80a716ad8cb?s=80&d=identicon', 'web_url': 'https://gitlab.com/sjtu-biggie'}, 

2. The operation
gl.project.list()you see is the public project, and
gl.project.list(membership=True,all=True)you see all the projects related to you. Note that if you are in the intern group, all projects under intern/ will be listed, even if you are not a project member.

3. Create the project

project = myProject.gl.projects.create({
    
    'name':'test_project2'})

import & export

export = project.exports.create()
export.refresh()
while export.export_status != 'finished':
    time.sleep(1)
    export.refresh()
# Download the result
with open('repo/export.tgz', 'wb') as f:
    export.download(streamed=True, action=f.write)

commit

1. Properties

{
    
    
'id': '6b1bbec7bbdfas7d6c7ce499ce714e2e71fa2eb', 
'short_id': '6b1sdaec7', 
'created_at': '2021-01-10T03:30:44.000+00:00', 
'parent_ids': [], 
'title': 'Initial commit', 
'message': 'Initial commit', 
'author_name': 'sjtu-biggie', 
'author_email': '[email protected]', 
'authored_date': '2021-01-10T03:30:44.000+00:00', 
'committer_name': 'sjtu-biggie', 
'committer_email': '[email protected]', 
'committed_date': '2021-01-10T03:30:44.000+00:00', 
'web_url': 'https://gitlab.com/sjtu-biggie/test_project1/-/commit/6b1bbec7bbbb7b1asdsd9ce714e2e71fa2eb'
}

branch

1. Properties

 {
    
    
 'name': 'test_branch', 
 'commit': {
    
    'id': '980b816c1129bb37ca58081bef484fec69d6ddd7', 
 'short_id': '980b816c', 'created_at': '2021-01-20T05:53:47.000+00:00', 
 'parent_ids': None, 
 'title': 'Add new file', 
 'message': 'Add new file', 
 'author_name': 'sjtu-biggie', 
 'author_email': '[email protected]', 
 'authored_date': '2021-01-20T05:53:47.000+00:00', 
 'committer_name': 'sjtu-biggie', 
 'committer_email': '[email protected]', 
 'committed_date': '2021-01-20T05:53:47.000+00:00', 
 'web_url': 'https://gitlab.com/sjtu-biggie/test_project1/-/commit/980b816c1129bb37ca58081bef484fec69d6ddd7'
 }

member

1. Properties

{
    
    'id': 12345, 
'name': 'sjtu', 
'username': 'sjtu', 
'state': 'active', 
'avatar_url': 'https://secure.gravatar.com/avatar/6ac582858d5a758cc3e3f80a716ad8cb?s=80&d=identicon', 
'web_url': 'https://gitlab.com/sjtu', 
'access_level': 30, 
'created_at': '2021-01-25T06:47:26.231Z', 
'expires_at': None
}

member can be group.member or project.member can be
called member.delete()to exit a group/project

Get file information changed by commit

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44602409/article/details/112860729