Sign in
agent:

Get average length of job execution in jenkins for the given repo

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
import requests import json num_results = 10 # Fetch environment variables jenkins_username = getEnvVar('JENKINS_USERNAME') jenkins_api_token = getEnvVar('JENKINS_API_TOKEN') jenkins_url = getEnvVar('JENKINS_URL') # Print input parameters print(f"Job Name: {job_name}") print(f"Number of Results: {num_results}") # Jenkins API URL for job builds api_url = f"{jenkins_url}/job/{job_name}/api/json?tree=builds[number,duration,result]&depth=1&pretty=true" # Make the request to Jenkins API response = requests.get(api_url, auth=(jenkins_username, jenkins_api_token)) # Check if the request was successful if response.status_code != 200: raise Exception(f"Failed to fetch job details: {response.status_code} {response.text}") # Parse the JSON response job_data = response.json() # Extract the last 'num_results' builds builds = job_data.get('builds', [])[:num_results] # Calculate the average duration of successful builds total_duration = 0 successful_builds = 0 for build in builds: if build.get('result') == 'SUCCESS': total_duration += build.get('duration', 0) successful_builds += 1 if successful_builds == 0: average_execution_length = 0 else: average_execution_length = total_duration / successful_builds average_execution_length = int(average_execution_length/1000) # Print the output print(f"Average Execution Length: {average_execution_length}")
copied