我正在尝试使用 python 脚本以编程方式在 Azure 板上创建问题卡。
我正在使用 PAT(个人访问令牌)。
headers = base64encodedPAT
headers[Content-Type']='application/json-patch+json'
payload = dictionary_of_different_values_to_set
requesturl=f"https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Issue?api-version=7.0"
requestcall = requests.post(url=requesturl, headers=headers, data=bytes(json.dumps(payload),'utf-8'))
我尝试了什么:
请让我知道我的方法有什么问题,或者提出一些想法,或者向我指出好的在线资源。
我正在尝试使用 python 脚本以编程方式在 Azure 板上创建问题卡。
解决方案 1:对授权标头的
personal_access_token
进行编码。
import requests
import base64
# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
personal_access_token = ''
work_item_type = 'Issue' # e.g., Task, Bug
# Encode the PAT for the Authorization header
encoded_pat = base64.b64encode(f':{personal_access_token}'.encode()).decode()
# Define the headers
headers = {
'Content-Type': 'application/json-patch+json',
'Authorization': f'Basic {encoded_pat}'
}
# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.1-preview.3'
# Data for the new work item
data = [
{
"op": "add",
"path": "/fields/System.Title",
"value": "Sample work item"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "This is a sample work item created via REST API."
}
]
# Make the request to create the work item
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
work_item = response.json()
print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
# Print the raw response content for debugging
print(f"Failed to create work item: {response.status_code}")
print(f"Response content: {response.text}")
解决方案2:直接在
auth=('', personal_access_token)
中使用requests.post
。
import requests
# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
personal_access_token = ''
work_item_type = 'Issue' # e.g., Task, Bug
# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.0'
# Data for the new work item
data = [
{
"op": "add",
"path": "/fields/System.Title",
"value": "Sample work item"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "This is a sample work item created via API."
}
]
# Make the request to create the work item
response = requests.post(url, json=data, headers={'Content-Type': 'application/json-patch+json'}, auth=('', personal_access_token))
if response.status_code == 200:
work_item = response.json()
print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
# Print the raw response content for debugging
print(f"Failed to create work item: {response.status_code}")
print(f"Response content: {response.text}")
from azure.devops.connection import Connection
from azure.devops.v7_1.work_item_tracking.models import JsonPatchOperation
from msrest.authentication import BasicAuthentication
# Replace with your Azure DevOps organization and project details
organization_url = 'https://dev.azure.com/orgname'
project = ''
personal_access_token = ''
work_item_type = 'Issue' # e.g., Task, Bug
# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()
# Data for the new work item
document = [
JsonPatchOperation(
op='add',
path='/fields/System.Title',
value='Sample work item'
),
JsonPatchOperation(
op='add',
path='/fields/System.Description',
value='This is a sample work item created via API.'
)
]
# Create the work item
work_item = wit_client.create_work_item(
document=document,
project=project,
type=work_item_type
)
# Print the created work item details
print(f"Work item created: {work_item.id}, title: {work_item.fields['System.Title']}")
我看到了很多 YouTube 视频,其中展示了 PAT 的创建,但没有介绍如何在管道中使用它。
要在管道中使用上述Python脚本,可以按照以下步骤操作:
trigger: none
pool:
vmImage: windows-latest
steps:
- bash: pip install requests
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
import requests
# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
pat = '$(PAT)'
work_item_type = 'Issue' # e.g., Task, Bug
# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.0'
# Data for the new work item
data = [
{
"op": "add",
"path": "/fields/System.Title",
"value": "Sample work item"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "This is a sample work item created via API."
}
]
# Make the request to create the work item
response = requests.post(url, json=data, headers={'Content-Type': 'application/json-patch+json'}, auth=('', pat))
if response.status_code == 200:
work_item = response.json()
print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
# Print the raw response content for debugging
print(f"Failed to create work item: {response.status_code}")
print(f"Response content: {response.text}")
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
import requests
import base64
# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
pat = '$(PAT)'
work_item_type = 'Issue' # e.g., Task, Bug,
# Encode the PAT for the Authorization header
encoded_pat = base64.b64encode(f':{pat}'.encode()).decode()
# Define the headers
headers = {
'Content-Type': 'application/json-patch+json',
'Authorization': f'Basic {encoded_pat}'
}
# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.1-preview.3'
# Data for the new work item
data = [
{
"op": "add",
"path": "/fields/System.Title",
"value": "Sample work item"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "This is a sample work item created via REST API."
}
]
# Make the request to create the work item
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
work_item = response.json()
print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
# Print the raw response content for debugging
print(f"Failed to create work item: {response.status_code}")
print(f"Response content: {response.text}")
- bash: pip install azure-devops
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
from azure.devops.connection import Connection
from azure.devops.v7_1.work_item_tracking.models import JsonPatchOperation
from msrest.authentication import BasicAuthentication
# Replace with your Azure DevOps organization and project details
organization_url = 'https://dev.azure.com/orgname'
project = ''
personal_access_token = '$(PAT)'
work_item_type = 'Issue' # e.g., Task, Bug
# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()
# Data for the new work item
document = [
JsonPatchOperation(
op='add',
path='/fields/System.Title',
value='Sample work item'
),
JsonPatchOperation(
op='add',
path='/fields/System.Description',
value='This is a sample work item created via API.'
)
]
# Create the work item
work_item = wit_client.create_work_item(
document=document,
project=project,
type=work_item_type
)
# Print the created work item details
print(f"Work item created: {work_item.id}, title: {work_item.fields['System.Title']}")