当前位置: 首页 > 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.lryc.cn/news/573813.html

相关文章:

  • Kafka与Zookeeper在linux上的下载记录
  • LLMs之Embedding:Qwen3 Embedding的简介、安装和使用方法、案例应用之详细攻略
  • ms-swift 部分命令行参数说明
  • OpenGL ES 中的材质
  • AI 辅助生成 Mermaid 流程图
  • 华为公布《鸿蒙编程语言白皮书》V1.0 版:解读适用场景
  • Android14 app被冻结导致进程间通信失败
  • 电脑商城--购物车
  • 【C++】C++枚举、const、static的用法
  • Python炫酷星空
  • 前端的跨域问题
  • Java Stream流
  • 量化-因子处理
  • 飞轮储能辅助双馈风机参与电力系统一次调频的仿真模型研究
  • 半导体二极管
  • work遇到的状态码
  • 跨平台开发flutter初体验
  • 华为云Flexus+DeepSeek征文|华为云 Dify 高可用部署教程:CCE 容器集群一键构建企业级智能应用
  • 登录拦截功能实现 -瑞吉外卖笔记
  • Windows 后渗透中可能会遇到的加密字符串分析
  • 等等等等等等
  • PostgreSQL全栈部署指南:从零构建企业级高可用数据库集群
  • pyqt f-string
  • jenkins对接、jenkins-rest
  • LocalAI: OpenAI、Claude 等的开源替代
  • 3、NLP黄金九步法(问题定义-数据获取-数据探索)
  • Flink Connector Kafka深度剖析与进阶实践指南
  • js 函数参数赋值问题
  • 【Android】am命令
  • ROS 2 中 Astra Pro 相机与 YOLOv5 检测功能编译启动全记录