调用python-gitlab模块管理项目信息,进行CI实践

安装与引入

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

# test.py
import gitlab

入门

gitlab.Gitlab is the primary class, handling the HTTP requests. It holds the GitLab URL and authentication information.
在使用API前,建议先熟悉project,group,user,commit,CICD等概念

从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,
        )

详解

project

1.属性

{
    
    '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.操作
gl.project.list()看到的是公开的project,
gl.project.list(membership=True,all=True)看到的是与自己有关的所有project,注意比如你是intern组的,那么intern/下的所有project都会被列出来,即使你不是project member。

3.创建项目

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

{
    
    
'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.属性

 {
    
    
 '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.属性

{
    
    '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可以是group.member也可以是project.member
调用member.delete()就可以退出一个组/项目

获取commit更改的文件信息

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44602409/article/details/112860729