Skip to content

Commit

Permalink
[invoke] Add tasks to query CODEOWNERS files (#24112)
Browse files Browse the repository at this point in the history
* [invoke] Add tasks to use CODEOWNERS files

* Add unit tests

* Apply suggestions from code review

Co-authored-by: Nicolas Schweitzer <[email protected]>

---------

Co-authored-by: Nicolas Schweitzer <[email protected]>
  • Loading branch information
KSerrania and chouetz authored Apr 5, 2024
1 parent def5ef6 commit 182707b
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 11 deletions.
2 changes: 2 additions & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
msi,
new_e2e_tests,
notify,
owners,
package,
pipeline,
process_agent,
Expand Down Expand Up @@ -155,6 +156,7 @@
ns.add_collection(kmt)
ns.add_collection(diff)
ns.add_collection(updater)
ns.add_collection(owners)
ns.add_collection(modules)
ns.configure(
{
Expand Down
2 changes: 1 addition & 1 deletion tasks/github_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from tasks.libs.common.datadog_api import create_count, send_metrics
from tasks.libs.common.junit_upload_core import repack_macos_junit_tar
from tasks.libs.common.utils import DEFAULT_BRANCH, DEFAULT_INTEGRATIONS_CORE_BRANCH, get_git_pretty_ref
from tasks.libs.pipeline.notifications import read_owners
from tasks.libs.owners.parsing import read_owners
from tasks.release import _get_release_json_value


Expand Down
Empty file added tasks/libs/owners/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions tasks/libs/owners/parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Any, List


def read_owners(owners_file: str) -> Any:
from codeowners import CodeOwners

with open(owners_file, 'r') as f:
return CodeOwners(f.read())


def search_owners(search: str, owners_file: str) -> List[str]:
parsed_owners = read_owners(owners_file)
# owners.of returns a list in the form: [('TEAM', '@DataDog/agent-build-and-releases')]
return [owner[1] for owner in parsed_owners.of(search)]
8 changes: 1 addition & 7 deletions tasks/libs/pipeline/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import yaml

from tasks.libs.ciproviders.gitlab import Gitlab, get_gitlab_token
from tasks.libs.owners.parsing import read_owners
from tasks.libs.types.types import FailedJobs, Test


Expand All @@ -32,13 +33,6 @@ def load_and_validate(file_name: str, default_placeholder: str, default_value: s
GITHUB_JIRA_MAP = load_and_validate("github_jira_map.yaml", "DEFAULT_JIRA_PROJECT", DEFAULT_JIRA_PROJECT)


def read_owners(owners_file):
from codeowners import CodeOwners

with open(owners_file, 'r') as f:
return CodeOwners(f.read())


def check_for_missing_owners_slack_and_jira(print_missing_teams=True, owners_file=".github/CODEOWNERS"):
owners = read_owners(owners_file)
error = False
Expand Down
13 changes: 13 additions & 0 deletions tasks/owners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from invoke import task

from tasks.libs.owners.parsing import search_owners


@task
def find_jobowners(_, job, owners_file=".gitlab/JOBOWNERS"):
print(", ".join(search_owners(job, owners_file)))


@task
def find_codeowners(_, path, owners_file=".github/CODEOWNERS"):
print(", ".join(search_owners(path, owners_file)))
3 changes: 2 additions & 1 deletion tasks/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
nightly_entry_for,
release_entry_for,
)
from tasks.libs.pipeline.notifications import read_owners, send_slack_message
from tasks.libs.owners.parsing import read_owners
from tasks.libs.pipeline.notifications import send_slack_message
from tasks.libs.pipeline.tools import (
FilteredOutException,
cancel_pipelines_with_confirmation,
Expand Down
2 changes: 1 addition & 1 deletion tasks/show_linters_issues/golangci_lint_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import defaultdict
from typing import Dict

from tasks.libs.pipeline.notifications import read_owners
from tasks.libs.owners.parsing import read_owners

# Example lint message
# "pointer.go:6:1: package-comments: should have a package comment (revive)"
Expand Down
2 changes: 1 addition & 1 deletion tasks/unit-tests/github_tasks_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestAssignTeamLabelMock(unittest.TestCase):
CODEOWNERS_FILE = './tasks/unit-tests/testdata/codeowners.txt'

def make_test(self, changed_files, expected_labels, pr_labels=None, possible_labels=None):
from tasks.libs.pipeline.notifications import read_owners
from tasks.libs.owners.parsing import read_owners

possible_labels = possible_labels or ['team/agent-platform', 'team/documentation', 'team/agent-security']

Expand Down
27 changes: 27 additions & 0 deletions tasks/unit-tests/owners_lib_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest

from tasks.libs.owners.parsing import search_owners


class TestSearchCodeOwners(unittest.TestCase):
CODEOWNERS_FILE = './tasks/unit-tests/testdata/codeowners.txt'
JOBOWNERS_FILE = './tasks/unit-tests/testdata/jobowners.txt'

def test_search_codeowners(self):
self.assertListEqual(search_owners("no_owners/file", self.CODEOWNERS_FILE), [])
self.assertListEqual(search_owners(".dotfile", self.CODEOWNERS_FILE), ["@DataDog/agent-platform"])
self.assertListEqual(
search_owners("doc.md", self.CODEOWNERS_FILE), ["@DataDog/agent-platform", "@DataDog/documentation"]
)
self.assertListEqual(search_owners(".gitlab/security.yml", self.CODEOWNERS_FILE), ["@DataDog/agent-security"])

def test_search_jobowners(self):
self.assertListEqual(search_owners("default_job", self.JOBOWNERS_FILE), ["@DataDog/agent-ci-experience"])
self.assertListEqual(search_owners("tests_default", self.JOBOWNERS_FILE), ["@DataDog/multiple"])
self.assertListEqual(search_owners("tests_ebpf_x64", self.JOBOWNERS_FILE), ["@DataDog/ebpf-platform"])
self.assertListEqual(
search_owners("security_go_generate_check", self.JOBOWNERS_FILE), ["@DataDog/agent-security"]
)
self.assertListEqual(
search_owners("security_go_generate_checks", self.JOBOWNERS_FILE), ["@DataDog/agent-ci-experience"]
)
7 changes: 7 additions & 0 deletions tasks/unit-tests/testdata/jobowners.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Extract of the .gitab/JOBOWNERS file, for testing purposes

* @DataDog/agent-ci-experience

tests_* @DataDog/multiple
tests_ebpf* @DataDog/ebpf-platform
security_go_generate_check @DataDog/agent-security

0 comments on commit 182707b

Please sign in to comment.