Πώς να χρησιμοποιήσετε το Azure DevOps API για να λάβετε τον συνολικό χρόνο κατασκευής (σε λεπτά) για ένα Build Agent Pool. Εάν πρέπει να επιστρέψετε από τον αγορασμένο πράκτορα στη δωρεάν έκδοση, αυτός ο οδηγός θα σας βοηθήσει.
Το Azure DevOps είναι μια δημοφιλής πλατφόρμα για τη διαχείριση διαδικασιών ανάπτυξης λογισμικού. Με το REST API του, Οι προγραμματιστές μπορούν να αλληλεπιδράσουν με την πλατφόρμα μέσω προγραμματισμού και να αυτοματοποιήσουν εργασίες όπως η διαχείριση έκδοσης και έκδοσης. Σε αυτό το σεμινάριο, θα δείξουμε πώς να χρησιμοποιείτε το Azure DevOps API για να λαμβάνετε τον συνολικό χρόνο κατασκευής (σε λεπτά) για μια ομάδα πρακτόρων δόμησης εντός ενός καθορισμένου εύρους ημερομηνιών. Θα παρέχουμε οδηγίες βήμα προς βήμα και θα χρησιμοποιήσουμε την Python για να υποβάλουμε αιτήματα API.
Βήματα:
- Get an Azure DevOps Personal Access Token (ΕΛΑΦΡΟ ΚΤΥΠΗΜΑ) Πριν ξεκινήσουμε, πρέπει να αποκτήσουμε ένα PAT Azure DevOps, το οποίο θα χρησιμοποιήσουμε για τον έλεγχο ταυτότητας των αιτημάτων μας API. Για να δημιουργήσετε ένα PAT, μεταβείτε στις ρυθμίσεις του οργανισμού Azure DevOps και κάντε κλικ στο “Personal access tokens”. Follow the instructions to create a new token with the “Χτίζω (read)” scope.
- Set up a Python environment We will be using Python to make our API requests. If you don’t have Python installed, you can download it from the official website (https://www.python.org/downloads/). We also recommend using a virtual environment to manage your Python dependencies. To set up a virtual environment, run the following commands in your terminal:
pip install virtualenv
virtualenv venv
source venv/bin/activate
- Install the requests library We will be using the
requests
library to make our API requests. To install it, εκτελέστε την παρακάτω εντολή:
pip install requests
- Define the API endpoint The API endpoint we will be using is:
https://{username}:{pat}@dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0
Αντικαθιστώ {username}
, {pat}
,{organization}
και {project}
with your actual organization and project names.
- Make the API request To get the total build time for a build agent pool within a specified date range, we need to use the
minTime
καιmaxTime
query parameters in our API request. We also need to filter the build results by the build agent pool. Here’s an example Python script that makes the API request and calculates the total build time (σε λεπτά):
pip install python-dateutil requests
import requests
from dateutil.parser import parse
# Set up variables for the API request
organization = "<Organization Name>"
project = "Project Name"
queue_id = "9"
min_time = "2023-04-01"
max_time = "2023-04-05"
# you can leave username as user
username = "user"
pat = "<PAT>"
# Construct the API URL
url = f"https://{username}:{pat}@dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0&minTime={min_time}&maxTime={max_time}&$filter=queue.id eq {queue_id}&$orderby=startTime asc"
# Send the API request
response = requests.get(url)
# Parse the response JSON
json_response = response.json()
# Retrieve the count of build runs
count = json_response["count"]
# Calculate the total build time in seconds
total_build_time = 0
for build in json_response["value"]:
start_time = parse(build["startTime"])
finish_time = parse(build["finishTime"])
duration = finish_time - start_time
total_build_time += duration.total_seconds()
# Convert the total build time to minutes
total_build_time_minutes = total_build_time / 60
print(f"Number of build runs: {count}")
print(f"Total build time: {total_build_time_minutes:.2f} minutes")
Output example:
Number of build runs: 35
Total build time: 122.19 minutes