1#!/usr/bin/env python3 2# Copyright © 2023 Collabora Ltd. 3# Authors: 4# Helen Koike <[email protected]> 5# 6# For the dependencies, see the requirements.txt 7# SPDX-License-Identifier: MIT 8 9 10import argparse 11import gitlab 12import re 13import os 14import pytz 15import traceback 16from datetime import datetime, timedelta 17from gitlab_common import ( 18 read_token, 19 GITLAB_URL, 20 get_gitlab_pipeline_from_url, 21) 22from ci_gantt_chart import generate_gantt_chart 23 24MARGE_USER_ID = 9716 # Marge 25 26LAST_MARGE_EVENT_FILE = os.path.expanduser("~/.config/last_marge_event") 27 28 29def read_last_event_date_from_file(): 30 try: 31 with open(LAST_MARGE_EVENT_FILE, "r") as f: 32 last_event_date = f.read().strip() 33 except FileNotFoundError: 34 # 3 days ago 35 last_event_date = (datetime.now() - timedelta(days=3)).isoformat() 36 return last_event_date 37 38 39def pretty_time(time_str): 40 """Pretty print time""" 41 local_timezone = datetime.now().astimezone().tzinfo 42 43 time_d = datetime.fromisoformat(time_str.replace("Z", "+00:00")).astimezone( 44 local_timezone 45 ) 46 return f'{time_str} ({time_d.strftime("%d %b %Y %Hh%Mm%Ss")} {local_timezone})' 47 48 49def compose_message(file_name, attachment_url): 50 return f""" 51Here is the Gantt chart for the referred pipeline, I hope it helps (tip: click on the "Pan" button on the top right bar): 52 53[{file_name}]({attachment_url}) 54 55<details> 56<summary>more info</summary> 57 58This message was generated by the ci_post_gantt.py script, which is running on a server at Collabora. 59</details> 60""" 61 62 63def gitlab_upload_file_get_url(gl, project_id, filepath): 64 project = gl.projects.get(project_id) 65 uploaded_file = project.upload(filepath, filepath=filepath) 66 return uploaded_file["url"] 67 68 69def gitlab_post_reply_to_note(gl, event, reply_message): 70 """ 71 Post a reply to a note in thread based on a GitLab event. 72 73 :param gl: The GitLab connection instance. 74 :param event: The event object containing the note details. 75 :param reply_message: The reply message. 76 """ 77 try: 78 note_id = event.target_id 79 merge_request_iid = event.note["noteable_iid"] 80 81 project = gl.projects.get(event.project_id) 82 merge_request = project.mergerequests.get(merge_request_iid) 83 84 # Find the discussion to which the note belongs 85 discussions = merge_request.discussions.list(iterator=True) 86 target_discussion = next( 87 ( 88 d 89 for d in discussions 90 if any(n["id"] == note_id for n in d.attributes["notes"]) 91 ), 92 None, 93 ) 94 95 if target_discussion is None: 96 raise ValueError("Discussion for the note not found.") 97 98 # Add a reply to the discussion 99 reply = target_discussion.notes.create({"body": reply_message}) 100 return reply 101 102 except gitlab.exceptions.GitlabError as e: 103 print(f"Failed to post a reply to '{event.note['body']}': {e}") 104 return None 105 106 107def parse_args() -> None: 108 parser = argparse.ArgumentParser(description="Monitor rejected pipelines by Marge.") 109 parser.add_argument( 110 "--token", 111 metavar="token", 112 help="force GitLab token, otherwise it's read from ~/.config/gitlab-token", 113 ) 114 parser.add_argument( 115 "--since", 116 metavar="since", 117 help="consider only events after this date (ISO format), otherwise it's read from ~/.config/last_marge_event", 118 ) 119 return parser.parse_args() 120 121 122if __name__ == "__main__": 123 args = parse_args() 124 125 token = read_token(args.token) 126 127 gl = gitlab.Gitlab(url=GITLAB_URL, private_token=token, retry_transient_errors=True) 128 129 user = gl.users.get(MARGE_USER_ID) 130 last_event_at = args.since if args.since else read_last_event_date_from_file() 131 132 print(f"Retrieving Marge messages since {pretty_time(last_event_at)}\n") 133 134 # the "after" only considers the "2023-10-24" part, it doesn't consider the time 135 events = user.events.list( 136 all=True, 137 target_type="note", 138 after=(datetime.now() - timedelta(days=3)).isoformat(), 139 sort="asc", 140 ) 141 142 last_event_at_date = datetime.fromisoformat( 143 last_event_at.replace("Z", "+00:00") 144 ).replace(tzinfo=pytz.UTC) 145 146 for event in events: 147 created_at_date = datetime.fromisoformat( 148 event.created_at.replace("Z", "+00:00") 149 ).replace(tzinfo=pytz.UTC) 150 if created_at_date <= last_event_at_date: 151 continue 152 last_event_at = event.created_at 153 154 match = re.search(r"https://[^ ]+", event.note["body"]) 155 if match: 156 try: 157 print("Found message:", event.note["body"]) 158 pipeline_url = match.group(0)[:-1] 159 pipeline, _ = get_gitlab_pipeline_from_url(gl, pipeline_url) 160 print("Generating gantt chart...") 161 fig = generate_gantt_chart(pipeline) 162 file_name = "Gantt.html" 163 fig.write_html(file_name) 164 print("Uploading gantt file...") 165 file_url = gitlab_upload_file_get_url(gl, event.project_id, file_name) 166 print("Posting reply ...\n") 167 message = compose_message(file_name, file_url) 168 gitlab_post_reply_to_note(gl, event, message) 169 except Exception as e: 170 print(f"Failed to generate gantt chart, not posting reply.{e}") 171 traceback.print_exc() 172 173 if not args.since: 174 print( 175 f"Updating last event date to {pretty_time(last_event_at)} on {LAST_MARGE_EVENT_FILE}\n" 176 ) 177 with open(LAST_MARGE_EVENT_FILE, "w") as f: 178 f.write(last_event_at) 179