from flask_cors import CORS
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import Wiql
app = Flask(__name__)
CORS(app)
ORGANIZATION = "preet442727"
PROJECT = "test"
PAT = ""
credentials = BasicAuthentication('', PAT)
connection = Connection(base_url=f"https://dev.azure.com/{ORGANIZATION}", creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()
@app.route("/work-items", methods=["GET"])
def get_work_items():
work_item_id = 1
try:
# Get work item by ID
work_item = wit_client.get_work_item(work_item_id)
return jsonify(work_item.as_dict())
except Exception as e:
return jsonify({"error": "Failed to fetch work item", "details": str(e)}), 500
@app.route("/all-work-items", methods=["POST"])
def get_all_work_items():
# Create the WIQL query to retrieve work items
wiql_query = Wiql(
query="""
SELECT [System.Id], [System.WorkItemType], [System.State]
FROM WorkItems
WHERE [System.TeamProject] = 'test'
ORDER BY [System.ChangedDate] DESC
"""
)
try:
result = wit_client.query_by_wiql(wiql_query)
work_items = result.work_items
detailed_work_items = []
for item in work_items:
work_item_id = item.id
work_item = wit_client.get_work_item(work_item_id)
detailed_work_items.append(work_item.as_dict())
return jsonify({"workItems": detailed_work_items})
except Exception as e:
return jsonify({"error": "Failed to fetch work items", "details": str(e)}), 500
@app.route("/all-work-items", methods=["GET"])
def get_work_item():
return get_all_work_items()
if __name__ == "__main__":
app.run(debug=True)
两个 get 请求都会随机给出错误 500,有时它会起作用并给出正确的响应,有时它会给我这个错误。 错误500 VSC 终端
如果我在邮递员上使用 devops URL,则使用本地服务器 url 的邮递员也会发生同样的情况https://dev.azure.com/preet442727/test/_apis/wit/workitems/1?api-version= 7.1 它有效并且每次都给我正确的响应,但不是本地服务器
根据您的 python 应用程序的代码片段,我在示例 hello-world 应用程序中使用
GET
API 进行了测试,它正在工作。 (我添加了from flask import jsonify
。)
from datetime import datetime
from flask import Flask, render_template, jsonify
from . import app
import os, json
from flask_cors import CORS
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import Wiql
ORGANIZATION = "MyOrgName"
PROJECT = "TheProjectName"
PAT = "xxxxxx"
credentials = BasicAuthentication('', PAT)
connection = Connection(base_url=f"https://dev.azure.com/{ORGANIZATION}", creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()
@app.route("/work-items", methods=["GET"])
def get_work_items():
work_item_id = 1
work_item = wit_client.get_work_item(work_item_id)
try:
# Get work item by ID
work_item = wit_client.get_work_item(work_item_id)
return jsonify(work_item.as_dict())
except Exception as e:
return jsonify({"error": "Failed to fetch work item", "details": str(e)}), 500
@app.route("/")
def home():
return render_template("home.html")
您可以尝试使用另一个脚本进行测试,以帮助确定问题是否是由 Azure DevOps API 本身引起的。
import json
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import Wiql
ORGANIZATION = "MyOrgName"
PROJECT = "TheProjectName"
PAT = "xxxxxx"
credentials = BasicAuthentication('', PAT)
connection = Connection(base_url=f"https://dev.azure.com/{ORGANIZATION}", creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()
work_item_id = 1
work_item = wit_client.get_work_item(work_item_id)
print(json.dumps(work_item.as_dict(), indent=4))
如果问题目前已消失,则可能与 Azure DevOps 事件有关。