__init__.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. """Utility functions only useful in CI."""
  10. import os
  11. from pathlib import Path
  12. from typing import Any
  13. import requests
  14. from semver.version import VersionInfo
  15. from materialize import MZ_ROOT, buildkite, cargo, ui
  16. def junit_report_filename(suite: str) -> Path:
  17. """Compute the JUnit report filename for the specified test suite.
  18. See also `upload_test_report`. In CI, the filename will include the
  19. Buildkite job ID.
  20. Args:
  21. suite: The identifier for the test suite in Buildkite Test Analytics.
  22. """
  23. filename = f"junit_{suite}"
  24. if "BUILDKITE_JOB_ID" in os.environ:
  25. filename += "_" + os.environ["BUILDKITE_JOB_ID"]
  26. return Path(f"{filename}.xml")
  27. def get_artifacts() -> Any:
  28. """Get artifact informations from Buildkite. Outside of CI, this function does nothing."""
  29. if not buildkite.is_in_buildkite():
  30. return []
  31. ui.section("Getting artifact informations from Buildkite")
  32. build = os.environ["BUILDKITE_BUILD_NUMBER"]
  33. build_id = os.environ["BUILDKITE_BUILD_ID"]
  34. job = os.environ["BUILDKITE_JOB_ID"]
  35. token = os.environ["BUILDKITE_AGENT_ACCESS_TOKEN"]
  36. payload = {
  37. "query": "*",
  38. "step": job,
  39. "build": build,
  40. "state": "finished",
  41. "includeRetriedJobs": "false",
  42. "includeDuplicates": "false",
  43. }
  44. res = requests.get(
  45. f"https://agent.buildkite.com/v3/builds/{build_id}/artifacts/search",
  46. params=payload,
  47. headers={"Authorization": f"Token {token}"},
  48. )
  49. if res.status_code != 200:
  50. print(f"Failed to get artifacts: {res.status_code} {res.text}")
  51. return []
  52. return res.json()
  53. def get_mz_version(workspace: cargo.Workspace | None = None) -> VersionInfo:
  54. """Get the current Materialize version from Cargo.toml."""
  55. if not workspace:
  56. workspace = cargo.Workspace(MZ_ROOT)
  57. return VersionInfo.parse(workspace.crates["mz-environmentd"].version_string)