Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/github_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def get_uuid_from_string(input_string):
return uuid.UUID(hash_value[:32]).hex


def _get_author_from_run(runs):
head_commit = runs.get("head_commit") or {}
author = head_commit.get("author")
if author:
return author

actor = runs.get("actor") or runs.get("triggering_actor") or {}
user = {}
if actor.get("id") is not None:
user["id"] = str(actor["id"])
if actor.get("login"):
user["username"] = actor["login"]
return user


class GithubClient:
# This transform GH jobs conclusion keywords to Sentry performance status
github_status_trace_status = {"success": "ok", "failure": "internal_error"}
Expand Down Expand Up @@ -53,7 +68,7 @@ def _get_extra_metadata(self, job):
repo = runs["repository"]["full_name"]
meta = {
# "workflow_name": workflow["name"],
"author": runs["head_commit"]["author"],
"author": _get_author_from_run(runs),
# https://getsentry.atlassian.net/browse/TET-22
# Tags are not linkified externally, plain text data can be selected in browsers and opened
"data": {
Expand Down
28 changes: 28 additions & 0 deletions tests/test_github_sdk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import sys
from datetime import datetime
from unittest.mock import patch
Expand Down Expand Up @@ -110,6 +111,33 @@ def test_trace_generation_with_failing_steps(
assert trace["tags"]["event"] == "push"


@responses.activate
def test_trace_generation_without_head_commit_uses_actor(
failure_job,
failure_runs,
failure_workflow,
):
runs = copy.deepcopy(failure_runs)
runs["head_commit"] = None

responses.get(
failure_job["run_url"],
json=runs,
)
responses.get(
runs["workflow_url"],
json=failure_workflow,
)

client = GithubClient(dsn=DSN, token=TOKEN)
trace = client._generate_trace(failure_job)

assert trace["user"] == {
"id": str(runs["actor"]["id"]),
"username": runs["actor"]["login"],
}


@freeze_time()
@responses.activate
@patch("src.github_sdk.get_uuid")
Expand Down
Loading