Skip to content

Commit

Permalink
Only report on jobs that were triggered by automation (#4213)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegreyd authored Aug 2, 2024
1 parent 4dd1ad7 commit b7964f8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions scheduled-jobs/scanning/art-notify/art-notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
JENKINS_URL = os.getenv('JENKINS_URL').rstrip('/')

RELEASE_ARTIST_HANDLE = 'release-artists'
ART_BOT_JENKINS_USERID = 'openshift-art' # userId of our automation account in jenkins which triggers builds
FAILED_JOB_HOURS = 8 # Last x hours that we consider for our failed job search


def get_failed_jobs_text():
projects = ["aos-cd-builds", "scheduled-builds"]
failed_jobs = []
query = "?tree=jobs[name,url,builds[number,result,timestamp]]" # status,displayName are also useful
# API reference
# for a job: <jenkins_url>/job/<project>/job/<job_name>/api/json?pretty=true
# for a build: <jenkins_url>/job/<project>/job/<job_name>/<build_number>/api/json?pretty=true
query = "?tree=jobs[name,url,builds[number,result,timestamp,actions[causes[userId]]]]"
now = datetime.now(timezone.utc)
for project in projects:
api_url = f"{JENKINS_URL}/job/{project}/api/json"
Expand All @@ -44,9 +49,15 @@ def get_failed_jobs_text():
td = now - dt
hours = td.seconds // 3600
minutes = (td.seconds // 60) % 60
if not (td.days == 0 and hours < 3):
if not (td.days == 0 and hours < FAILED_JOB_HOURS):
continue
if build['result'] == 'FAILURE':
# Filter all jobs that were not triggered by our automation account
# We do not want to report on these since they are manually triggered and
# would be monitored by whoever triggered them
user_id = next(b['causes'][0]['userId'] for b in build['actions'] if 'causes' in b)
if user_id != ART_BOT_JENKINS_USERID:
continue
failed_job_ids.append(build['number'])
if len(failed_job_ids) > 0:
failed_jobs.append((unquote(job_name), failed_job_ids, job['url']))
Expand All @@ -73,7 +84,7 @@ def slack_link(job_name, job_url, job_id=None, text=None):
text += f"[{slack_link(job_name, job_url, job_id=failed_job_ids[i], text=i+1)}] "
failed_jobs_list.append(text)
failed_jobs_list = "\n".join(failed_jobs_list)
failed_jobs_text = f"Failed builds in last 3 hours: \n{failed_jobs_list}"
failed_jobs_text = f"Failed builds in last `{FAILED_JOB_HOURS}` hours triggered by `{ART_BOT_JENKINS_USERID}`: \n{failed_jobs_list}"
return failed_jobs_text
return ''

Expand Down

0 comments on commit b7964f8

Please sign in to comment.