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

chore(ci_visibility): don't log exception when codeowners can't be loaded #10423

6 changes: 4 additions & 2 deletions ddtrace/internal/ci_visibility/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ def set_codeowners_of(cls, location, span=None):
handles = cls._instance._codeowners.of(location)
if handles:
span.set_tag(test.CODEOWNERS, json.dumps(handles))
except KeyError:
log.debug("no matching codeowners for %s", location)
else:
log.debug("no matching codeowners for %s", location)
except: # noqa: E722
log.debug("Error setting codeowners for %s", location, exc_info=True)

@classmethod
def add_session(cls, session: CIVisibilitySession):
Expand Down
29 changes: 17 additions & 12 deletions ddtrace/internal/codeowners.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os
import re
from typing import List # noqa:F401
from typing import Optional # noqa:F401
from typing import Tuple # noqa:F401
from typing import List
from typing import Optional
from typing import Tuple


def path_to_regex(pattern):
# type: (str) -> re.Pattern
def path_to_regex(pattern: str) -> re.Pattern:
"""
source https://github.com/sbdchd/codeowners/blob/c95e13d384ac09cfa1c23be1a8601987f41968ea/codeowners/__init__.py

Expand Down Expand Up @@ -119,16 +118,20 @@ class Codeowners(object):
".gitlab/CODEOWNERS",
)

def __init__(self, path=None, cwd=None):
# type: (Optional[str], Optional[str]) -> None
def __init__(self, path: Optional[str] = None, cwd: Optional[str] = None):
"""Initialize Codeowners object.

:param path: path to CODEOWNERS file otherwise try to use any from known locations
"""
self.patterns: List[Tuple[re.Pattern, List[str]]] = []
self.path: Optional[str] = None

path = path or self.location(cwd)
if path is not None:
self.path = path # type: str
self.patterns = [] # type: List[Tuple[re.Pattern, List[str]]]

if path is None:
raise ValueError("CODEOWNERS file not found")

self.path = path
vitor-de-araujo marked this conversation as resolved.
Show resolved Hide resolved
self.parse()

def location(self, cwd: Optional[str] = None) -> Optional[str]:
Expand All @@ -140,9 +143,11 @@ def location(self, cwd: Optional[str] = None) -> Optional[str]:
return path
return None

def parse(self):
# type: () -> None
def parse(self) -> None:
"""Parse CODEOWNERS file and store the lines and regexes."""
if self.path is None:
return

with open(self.path) as f:
patterns = []
for line in f.readlines():
Expand Down
Loading