我目前正在执行任务,其目标是提供一个表格,其中包含从日期 01/01/2024 到 01/06/2024 上我们的 Azure 仪表板上所有管道版本的数据。
虽然我永远不会手动执行此操作,但我开始寻找一种自动化的方法。
我创建了一个 python 脚本来从 Azure API 获取版本并
https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1-preview.8
正在检索项目名称、发布名称、发布日期,但我找不到有关与该发布关联的提交消息的任何相关信息。
我还要求 ChatGPT 帮助我解决这个问题,它给了我一个旧的 API,因为我认为它返回 404。
检查 Azure API 时,我发现一个 API 正在检索所有提交,而不是与构建关联的 API。
我尝试过这个脚本
这是脚本。
`import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Replace with your organization, personal access token, and base URL
organization = 'NAME_OF_ORGANIZATION'
pat = 'PAT_TOKEN'
base_url = f'https://dev.azure.com/{organization}'
# Function to get list of projects
def get_projects():
url = f'{base_url}/_apis/projects?api-version=6.0'
response = requests.get(url, auth=HTTPBasicAuth('', pat))
print(f"Projects API Response Status: {response.status_code}")
# print(f"Projects API Response Text: {response.text}")
return response.json()
# Function to get list of releases for a project
def get_releases(project):
url = f'https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1-preview.8'
response = requests.get(url, auth=HTTPBasicAuth('', pat))
print(f"Releases API Request URL: {url}")
print(f"Releases API Response Status: {response.status_code}")
print(f"Releases API Response Text: {response.text}")
return response.json()
# Function to get details of a release
def get_release_details(project, release_id):
# GET https://vsrm.dev.azure.com/fabrikam/MyFirstProject/_apis/release/releases?definitionId=1&api-version=7.1-preview.8
url = f'https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/?definitionId=2&api-version=7.1-preview.8'
response = requests.get(url, auth=HTTPBasicAuth('', pat))
print(f"Release Details API Response Status: {response.status_code}")
# print(f"Release Details API Response Text: {response.text}")
return response.json()
# Fetch projects
projects = get_projects()
# Prepare a list to hold release data
release_data = []
# Iterate through each project
for project in projects['value']:
project_name = project['name']
releases = get_releases(project_name)
# Iterate through each release in the project
for release in releases['value']:
release_id = release['id']
release_name = release['name']
release_date = release['createdOn']
release_details = get_release_details(project_name, release_id)
# Gather commits and work items
commits = []
work_items = []
for artifact in release_details['value']:
build_id = artifact['id']
# GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=7.1-preview.1
commits_url = f'{base_url}/{project_name}/_apis/build/builds/{build_id}/commits?api-version=6.0'
commits_response = requests.get(commits_url, auth=HTTPBasicAuth('', pat))
commits_data = commits_response.json()
for commit in commits_data['value']:
commits.append(commit['comment'])
for artifact in release_details['artifacts']:
build_id = artifact['definitionReference']['version']['id']
work_items_url = f'{base_url}/{project_name}/_apis/build/builds/{build_id}/workitems?api-version=6.0'
work_items_response = requests.get(work_items_url, auth=HTTPBasicAuth('', pat))
work_items_data = work_items_response.json()
for work_item in work_items_data['value']:
work_items.append(work_item['id'])
# Add the release information to the list
release_data.append({
'Project Name': project_name,
'Release Name': release_name,
'Release Date': release_date,
'Commits': ', '.join(commits),
'Work Items': ', '.join(work_items)
})
# Create a DataFrame and export to CSV
df = pd.DataFrame(release_data)
df.to_csv('releases_report.csv', index=False)
print('Release data has been compiled and saved to releases_report.csv')`
有没有办法实现这一点,或者我应该悲伤地采用手动方式?
您可以使用 Builds - Get Build Changes 来获取与构建相关的更改。
响应示例:
{
"count": 2,
"value": [
{
"id": "b24228423d9b1eb7bxxxxxxxxxxe1f51de231925",
"message": "Linux build agent: upgraded tools in Docker image",
"type": "TfsGit",
"author": {
"displayName": "Bruce Wayne",
"_links": {
"avatar": {
"href": "https://dev.azure.com/myproject/_apis/GraphProfile/MemberAvatars/aad.xxx"
}
},
"id": "[email protected]",
"uniqueName": "[email protected]"
},
"timestamp": "2024-07-23T07:36:30Z",
"location": "https://dev.azure.com/myproject/_apis/git/repositories/xxxxxx-xxxx-xxxx-xxxx-xxxxxx/commits/b24228423d9b1xxxxxxxxxx5ea4e1f51de231925",
"displayUri": "https://dev.azure.com/myproject/606bb49f-3967-4c6a-b85a-45cab2dc2213/_git/xxxxxx-xxxx-xxxx-xxxx-xxxxxx/commit/b24228423d9b1xxxxxxxxxx5ea4e1f51de231925",
"pusher": "7af0d439-aa9b-40cf-93a4-fb725b0694a7"
},
{
"id": "c548c94axxxxxxxxxxa22f42761fbd0e0dca8241",
"message": "Removed unused yaml pipeline",
"type": "TfsGit",
"author": {
"displayName": "Bruce Wayne",
"_links": {
"avatar": {
"href": "https://dev.azure.com/myproject/_apis/GraphProfile/MemberAvatars/aad.xxx"
}
},
"id": "[email protected]",
"uniqueName": "[email protected]"
},
"timestamp": "2024-07-23T07:34:04Z",
"location": "https://dev.azure.com/myproject/_apis/git/repositories/xxxxxx-xxxx-xxxx-xxxx-xxxxxx/commits/c548c94a98213747a1axxxxxxxxxxd0e0dca8241",
"displayUri": "https://dev.azure.com/myproject/606bb49f-3967-4c6a-b85a-45cab2dc2213/_git/xxxxxx-xxxx-xxxx-xxxx-xxxxxx/commit/c548c94a98213747a1axxxxxxxxxxd0e0dca8241",
"pusher": "xxxxxxxxxx-aa9b-40cf-xxxx-fb725b0694a7"
}
]
}