【python】连接Jira获取token以及jira对象

此脚本可以连接Jira,通过Jira的token,Jira对象可以实现与Jira的交互,

从而完成jira与pytest的交互,或者其他自动化测试框架也行,

例如: 将pytest运行结果推送jira;将jira用例与自动化测试用例建立映射关系,将功能用例对应的自动化测试用例脚本路径推送到功能用例的描述栏,或者自动化栏 里面

python 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025-02-13 10:05
# @Author  : 
# @File    : connect_jira.py
# @Software: PyCharm

import requests
from jira import JIRA


class ConnJira:

    def __init__(self, jira_server, jira_username, jira_password):
        self.jira_server = jira_server
        self.jira_username = jira_username
        self.jira_password = jira_password


    def jira_login(self):
        try:
            # 创建一个 requests 会话对象
            session = requests.Session()
            jira = JIRA(server=self.jira_server, basic_auth=(self.jira_username, self.jira_password),
                        options={'session': session})

            # 创建一个自定义的会话对象并进行认证
            session.auth = (self.jira_username, self.jira_password)
            response = session.get(f'{self.jira_server}/projects/O45/summary')
            response.raise_for_status()
            # 提取 Cookie 信息
            cookie = response.headers['Set-Cookie'].split(';')[0] + ';' + \
                     response.headers['Set-Cookie'].split(';')[2].split(',')[1]
            # print(cookie)
            return cookie, self.jira_server, jira
        except requests.RequestException as req_ex:
            # 处理网络请求异常
            print(f"网络请求出错,未成功连接到jira!: {req_ex}")
        except Exception as ex:
            # 处理其他异常
            print(f"发生未知错误,未成功连接到jira! : {ex}")
        return None, None


# if __name__ == "__main__":
#     # jira_server = "http://10.1.20.64:8123"
#     # jira_username = 'duxiaowei12345678'
#     # jira_password = '123!123!123!123!'
#
#     conn = ConnJira()
#     cookie, jira_server, jira = conn.jira_login()
#     if cookie and jira:
#         print(cookie)
#         print(jira_server)
#         print("成功获取 Cookie 和 Jira 对象")
#     else:
#         print("登录失败")
相关推荐
顾林海5 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱7 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽12 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码12 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱1 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵1 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio1 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663671 天前
使用 Python 从零创建 Word 文档
python
Csvn1 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽1 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate