|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# typed: strict |
| 4 | + |
| 5 | +module CodeOwnership |
| 6 | + module Private |
| 7 | + class ForFileOutputBuilder |
| 8 | + extend T::Sig |
| 9 | + private_class_method :new |
| 10 | + |
| 11 | + sig { params(file_path: String, json: T::Boolean, verbose: T::Boolean).void } |
| 12 | + def initialize(file_path:, json:, verbose:) |
| 13 | + @file_path = file_path |
| 14 | + @json = json |
| 15 | + @verbose = verbose |
| 16 | + end |
| 17 | + |
| 18 | + sig { params(file_path: String, json: T::Boolean, verbose: T::Boolean).returns(String) } |
| 19 | + def self.build(file_path:, json:, verbose:) |
| 20 | + new(file_path: file_path, json: json, verbose: verbose).build |
| 21 | + end |
| 22 | + |
| 23 | + UNOWNED_OUTPUT = T.let( |
| 24 | + { |
| 25 | + team_name: 'Unowned', |
| 26 | + team_yml: 'Unowned' |
| 27 | + }, |
| 28 | + T::Hash[Symbol, T.untyped] |
| 29 | + ) |
| 30 | + |
| 31 | + sig { returns(String) } |
| 32 | + def build |
| 33 | + result_hash = @verbose ? build_verbose : build_terse |
| 34 | + |
| 35 | + return result_hash.to_json if @json |
| 36 | + |
| 37 | + build_message_for(result_hash) |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + sig { returns(T::Hash[Symbol, T.untyped]) } |
| 43 | + def build_verbose |
| 44 | + result = CodeOwnership.for_file_verbose(@file_path) |
| 45 | + return UNOWNED_OUTPUT if result.nil? |
| 46 | + |
| 47 | + { |
| 48 | + team_name: result[:team_name], |
| 49 | + team_yml: result[:team_config_yml], |
| 50 | + description: result[:reasons] |
| 51 | + } |
| 52 | + end |
| 53 | + |
| 54 | + sig { returns(T::Hash[Symbol, T.untyped]) } |
| 55 | + def build_terse |
| 56 | + team = CodeOwnership.for_file(@file_path) |
| 57 | + |
| 58 | + if team.nil? |
| 59 | + UNOWNED_OUTPUT |
| 60 | + else |
| 61 | + { |
| 62 | + team_name: team.name, |
| 63 | + team_yml: team.config_yml |
| 64 | + } |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + sig { params(result_hash: T::Hash[Symbol, T.untyped]).returns(String) } |
| 69 | + def build_message_for(result_hash) |
| 70 | + messages = ["Team: #{result_hash[:team_name]}", "Team YML: #{result_hash[:team_yml]}"] |
| 71 | + description_list = T.let(Array(result_hash[:description]), T::Array[String]) |
| 72 | + messages << build_description_message(description_list) unless description_list.empty? |
| 73 | + messages.last << "\n" |
| 74 | + messages.join("\n") |
| 75 | + end |
| 76 | + |
| 77 | + sig { params(reasons: T::Array[String]).returns(String) } |
| 78 | + def build_description_message(reasons) |
| 79 | + "Description:\n- #{reasons.join("\n-")}" |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments