Created
September 5, 2023 04:27
-
-
Save punitganshani/2a025255fe37f3ab79ccbc9fc973d338 to your computer and use it in GitHub Desktop.
Azure DevOps Packages List in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import base64 | |
import json | |
import pandas as pd | |
# Retrieve variables from system environment variables | |
organization = os.environ.get("AZURE_DEVOPS_ORG_NAME") | |
project = os.environ.get("AZURE_DEVOPS_PROJECT_NAME") | |
feed_id = os.environ.get("AZURE_DEVOPS_FEED_ID") | |
personal_access_token = os.environ.get("AZURE_DEVOPS_PAT") | |
pat_base64 = base64.b64encode(f":{personal_access_token}".encode("ascii")).decode() | |
if not organization or not feed_id or not personal_access_token: | |
raise ValueError("Please set the AZURE_DEVOPS_ORGANIZATION, AZURE_DEVOPS_FEED_ID, and AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN environment variables.") | |
# Define the base URL for Azure DevOps REST API | |
base_url = f"https://feeds.dev.azure.com/{organization}/{project}/_apis" | |
# Define the API version | |
api_version = "7.0" | |
# Define the headers for authentication and content type | |
headers = { | |
"Authorization": f"Basic {pat_base64}", | |
"Content-Type": "application/json" | |
} | |
def get_feeds(): | |
# Define the Azure DevOps authentication header | |
AzureDevOpsAuthenicationHeader = { | |
"Authorization": "Basic " + pat_base64, | |
"Content-Type": "application/json" | |
} | |
# Define the base URL for feeds | |
UriRootFeeds = f"https://feeds.dev.azure.com/{organization}/" | |
# Define the URL to get feeds | |
UriFeeds = UriRootFeeds + f"_apis/packaging/feeds?api-version={api_version}" | |
# Make an HTTP GET request to fetch feeds | |
FeedResult = requests.get(UriFeeds, headers=AzureDevOpsAuthenicationHeader).json() | |
# Create lists to store data | |
feed_data = [] | |
# Iterate over each feed | |
for feed in FeedResult["value"]: | |
print(feed) | |
project_name = "" # for organization feeds, we dont need project name | |
# Define the URL to get feed packages | |
UriFeedPackages = f"{UriRootFeeds}{project_name}/_apis/packaging/Feeds/{feed['id']}/packages?api-version={api_version}" | |
# Make an HTTP GET request to fetch feed packages | |
FeedPackageResult = requests.get(UriFeedPackages, headers=AzureDevOpsAuthenicationHeader).json() | |
# Iterate over each feed package | |
for feedpackage in FeedPackageResult["value"]: | |
feedpackage_id = feedpackage["id"] | |
# Define the URL to get feed package versions | |
UriFeedPackageVersion = f"{UriRootFeeds}{project_name}/_apis/packaging/Feeds/{feed['id']}/Packages/{feedpackage_id}/versions?api-version={api_version}" | |
# Make an HTTP GET request to fetch feed package versions | |
FeedPackageVersionResult = requests.get(UriFeedPackageVersion, headers=AzureDevOpsAuthenicationHeader).json() | |
# Iterate over each feed package version | |
for feedpackageversion in FeedPackageVersionResult["value"]: | |
packageVersionIds = {"packageVersionIds": [feedpackageversion["id"]]} | |
BodyPackageVersionIds = json.dumps(packageVersionIds) | |
# Define the URL to get feed package version usage | |
UriFeedPackageVersionUsage = f"{UriRootFeeds}{project_name}/_apis/packaging/Feeds/{feed['id']}/Packages/{feedpackage_id}/versionmetricsbatch?api-version={api_version}" | |
# Make an HTTP POST request to fetch feed package version usage | |
FeedPackageVersionUsageResult = requests.post(UriFeedPackageVersionUsage, headers=AzureDevOpsAuthenicationHeader, data=BodyPackageVersionIds).json() | |
downloadCount = 0 | |
downloadUniqueUsers = 0 | |
feedPackageSource = "This feed" | |
# Check if there is usage data | |
if FeedPackageVersionUsageResult.get("value"): | |
for feedpackageversionusage in FeedPackageVersionUsageResult["value"]: | |
downloadCount = feedpackageversionusage.get("downloadCount", 0) | |
downloadUniqueUsers = feedpackageversionusage.get("downloadUniqueUsers", 0) | |
# Check if there is a source chain | |
if feedpackageversion.get("sourceChain"): | |
feedPackageSource = feedpackageversion["sourceChain"][0]["name"] | |
# Print or process the data as needed | |
print(f"Feed Name: {feed['name']}") | |
print(f"Feed Package Name: {feedpackage['name']}") | |
print(f"Feed Package Version: {feedpackageversion['version']}") | |
print(f"Feed Package Source: {feedPackageSource}") | |
print(f"Download Count: {downloadCount}") | |
print(f"Download Unique Users: {downloadUniqueUsers}") | |
print("=" * 50) | |
# Append data to the feed_data list | |
feed_data.append({ | |
"Feed Name": feed['name'], | |
"Feed Package Name": feedpackage['name'], | |
"Feed Package Version": feedpackageversion['version'], | |
"Feed Package Source": feedPackageSource, | |
"Download Count": downloadCount, | |
"Download Unique Users": downloadUniqueUsers | |
}) | |
# Create a DataFrame from the collected data | |
df = pd.DataFrame(feed_data) | |
# Generate a CSV file from the DataFrame | |
df.to_csv('azure_devops_feed_data.csv', index=False) | |
get_feeds() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment