当前位置: 首页 > news >正文

Python 使用Gitlab Api

代码

REST API

见自带帮助文档

python

安装python-gitlab

pip install --upgrade python-gitlab

使用API

参考:https://python-gitlab.readthedocs.io/en/stable/api-usage.html

import gitlab# anonymous read-only access for public resources (GitLab.com)
gl = gitlab.Gitlab()# anonymous read-only access for public resources (self-hosted GitLab instance)
gl = gitlab.Gitlab('https://gitlab.example.com')# private token or personal token authentication (GitLab.com)
gl = gitlab.Gitlab(private_token='JVNSESs8EwWRx5yDxM5q')# private token or personal token authentication (self-hosted GitLab instance)
gl = gitlab.Gitlab(url='https://gitlab.example.com', private_token='JVNSESs8EwWRx5yDxM5q')# oauth token authentication
gl = gitlab.Gitlab('https://gitlab.example.com', oauth_token='my_long_token_here')

删除流水线

from bs4 import BeautifulSoup
import requests
import re 
import time
import json
import os
import gitlabfrom datetime import datetime, timedelta
from dateutil import parserdeadline = datetime.today() - timedelta(days=3)headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36', 'Accept-Language':'zh-CN,zh;q=0.9'}#全局变量
baseUrl="http://192.168.x.x"
privateToken="xxxxxxx"def listProject(gl):projects = gl.projects.list(iterator=True)for project in projects:print(f"name:{project.path_with_namespace},id:{project.id} \n")def deleteProjectById(gl,pjId):project = gl.projects.get(pjId)deleteProject(project)def deleteProject(project):deleteProjectPipelines(project)def deleteProjectPipelines(project):pipelines = project.pipelines.list(iterator=True)for pipeline in pipelines:deletePipeline(project,pipeline);def deletePipeline(project,pipeline):createdAt = parser.isoparse(pipeline.created_at).replace(tzinfo=None)if createdAt < deadline :print("delete pipeline: " + str(project.id) + "," + str(pipeline.id) )pipeline.delete()time.sleep(2)if __name__ == "__main__": # 运行入口gl = gitlab.Gitlab(baseUrl, private_token=privateToken)deleteProjectById(gl,421)

附录

参考

REST API 资源:https://gitlab.cn/docs/jh/api/api_resources.html

Python-Gitlab API:https://python-gitlab.readthedocs.io/en/stable/index.html

其他包

pip3 install beautifulsoup4
http://www.lqws.cn/news/480709.html

相关文章:

  • Javaweb - 4.1 JavaScript
  • (线性代数最小二乘问题)Normal Equation(正规方程)
  • Go语言--语法基础6--基本数据类型--数组类型(1)
  • rom定制系列------红米note11 5G版 MTK芯片强解bl锁修复bug 官方系统 面具root批量线刷版
  • C++结构体初始化与成员函数实现语法详解
  • python基础(for...else...)
  • 怎么让二级域名绑定到wordpesss指定的页面
  • python源码:执行pdf合并/分页/图片管理功能
  • Linux 下的 socket
  • 如何用AI开发完整的小程序<9>—UI自适应与游戏页优化
  • Docker 高级管理——容器通信技术与数据持久化
  • Neo4j 中存储和查询数组数据的完整指南
  • 删除node并且重装然后重装vue
  • day039-nginx配置补充
  • 从 Cluely 融资看“AI 协同开发”认证:软件考试应该怎么升级?
  • 【unitrix】 4.0 类型级数值表示系统(types.rs)
  • 如何用AI开发完整的小程序<7>—让AI微调UI排版
  • 深度解析云计算网络架构:VLAN+OVS+Bonding构建高可靠虚拟化平台
  • Redis-CPP 5大类型操作
  • 深入解析C#数组协变与克隆机制
  • c#websocket心跳包自定义实现,支持异步操作的取消
  • C++——继承
  • 7.4.1_1B树
  • 从零开始手写redis(16)实现渐进式 rehash map
  • 《福格行为模型》
  • 鲲鹏服务器创建Zookeeper镜像实例
  • 【LLIE专题】NTIRE 2025 低照度图像增强第一名方案解读
  • C#设计模式-Builder-生成器-对象创建型模式
  • 编程江湖-设计模式
  • CentOS7中源码编译安装freeswitch