Skip to content

Commit

Permalink
Fix timestamp parsing (#47)
Browse files Browse the repository at this point in the history
* use gitlab-supplied format instead of ISO 8601

* change gitlab timestamp format to be consistent with webhook response in tests
  • Loading branch information
cmelone authored Jun 10, 2024
1 parent 81712af commit 0dcac43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 5 additions & 3 deletions gantry/models/job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from datetime import datetime

from gantry.util.time import webhook_timestamp


class Job:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gantry/tests/defs/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"build_status": "success",
"build_name": "[email protected] /jcchwaj %[email protected] 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"},
}
Expand Down
15 changes: 15 additions & 0 deletions gantry/util/time.py
Original file line number Diff line number Diff line change
@@ -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()
)

0 comments on commit 0dcac43

Please sign in to comment.