aboutsummaryrefslogtreecommitdiff
path: root/cros_utils
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2023-03-29 13:09:01 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-29 22:43:41 +0000
commitfaf98c21eafd89bd036a9d9072bc4c01549ed47b (patch)
treefcd92de194d647377a332b0176ef6235a88d8376 /cros_utils
parent613b5005fa93aa29aafe291c27b0df73604d6ee3 (diff)
downloadtoolchain-utils-faf98c21eafd89bd036a9d9072bc4c01549ed47b.tar.gz
bugs: allow users to set directories for bug reports
This allows easily overriding the default of X20_PATH. BUG=b:275742850 TEST=Unittests Change-Id: Ia102e7b6abc2cf738c26c06cd88394efa73e88cc Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/4382438 Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com> Tested-by: George Burgess <gbiv@chromium.org> Commit-Queue: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'cros_utils')
-rwxr-xr-xcros_utils/bugs.py28
-rwxr-xr-xcros_utils/bugs_test.py33
2 files changed, 54 insertions, 7 deletions
diff --git a/cros_utils/bugs.py b/cros_utils/bugs.py
index c2c6ca22..cfec3a6b 100755
--- a/cros_utils/bugs.py
+++ b/cros_utils/bugs.py
@@ -2,7 +2,6 @@
# Copyright 2021 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-
"""Utilities to file bugs."""
import datetime
@@ -15,6 +14,7 @@ from typing import Any, Dict, List, Optional
X20_PATH = "/google/data/rw/teams/c-compiler-chrome/prod_bugs"
+
# These constants are sourced from
# //google3/googleclient/chrome/chromeos_toolchain/bug_manager/bugs.go
class WellKnownComponents(enum.IntEnum):
@@ -66,7 +66,9 @@ class _FileNameGenerator:
_GLOBAL_NAME_GENERATOR = _FileNameGenerator()
-def _WriteBugJSONFile(object_type: str, json_object: Dict[str, Any]):
+def _WriteBugJSONFile(
+ object_type: str, json_object: Dict[str, Any], directory: os.PathLike
+):
"""Writes a JSON file to X20_PATH with the given bug-ish object."""
final_object = {
"type": object_type,
@@ -75,7 +77,7 @@ def _WriteBugJSONFile(object_type: str, json_object: Dict[str, Any]):
now = datetime.datetime.now(tz=datetime.timezone.utc)
file_path = os.path.join(
- X20_PATH, _GLOBAL_NAME_GENERATOR.generate_json_file_name(now)
+ directory, _GLOBAL_NAME_GENERATOR.generate_json_file_name(now)
)
temp_path = file_path + ".in_progress"
try:
@@ -88,7 +90,9 @@ def _WriteBugJSONFile(object_type: str, json_object: Dict[str, Any]):
return file_path
-def AppendToExistingBug(bug_id: int, body: str):
+def AppendToExistingBug(
+ bug_id: int, body: str, directory: os.PathLike = X20_PATH
+):
"""Sends a reply to an existing bug."""
_WriteBugJSONFile(
"AppendToExistingBugRequest",
@@ -96,6 +100,7 @@ def AppendToExistingBug(bug_id: int, body: str):
"body": body,
"bug_id": bug_id,
},
+ directory,
)
@@ -105,6 +110,7 @@ def CreateNewBug(
body: str,
assignee: Optional[str] = None,
cc: Optional[List[str]] = None,
+ directory: os.PathLike = X20_PATH,
):
"""Sends a request to create a new bug.
@@ -117,6 +123,8 @@ def CreateNewBug(
"well-known" assignee (detective, mage).
cc: A list of emails to add to the CC list. Must either be an email
address, or a "well-known" individual (detective, mage).
+ directory: The directory to write the report to. Defaults to our x20 bugs
+ directory.
"""
obj = {
"component_id": component_id,
@@ -130,11 +138,15 @@ def CreateNewBug(
if cc:
obj["cc"] = cc
- _WriteBugJSONFile("FileNewBugRequest", obj)
+ _WriteBugJSONFile("FileNewBugRequest", obj, directory)
def SendCronjobLog(
- cronjob_name: str, failed: bool, message: str, turndown_time_hours: int = 0
+ cronjob_name: str,
+ failed: bool,
+ message: str,
+ turndown_time_hours: int = 0,
+ directory: os.PathLike = X20_PATH,
):
"""Sends the record of a cronjob to our bug infra.
@@ -147,6 +159,8 @@ def SendCronjobLog(
turned down if more than `turndown_time_hours` pass without a report of
success or failure. If zero, this job will not automatically be turned
down.
+ directory: The directory to write the report to. Defaults to our x20 bugs
+ directory.
"""
json_object = {
"name": cronjob_name,
@@ -155,4 +169,4 @@ def SendCronjobLog(
}
if turndown_time_hours:
json_object["cronjob_turndown_time_hours"] = turndown_time_hours
- _WriteBugJSONFile("CronjobUpdate", json_object)
+ _WriteBugJSONFile("CronjobUpdate", json_object, directory)
diff --git a/cros_utils/bugs_test.py b/cros_utils/bugs_test.py
index 1b58e984..226d1fef 100755
--- a/cros_utils/bugs_test.py
+++ b/cros_utils/bugs_test.py
@@ -11,8 +11,10 @@
import datetime
import json
import os
+from pathlib import Path
import tempfile
import unittest
+from unittest import mock
from unittest.mock import patch
from cros_utils import bugs
@@ -41,6 +43,7 @@ class Tests(unittest.TestCase):
"foo": "bar",
"baz": bugs.WellKnownComponents.CrOSToolchainPublic,
},
+ bugs.X20_PATH,
)
self.assertTrue(
@@ -72,6 +75,7 @@ class Tests(unittest.TestCase):
"body": "hello, world!",
"bug_id": 1234,
},
+ bugs.X20_PATH,
)
@patch.object(bugs, "_WriteBugJSONFile")
@@ -115,6 +119,7 @@ class Tests(unittest.TestCase):
mock_write_json_file.assert_called_once_with(
"FileNewBugRequest",
expected_output,
+ bugs.X20_PATH,
)
mock_write_json_file.reset_mock()
@@ -129,6 +134,7 @@ class Tests(unittest.TestCase):
"message": "hello, world!",
"failed": False,
},
+ bugs.X20_PATH,
)
@patch.object(bugs, "_WriteBugJSONFile")
@@ -147,6 +153,7 @@ class Tests(unittest.TestCase):
"failed": False,
"cronjob_turndown_time_hours": 42,
},
+ bugs.X20_PATH,
)
def testFileNameGenerationProducesFileNamesInSortedOrder(self):
@@ -187,6 +194,32 @@ class Tests(unittest.TestCase):
child_file = gen.generate_json_file_name(_ARBITRARY_DATETIME)
self.assertNotEqual(parent_file, child_file)
+ @patch.object(bugs, "_WriteBugJSONFile")
+ def testCustomDirectoriesArePassedThrough(self, mock_write_json_file):
+ directory = "/path/to/somewhere/interesting"
+ bugs.AppendToExistingBug(1, "foo", directory=directory)
+ mock_write_json_file.assert_called_once_with(
+ mock.ANY, mock.ANY, directory
+ )
+ mock_write_json_file.reset_mock()
+
+ bugs.CreateNewBug(1, "title", "body", directory=directory)
+ mock_write_json_file.assert_called_once_with(
+ mock.ANY, mock.ANY, directory
+ )
+ mock_write_json_file.reset_mock()
+
+ bugs.SendCronjobLog("cronjob", False, "message", directory=directory)
+ mock_write_json_file.assert_called_once_with(
+ mock.ANY, mock.ANY, directory
+ )
+
+ def testWriteBugJSONFileWritesToGivenDirectory(self):
+ with tempfile.TemporaryDirectory() as tmpdir:
+ bugs.AppendToExistingBug(1, "body", directory=tmpdir)
+ json_files = list(Path(tmpdir).glob("*.json"))
+ self.assertEqual(len(json_files), 1, json_files)
+
if __name__ == "__main__":
unittest.main()