Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug report: (codecov/shared) GitLab timestamps with timezone offsets not properly handled #597

Open
CerebralXor opened this issue Dec 1, 2024 · 0 comments

Comments

@CerebralXor
Copy link

CerebralXor commented Dec 1, 2024

Describe the bug

There appears to be a bug in the GitLab adapter at shared/torngit/gitlab.py, specifically in the get_commit method (and perhaps other methods and functions that consume timestamps from the GitLab API).

GitLab's EE/CE (i.e. self hosted) API documentation explicitly shows that timestamps include timezone offsets (e.g. "2021-09-20T09:06:12.300+03:00"): https://docs.gitlab.com/ee/api/commits.html

Codecov seems to ignore these offsets and treat the timestamps as UTC time. This leads to incorrect timestamps being stored and displayed by codecov, with the error being equal to the timezone offset of the original timestamp.

The issue impacts commit ordering and any features that rely on accurate commit timestamps. It also causes strange behaviour in the UI, for example if the UTC offset in the timestamp for the latest commit was +8 hours, the codecov UI will display "Last updated: In about 8 hours".

A local test patch confirms the issue can be fixed by properly handling the timezone conversion:

--- shared/torngit/gitlab.py   2024-10-01 17:45:38.000000000 +0000
+++ shared/torngit/gitlab.py   2024-12-01 02:54:56.222429098 +0000
@@ -1122,12 +1122,24 @@
                    name = authors[0]["name"]
                    break

+        # GitLab returns timestamps with timezone offsets (e.g. "2021-09-20T09:06:12.300+03:00")
+        # These need to be converted to UTC ISO 8601 format for consistency
+        from datetime import datetime
+        import pytz
+        # Parse the datetime string to a datetime object
+        local_dt = datetime.fromisoformat(res["committed_date"])
+        # Convert the local datetime to UTC
+        utc_dt = local_dt.astimezone(pytz.utc)
+        # Format the UTC datetime to ISO 8601 string
+        utc_iso8601 = utc_dt.isoformat().replace('+00:00', 'Z')
+        log.info(f'PATCH: The timestamp for commit {commit} from GitLab was {res["committed_date"]}, but we will convert it to {utc_iso8601}')
+
        return dict(
            author=dict(id=_id, username=username, email=email, name=name),
            message=res["message"],
            parents=res["parent_ids"],
            commitid=commit,
-            timestamp=res["committed_date"],
+            timestamp=utc_iso8601,
        )

    async def get_pull_request_commits(self, pullid, token=None):

Environment

I have GitLab community edition and codecov self-hosted images version 24.10.1 running on a kubernetes cluster.

To Reproduce

  • Use a GitLab project that is configured with CI/CD pipelines to push test coverage to codecov.
  • Make a commit using git CLI command on a system whose timezone is not UTC.
  • Push that commit to the GitLab EE or CE repository so the pipeline runs.
  • Query the commit information through GitLab's API directly to see the timezone offset in the timestamp:
    curl "https://<your_gitlab_host>/api/v4/projects/<project_id>/repository/commits/<commit_sha>"
  • Compare this with how the timestamp appears in CodeCov.
  • Notice the timestamp is off by the amount of the timezone offset.

Expected behaviour

The system should:

  • Parse the GitLab timestamp string, preserving the timezone information
  • Convert the datetime to UTC
  • Store/return the UTC timestamp in ISO 8601 format
  • Display consistent timestamps regardless of the original commit's timezone

Additional context

I found this issue with my own GitLab community edition self hosted instance, but I assume it also affects GitLab.com.

@covecod covecod bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Dec 1, 2024
@CerebralXor CerebralXor changed the title Bug Report Bug report: (codecov/shared) GitLab timestamps with timezone offsets not properly handled Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for: Product Owner
Development

No branches or pull requests

1 participant