I'm trying to filter my project's jobs to return trace from successful jobs only and compare with current trace. Following is my code:
response = requests.get(GITLAB_PROJECT_BASE_URL + '/pipeline_schedules/' + str(PIPELINE_SCHEDULE_ID) + '/pipelines', headers=headers)
successful_pipeline_ids = list(filter(lambda x: x['status'] == 'success', json.loads(response.text)))
print(successful_pipeline_ids)
pipeline_id = successful_pipeline_ids[-1]['id']
response = requests.get(GITLAB_PROJECT_BASE_URL + '/pipelines/{}/jobs'.format(pipeline_id), headers=headers)
last_pipeline_job_id = json.loads(response.text)[0]['id']
log_file = str(last_pipeline_job_id) + ".trace"
download_file(GITLAB_PROJECT_BASE_URL + '/jobs/{}/trace'.format(last_pipeline_job_id), log_file)
last_pipeline_job_response_time = get_response_time_trph_from_log(log_file)
get_diff_with_last_pipeline(last_pipeline_job_response_time)
Please help find my issue
Expected: Trace returned for last successful job
Actual: Trace returned for last any job
The issue is you did not filter the jobs, you only filtrered the pipelines by success
status. You are looking for successful jobs in a succesful pipeline, so two filters are needed.
In these lines:
pipeline_id = successful_pipeline_ids[-1]['id']
response = requests.get(GITLAB_PROJECT_BASE_URL + '/pipelines/{}/jobs'.format(pipeline_id), headers=headers)
last_pipeline_job_id = json.loads(response.text)[0]['id']
last_pipeline_job_id
is getting the first job
from all the jobs under a successful pipeline, not just successful ones. You should also apply another filter:
response = requests.get(GITLAB_PROJECT_BASE_URL + '/pipelines/{}/jobs'.format(pipeline_id), headers=headers)
successful_pipeline_jobs = list(filter(lambda x: x['status'] == 'success', json.loads(response.text)))
last_pipeline_job_id = successful_pipeline_jobs[-1]['id']