azureazure-devopsazure-api-managementazure-rest-apiazure-releases

How to get Project Name, Release Name, Release Date, Release Changes (Commit Message) From Azure Release APIs


I'm currently working on task the which is the goal from it is to give a table with the below data from all Pipeline releases on our Azure Dashboard from date 01/01/2024 to 01/06/2024.

I though that I'll never do this manually and I started searching for an automated way to this.

I created a python script to fetch the releases from Azure API and https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1-preview.8

which is retrieving the Project Name, Release Name, Release Date, but I cannot find any related info about the commit message associated to the Release.

I also Asked ChatGPT to help me on this, it gives me an old API as I think because it returns 404.

When checked Azure APIs I found an API which is retrieving all commits, not the one associated to the build.

I tried this script

Here is the script.

`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')`

is there anyway to achieve this, or I should sadly go to the manual way ?


Solution

  • You can use Builds - Get Build Changes to get the changes associated with a build.

    Example response:

    {
      "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": "batman@mail.com",
            "uniqueName": "batman@mail.com"
          },
          "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": "batman@mail.com",
            "uniqueName": "batman@mail.com"
          },
          "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"
        }
      ]
    }