Skip to content

Commit

Permalink
Release v1.10.3 - Editable events, frontend improvements. (#483)
Browse files Browse the repository at this point in the history
* Release v1.10.3 - Editable events, frontend improvements.

* Remove testing params.
  • Loading branch information
echoboomer committed Mar 11, 2024
1 parent 9155bf0 commit 37d78f7
Show file tree
Hide file tree
Showing 27 changed files with 16,009 additions and 12,152 deletions.
75 changes: 64 additions & 11 deletions backend/bot/api/routes/incident.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import config

from bot.api.routes.auth import api_key_required
Expand Down Expand Up @@ -166,9 +167,12 @@ def post_incident_ext():
)


@incidentrt.route("/incident/<incident_id>/audit", methods=["GET", "DELETE"])
@incidentrt.route(
"/incident/<incident_id>/audit",
methods=["GET", "DELETE", "PATCH", "POST", "OPTIONS"],
)
@jwt_required()
def get_incident_audit_log(incident_id):
def get_delete_post_incident_audit_log(incident_id):
if request.method == "GET":
try:
audit_logs = log.read(incident_id)
Expand All @@ -188,8 +192,8 @@ def get_incident_audit_log(incident_id):
try:
success, error = log.delete(
incident_id=incident_id,
id=request_data["id"],
log=request_data["log"],
ts=request_data["ts"],
)
if success:
return (
Expand All @@ -209,6 +213,51 @@ def get_incident_audit_log(incident_id):
500,
{"ContentType": "application/json"},
)
elif request.method == "POST":
request_data = request.json
try:
audit_logs = log.write(
incident_id=incident_id,
event=request_data["event"],
ts=request_data["timestamp"],
user=request_data["user"],
)
return (
jsonify({"success": True}),
200,
{"ContentType": "application/json"},
)
except Exception as error:
return (
jsonify({"error": str(error)}),
500,
{"ContentType": "application/json"},
)
elif request.method == "PATCH":
request_data = request.json
try:
create, msg = log.edit(
incident_id=incident_id,
id=request_data["id"],
new_log=request_data["event"],
)
if not create:
return (
jsonify({"error": str(msg)}),
500,
{"ContentType": "application/json"},
)
return (
jsonify({"success": True}),
200,
{"ContentType": "application/json"},
)
except Exception as error:
return (
jsonify({"error": str(error)}),
500,
{"ContentType": "application/json"},
)


@incidentrt.route("/incident/<incident_id>/pinned", methods=["GET"])
Expand Down Expand Up @@ -241,29 +290,29 @@ def get_incident_pinned_items(incident_id):


@incidentrt.route(
"/incident/<incident_id>/pinned/<id>", methods=["GET", "DELETE"]
"/incident/<incident_id>/pinned/<id>", methods=["GET", "PATCH", "DELETE"]
)
@jwt_required()
def get_delete_item_by_id(incident_id, id):
def get_patch_delete_item_by_id(id):
try:
img = Session.query(IncidentLogging).filter_by(id=id).first()
obj = Session.query(IncidentLogging).filter_by(id=id).first()
match request.method:
case "GET":
if not img:
if not obj:
return (
jsonify({"error": "object not found"}),
500,
{"ContentType": "application/json"},
)
return (
Response(
img.img,
mimetype=img.mimetype,
obj.img,
mimetype=obj.mimetype,
),
200,
)
case "DELETE":
Session.delete(img)
Session.delete(obj)
Session.commit()
return (
jsonify({"success": True}),
Expand Down Expand Up @@ -293,7 +342,11 @@ def post_set_incident_role(incident_id):
bp_message_ts=request_data["bp_message_ts"],
user=request_data["user"],
)
actions.assign_role(web_data=inc_request_data, request_origin="web")
asyncio.run(
actions.assign_role(
web_data=inc_request_data, request_origin="web"
)
)
return (
jsonify({"success": True}),
200,
Expand Down
86 changes: 82 additions & 4 deletions backend/bot/audit/log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from bot.models.pg import AuditLog, Session
from bot.shared import tools
from bot.slack.client import get_user_name
Expand All @@ -9,8 +11,8 @@

def delete(
incident_id: str,
id: str,
log: str,
ts: str,
database_session: scoped_session = Session,
) -> Tuple[bool, str]:
"""
Expand All @@ -29,7 +31,7 @@ def delete(
.one()
)
for log_obj in existing_row.data:
if log in log_obj.values() and ts in log_obj.values():
if log in log_obj.values() and id in log_obj.values():
found = True
break
else:
Expand All @@ -42,11 +44,12 @@ def delete(
data=[
i
for i in existing_row.data
if not (log in i.values() and ts in i.values())
if not (log in i.values() and id in i.values())
]
)
)
database_session.commit()

return True, "removed log entry"
else:
return False, "log entry not found"
Expand All @@ -67,6 +70,80 @@ def delete(
database_session.remove()


def edit(
incident_id: str,
id: str,
new_log: str,
database_session: scoped_session = Session,
):
"""
Update an audit log for an incident
"""
try:
if (
database_session.query(AuditLog)
.filter_by(incident_id=incident_id)
.all()
):
try:
existing_row = (
database_session.query(AuditLog)
.filter_by(incident_id=incident_id)
.one()
)

for log_obj in existing_row.data:
if id in log_obj.values():
found = True
break
else:
found = False
if found:
index = next(
(
index
for (index, d) in enumerate(existing_row.data)
if d["id"] == id
),
None,
)

if not index:
return False, "could not find log entry in list"

# Replace log
existing_data = existing_row.data
existing_item = existing_row.data[index]
existing_item["log"] = new_log
existing_data[index] = existing_item

database_session.execute(
update(AuditLog)
.where(AuditLog.incident_id == incident_id)
.values(data=existing_data)
)
database_session.commit()

logger.info("edited event {incident_id}/{id}")

return True, "edited event"
else:
return False, "log entry not found"
except Exception as error:
logger.error(
f"Audit log row lookup failed for incident {incident_id}: {error}"
)
else:
logger.warning(f"No audit log record for {incident_id}")
except Exception as error:
logger.error(
f"Audit log row lookup failed for incident {incident_id}: {error}"
)
finally:
database_session.close()
database_session.remove()


def read(
incident_id: str, database_session: scoped_session = Session
) -> List[Dict]:
Expand Down Expand Up @@ -112,7 +189,7 @@ def write(
"""
Write an audit log for an incident
Logs are appended to a JSON list
Logs are appended to a JSON list and identified by uuid
"""
try:
# Create the row if it doesn't exist
Expand All @@ -137,6 +214,7 @@ def write(
data = existing_row.data
data.append(
{
"id": str(uuid.uuid4()),
"log": event,
"user": get_user_name(user),
"content": content,
Expand Down
20 changes: 11 additions & 9 deletions backend/bot/incident/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,17 @@ async def set_status(
status=action_value,
severity=incident_data.severity,
conference_bridge=incident_data.conference_bridge,
postmortem_link=postmortem_link
if action_value == "resolved"
and ("atlassian" in config.active.integrations)
and (
config.active.integrations.get("atlassian")
.get("confluence")
.get("auto_create_postmortem")
)
else None,
postmortem_link=(
postmortem_link
if action_value == "resolved"
and ("atlassian" in config.active.integrations)
and (
config.active.integrations.get("atlassian")
.get("confluence")
.get("auto_create_postmortem")
)
else None
),
),
text="",
)
Expand Down
Loading

0 comments on commit 37d78f7

Please sign in to comment.