artifact_search_presentation.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.buildkite_api.builds_api import get_url_to_build
  10. from materialize.buildkite_insights.util.search_utility import (
  11. highlight_match,
  12. trim_match,
  13. )
  14. from materialize.terminal import (
  15. COLOR_CYAN,
  16. with_formatting,
  17. )
  18. SHORT_SEPARATOR = "----------"
  19. LONG_SEPARATOR = "-------------------------------------------------------------------------------------"
  20. def print_before_search_results() -> None:
  21. print()
  22. print(LONG_SEPARATOR)
  23. def print_artifact_match(
  24. file_name: str,
  25. line_number: int,
  26. position_in_line: int,
  27. content: str,
  28. search_value: str,
  29. use_regex: bool,
  30. search_offset: int,
  31. ) -> None:
  32. matched_snippet = trim_match(
  33. match_text=content,
  34. search_value=search_value,
  35. use_regex=use_regex,
  36. search_offset=search_offset,
  37. one_line_match_presentation=False,
  38. )
  39. matched_snippet = highlight_match(
  40. input=matched_snippet,
  41. search_value=search_value,
  42. use_regex=use_regex,
  43. )
  44. print(
  45. f"{with_formatting(file_name, COLOR_CYAN)}, line {line_number}, position {position_in_line}"
  46. )
  47. print(SHORT_SEPARATOR)
  48. print(matched_snippet)
  49. print(LONG_SEPARATOR)
  50. def print_summary(
  51. pipeline_slug: str,
  52. build_number: int,
  53. job_id: str | None,
  54. count_artifacts: int,
  55. count_matches: int,
  56. ignored_file_names: set[str],
  57. max_search_results_hit: bool,
  58. ) -> None:
  59. job_execution_info = (
  60. f"job execution {job_id}" if job_id is not None else "all job executions"
  61. )
  62. url_to_build = get_url_to_build(
  63. pipeline_slug=pipeline_slug, build_number=build_number, job_id=job_id
  64. )
  65. search_scope = f"{job_execution_info} in build #{build_number} of pipeline {pipeline_slug} ({url_to_build})"
  66. if count_artifacts == 0:
  67. print(f"Found no artifacts for {search_scope}!")
  68. else:
  69. suppressed_results_info = (
  70. f"Showing only the first {count_matches} matches! "
  71. if max_search_results_hit
  72. else ""
  73. )
  74. ignored_artifacts_info = (
  75. f"\nOut of {count_artifacts} artifacts, {len(ignored_file_names)} were ignored due to their file extension: {', '.join(ignored_file_names)}."
  76. if len(ignored_file_names) > 0
  77. else ""
  78. )
  79. print(
  80. f"{count_matches} match(es) for {search_scope}. "
  81. f"{suppressed_results_info}"
  82. f"{ignored_artifacts_info}"
  83. )