zoukankan      html  css  js  c++  java
  • 使用python-gitlab包在gitlab上自动创建分组

    项目需求:在公司内部搭建的gitlab服务器上,需要在补丁包service-pack分组内,根据补丁包所支持的版本号,再建立一级子分组,然后在版本号的路径下面,存放对应版本的补丁包项目.

    格式形如:

    --普通补丁包

      --6.0.0

        --对应的补丁包项目

    官方文档:https://python-gitlab.readthedocs.io/en/stable/index.html

    gitlab官方文档:https://docs.gitlab.com/ee/api/groups.html

    准备工作:

    1、先要去gitlab服务器上面生成自己的token。具体操作步骤:点击个人中心的settings--->左侧边栏的访问令牌--->添加个人访问令牌--->设置名称,过期时间之后点击创建个人访问令牌即可。

    2、在个人开发的服务器上面配置gitlab账号,具体步骤百度即可。

    主要用到的文档部分:

    Groups部分:https://python-gitlab.readthedocs.io/en/stable/gl_objects/groups.html

    Projects部分:https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html

    # coding:utf-8
    
    # 模块名字叫python-gitlab,在这里导包的时候是 gitlab
    import gitlab
    
    gl = gitlab.Gitlab(gitlab服务器地址, private_token=前面设置的个人访问令牌)
    
    # 默认all=False,单页显示20条数据,
    # 设置为all=True后显示所有
    groups = gl.groups.list(all=True)
    
    # 获取指定分组
    # 分组ID在 gitlab中的设置-->常规-->GroupID 获取
    group = gl.groups.get(分组ID)
    
    # 获取当前分组的所有子分组
    all_groups = group.subgroups.list(all=True)
    # 获取所有子分组的名字
    sub_group_names = [i.name for i in all_groups]
    
    # 如果知道了分组的ID,可以直接获取该分组对象,不必从gitlab服务器的根路径下开始进行操作.例如:
    def create_version_group(version_str):
        """根据补丁支持的版本号在git上面创建对应的group"""
        gl = gitlab.Gitlab(GIT_URL, private_token=PRIVATE_TOKEN)
        service_pack_group = gl.groups.get(COMMON_PACK_GROUP_ID)
        # 获取当前 补丁包组 下面所有的子组
        all_groups = service_pack_group.subgroups.list(all=True)
        group_names = [i.name for i in all_groups]
        if version_str not in group_names:
            data = {
                "name": version_str,
                "path": version_str,
                "parent_id": COMMON_PACK_GROUP_ID
            }
            target_group = gl.groups.create(data)
            print("git上面创建对应的版本分组成功!")
            return target_group
        else:
            return
    
    # 这里要注意创建分组的时候.data参数中需要指定name,path,parent_id(就是上一级分组的ID)三个参数.在之前的调试过程中,因为没有指定parent_id,程序一直抛出401权限认证失败的异常.
    
    # 当前分组下的所有项目
    all_projects = target_group.projects.list(all=True)
    pkg_names = [i.name for i in all_projects]
    
    # 删除指定分组
    gl.groups.delete(group_id)
    # 删除指定项目
    gl.projects.delete(pkg_id)
  • 相关阅读:
    [Leetcode Week3]Evaluate Division
    [Leetcode Week3]Course Schedule
    [Leetcode Week3]Clone Graph
    [Leetcode Week2]Sort List
    [Leetcode Week2]Sort Colors
    [Leetcode Week2]Merge Intervals
    [Leetcode Week1]Longest Substring Without Repeating Characters
    哈夫曼树的数组实现
    cocos2d-x3.16 default模式项目 Android Studio C++文件编译失败问题
    html display和visibility在资源加载上的区别
  • 原文地址:https://www.cnblogs.com/huaibin/p/12752165.html
Copyright © 2011-2022 走看看