Skip to content

Commit b35382a

Browse files
authored
refactor(content-health-monitor): include user's name in About and Log error (#257)
* Add new function to get the current user's full name * Use the user's name in the About box * Include current user's name in About and Log error messages * Remove unnecessary logic related to logs * Remove unused variable
1 parent b48fc76 commit b35382a

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

extensions/content-health-monitor/content-health-monitor.qmd

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ if not show_instructions:
4545
# Always record the current time for reporting purposes
4646
check_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
4747
48+
# Initialize current user name with a default for display
49+
current_user_name = "the publisher" # Default, used if no name is available
50+
51+
# Get current user's full name if client is available
52+
if not show_instructions and client:
53+
try:
54+
user_name = get_current_user_full_name(client)
55+
if user_name: # Only update if we got a valid name
56+
current_user_name = user_name
57+
except Exception as e:
58+
print(f"Warning: Could not get current user name: {str(e)}")
59+
4860
# Only proceed with content validation if we have all requirements
4961
# (show_instructions=False already tells us env vars are set and client was initialized)
5062
if not show_instructions:
@@ -71,12 +83,13 @@ if not show_instructions:
7183
7284
# ------ DISPLAY SECTION ------ #
7385
# Create the about content text
74-
about_content = """<div>
75-
This report uses the publisher's API key to monitor a single piece of content. It checks whether the content is
86+
about_content = f"""<div>
87+
This report uses {current_user_name}'s API key to monitor a single piece of content. It checks whether the content is
7688
reachable, but does not validate its functionality. When scheduled to run regularly, it will send an email alert if the
7789
content becomes unreachable.
7890
</div>"""
7991
92+
8093
# Prepare instructions HTML if needed
8194
if show_instructions:
8295
instructions_html = "".join(f"<div style='margin-bottom: 10px;'>{instruction}</div>" for instruction in instructions)
@@ -87,7 +100,7 @@ else:
87100
html_components = {
88101
'instructions': create_instructions_box(instructions_html) if show_instructions else None,
89102
'error': create_error_box(error_guid, error_message) if show_error else None,
90-
'report': create_report_display(content_result, check_time) if content_result and not has_error(content_result) else None,
103+
'report': create_report_display(content_result, check_time, current_user_name) if content_result and not has_error(content_result) else None,
91104
'no_results': create_no_results_box() if not (show_instructions or show_error or
92105
(content_result and not has_error(content_result))) else None
93106
}

extensions/content-health-monitor/content_health_utils.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ def get_user(client, user_guid):
118118
error_message = format_error_message(e)
119119
raise RuntimeError(f"Error getting user: {error_message}")
120120

121+
def get_current_user_full_name(client):
122+
"""
123+
Get the full name of the current user from the Connect API
124+
125+
Args:
126+
client: The Connect client instance
127+
128+
Returns:
129+
str: The full name of the current user or "Unknown" if not available
130+
"""
131+
try:
132+
# Get the current user information
133+
current_user = client.me
134+
135+
# Extract first and last name
136+
first_name = current_user.get("first_name", "")
137+
last_name = current_user.get("last_name", "")
138+
139+
# Combine into full name
140+
full_name = f"{first_name} {last_name}".strip()
141+
142+
# Return username if full name is empty
143+
if not full_name:
144+
return current_user.get("username", "Unknown")
145+
146+
return full_name
147+
except Exception as e:
148+
# Handle any errors gracefully
149+
error_message = format_error_message(e)
150+
print(f"Warning: Could not retrieve current user: {error_message}")
151+
return "Unknown"
152+
121153
# Function to validate content health (simple HTTP 200 check)
122154
def validate(client, guid, connect_server, api_key):
123155
# Get content details
@@ -313,7 +345,7 @@ def create_instructions_box(instructions_html_content):
313345
"""
314346

315347
# Function to create the report display for a result
316-
def create_report_display(result_data, check_time_value):
348+
def create_report_display(result_data, check_time_value, current_user_name):
317349
if result_data is None:
318350
return None
319351

@@ -338,15 +370,12 @@ def create_report_display(result_data, check_time_value):
338370
status_colors = CSS_COLORS["fail"]
339371
status_icon = "❌" # X mark
340372

341-
# Get HTTP Code
342-
http_code = result_data.get('http_code', '')
343-
344373
# Format logs link if available
345374
logs_url = result_data.get('logs_url', '')
346375
if logs_url:
347376
logs_display = f"<a href='{logs_url}' target='_blank' style='text-decoration:none;'>📋 View Logs</a>"
348377
else:
349-
logs_display = "No logs available — only visible to the content owner and collaborators."
378+
logs_display = f"Log access is restricted for {current_user_name}. Logs are only available to the content owner and collaborators."
350379

351380
# Format owner information
352381
owner_name = result_data.get('owner_name', 'Unknown')

0 commit comments

Comments
 (0)