From 0dcac4382c8befe302d2e55c61ff2594f87bf968 Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Mon, 10 Jun 2024 10:05:04 -0700 Subject: [PATCH] Fix timestamp parsing (#47) * use gitlab-supplied format instead of ISO 8601 * change gitlab timestamp format to be consistent with webhook response in tests --- gantry/models/job.py | 8 +++++--- gantry/tests/defs/collection.py | 4 ++-- gantry/util/time.py | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 gantry/util/time.py diff --git a/gantry/models/job.py b/gantry/models/job.py index 5dd8649..0d33013 100644 --- a/gantry/models/job.py +++ b/gantry/models/job.py @@ -1,5 +1,6 @@ import re -from datetime import datetime + +from gantry.util.time import webhook_timestamp class Job: @@ -17,9 +18,10 @@ def __init__( self.gl_id = gl_id # handle jobs that haven't started or finished if start: - self.start = datetime.fromisoformat(start).timestamp() + self.start = webhook_timestamp(start) if end: - self.end = datetime.fromisoformat(end).timestamp() + self.end = webhook_timestamp(end) + self.ref = ref @property diff --git a/gantry/tests/defs/collection.py b/gantry/tests/defs/collection.py index 92fcab8..a6326be 100644 --- a/gantry/tests/defs/collection.py +++ b/gantry/tests/defs/collection.py @@ -12,8 +12,8 @@ "build_status": "success", "build_name": "gmsh@4.8.4 /jcchwaj %gcc@11.4.0 arch=linux-ubuntu20.04-x86_64_v3 E4S", "build_id": 9892514, # not used in testing unless it already exists in the db - "build_started_at": "2024-01-24T17:24:06.000Z", - "build_finished_at": "2024-01-24T17:47:00.000Z", + "build_started_at": "2024-01-24 17:24:06 UTC", + "build_finished_at": "2024-01-24 17:47:00 UTC", "ref": "pr42264_bugfix/mathomp4/hdf5-appleclang15", "runner": {"description": "aws"}, } diff --git a/gantry/util/time.py b/gantry/util/time.py new file mode 100644 index 0000000..1989c79 --- /dev/null +++ b/gantry/util/time.py @@ -0,0 +1,15 @@ +import datetime + + +def webhook_timestamp(dt: str) -> float: + """Converts a gitlab webhook datetime to a unix timestamp.""" + # gitlab sends dates in 2021-02-23 02:41:37 UTC format + # documentation says they use iso 8601, but they don't consistently apply it + # https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#job-events + GITLAB_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S %Z" + # strptime doesn't tag with timezone by default + return ( + datetime.datetime.strptime(dt, GITLAB_DATETIME_FORMAT) + .replace(tzinfo=datetime.timezone.utc) + .timestamp() + )