annotation_search_presentation.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from materialize.buildkite_insights.annotation_search.buildkite_search_source import (
  10. ANY_BRANCH_VALUE,
  11. )
  12. from materialize.buildkite_insights.data.build_annotation import BuildAnnotation
  13. from materialize.buildkite_insights.data.build_info import Build
  14. from materialize.buildkite_insights.util.search_utility import (
  15. highlight_match,
  16. trim_match,
  17. )
  18. from materialize.terminal import (
  19. COLOR_CYAN,
  20. STYLE_BOLD,
  21. with_formatting,
  22. )
  23. SHORT_SEPARATOR = "----------"
  24. LONG_SEPARATOR = "-------------------------------------------------------------------------------------"
  25. def print_before_search_results() -> None:
  26. print()
  27. print(LONG_SEPARATOR)
  28. def print_annotation_match(
  29. build: Build,
  30. annotation: BuildAnnotation,
  31. search_value: str,
  32. use_regex: bool,
  33. short_result_presentation: bool,
  34. one_line_match_presentation: bool,
  35. ) -> None:
  36. print(
  37. with_formatting(
  38. f"Match in build #{build.number} (pipeline {build.pipeline} on {build.branch}):",
  39. STYLE_BOLD,
  40. )
  41. )
  42. print(f"URL: {with_formatting(build.web_url, COLOR_CYAN)}")
  43. print(f"Date: {with_formatting(str(build.created_at), COLOR_CYAN)}")
  44. if annotation.title is not None:
  45. print(f"Annotation: {with_formatting(annotation.title, COLOR_CYAN)}")
  46. if not short_result_presentation:
  47. matched_snippet = trim_match(
  48. match_text=annotation.content,
  49. search_value=search_value,
  50. use_regex=use_regex,
  51. one_line_match_presentation=one_line_match_presentation,
  52. )
  53. matched_snippet = highlight_match(
  54. input=matched_snippet,
  55. search_value=search_value,
  56. use_regex=use_regex,
  57. )
  58. print(SHORT_SEPARATOR)
  59. print(matched_snippet)
  60. print(LONG_SEPARATOR)
  61. def print_summary(
  62. pipeline_slug: str,
  63. branch: str | None,
  64. builds: list[Build],
  65. count_matches: int,
  66. max_results: int,
  67. ) -> None:
  68. if len(builds) == 0:
  69. print("Found no builds!")
  70. else:
  71. most_recent_build_number = builds[0].number
  72. oldest_build_number = builds[-1].number
  73. suppressed_results_info = (
  74. f"Showing only the first {max_results} matches! "
  75. if count_matches > max_results
  76. else ""
  77. )
  78. branch = branch or ANY_BRANCH_VALUE
  79. print(
  80. f"{count_matches} match(es) in {len(builds)} searched builds of pipeline '{pipeline_slug}' and branch '{branch}'. "
  81. f"{suppressed_results_info}"
  82. f"The most recent considered build was #{most_recent_build_number}, "
  83. f"the oldest was #{oldest_build_number}."
  84. )