|
1 | 1 | # typed: strict |
2 | 2 | # frozen_string_literal: true |
3 | 3 |
|
| 4 | +require 'yaml' |
| 5 | +require_relative 'views' |
| 6 | + |
4 | 7 | module Chatwerk |
5 | | - # The API module provides a structured interface for accessing QueryPackwerk functionality. |
6 | | - # It's designed to be used by the MCP (Machine Control Protocol) server. |
7 | | - # All methods are designed to return structured data that can be easily serialized to JSON. |
8 | 8 | module API |
9 | | - extend T::Sig |
10 | | - extend self # rubocop:disable Style/ModuleFunction |
11 | | - |
12 | | - # Get detailed information about a single package |
13 | | - sig { params(pack_name: String).returns(T::Hash[String, T.untyped]) } |
14 | | - def package_info(pack_name) |
15 | | - pack = QueryPackwerk.package(pack_name) |
16 | | - { error: "Package '#{pack_name}' not found" } unless pack |
17 | | - end |
18 | | - |
19 | | - # Get all dependencies of a package, including information about ignored dependencies and violations |
20 | | - sig { params(pack_name: String).returns(T::Hash[String, T.untyped]) } |
21 | | - def package_dependencies(pack_name) |
22 | | - pack = QueryPackwerk.package(pack_name) |
23 | | - return { error: "Package '#{pack_name}' not found" } unless pack |
24 | | - |
25 | | - violations = QueryPackwerk::Violations.where(consuming_pack: pack.name) |
26 | | - # Group violations by the package they reference |
27 | | - violation_by_package = {} |
28 | | - violations.each do |v| |
29 | | - package_name = v.to_package_name |
30 | | - violation_by_package[package_name] ||= [] |
31 | | - violation_by_package[package_name] << v |
32 | | - end |
33 | | - |
34 | | - # Get declared dependencies |
35 | | - declared_dependencies = pack.dependency_names.map do |dep_name| |
36 | | - dep_pack = QueryPackwerk.package(dep_name) |
37 | | - next { name: dep_name, status: 'unknown' } unless dep_pack |
38 | | - |
39 | | - { |
40 | | - name: dep_name, |
41 | | - status: 'declared', |
42 | | - owner: dep_pack.owner, |
43 | | - enforce_dependencies: dep_pack.enforce_dependencies, |
44 | | - enforce_privacy: dep_pack.enforce_privacy |
45 | | - } |
46 | | - end |
47 | | - |
48 | | - # Get ignored dependencies from package.yml if available |
49 | | - ignored_dependencies = [] |
50 | | - if pack.metadata['ignored_dependencies'].is_a?(Array) |
51 | | - ignored_dependencies = pack.metadata['ignored_dependencies'].map do |dep_name| |
52 | | - dep_pack = QueryPackwerk.package(dep_name) |
53 | | - next { name: dep_name, status: 'ignored' } unless dep_pack |
54 | | - |
55 | | - { |
56 | | - name: dep_name, |
57 | | - status: 'ignored', |
58 | | - owner: dep_pack.owner |
59 | | - } |
| 9 | + class << self |
| 10 | + def packages(package_path: nil) |
| 11 | + packages = Helpers.all_packages(package_path) |
| 12 | + |
| 13 | + if packages.empty? |
| 14 | + has_packwerk_yml = File.exist?('packwerk.yml') |
| 15 | + Views::NoPackagesView.render(has_packwerk_yml:) |
| 16 | + else |
| 17 | + Views::PackagesView.render(packages:, has_packwerk_yml:) |
60 | 18 | end |
| 19 | + rescue StandardError => e |
| 20 | + raise Chatwerk::Error.new(e, package_path:) |
61 | 21 | end |
62 | 22 |
|
63 | | - # Get violation dependencies (undeclared dependencies) |
64 | | - violation_dependencies = violation_by_package.keys.reject do |dep| |
65 | | - pack.dependency_names.include?(dep) |
66 | | - end.map do |dep_name| |
67 | | - dep_pack = QueryPackwerk.package(dep_name) |
68 | | - vcount = violation_by_package[dep_name]&.size || 0 |
69 | | - |
70 | | - next { name: dep_name, status: 'violation', violation_count: vcount } unless dep_pack |
71 | | - |
72 | | - { |
73 | | - name: dep_name, |
74 | | - status: 'violation', |
75 | | - owner: dep_pack.owner, |
76 | | - enforce_dependencies: dep_pack.enforce_dependencies, |
77 | | - enforce_privacy: dep_pack.enforce_privacy, |
78 | | - violation_count: vcount |
79 | | - } |
80 | | - end |
81 | | - |
82 | | - { |
83 | | - package: pack.name, |
84 | | - dependencies: { |
85 | | - declared: declared_dependencies, |
86 | | - ignored: ignored_dependencies, |
87 | | - violations: violation_dependencies |
88 | | - }, |
89 | | - total_declared: declared_dependencies.size, |
90 | | - total_ignored: ignored_dependencies.size, |
91 | | - total_violations: violation_dependencies.size |
92 | | - } |
93 | | - end |
94 | | - |
95 | | - # Get all consumers of a package |
96 | | - sig { params(pack_name: String, threshold: Integer).returns(T::Hash[String, T.untyped]) } |
97 | | - def package_consumers(pack_name, threshold: 0) |
98 | | - pack = QueryPackwerk.package(pack_name) |
99 | | - return { error: "Package '#{pack_name}' not found" } unless pack |
100 | | - |
101 | | - consumer_counts = QueryPackwerk.consumers(pack_name, threshold: threshold) |
102 | | - |
103 | | - consumers = consumer_counts.map do |consumer_name, count| |
104 | | - consumer_pack = QueryPackwerk.package(consumer_name) |
105 | | - next { name: consumer_name, count: count } unless consumer_pack |
106 | | - |
107 | | - { |
108 | | - name: consumer_name, |
109 | | - owner: consumer_pack.owner, |
110 | | - count: count, |
111 | | - is_declared_dependency: consumer_pack.dependency_names.include?(pack_name) |
112 | | - } |
113 | | - end |
114 | | - |
115 | | - { |
116 | | - package: pack_name, |
117 | | - consumers: consumers, |
118 | | - total_consumers: consumers.size |
119 | | - } |
120 | | - end |
121 | | - |
122 | | - # Get all usage patterns for a package |
123 | | - sig { params(pack_name: String, threshold: Integer).returns(T::Hash[String, T.untyped]) } |
124 | | - def package_usage_patterns(pack_name, threshold: 0) |
125 | | - pack = QueryPackwerk.package(pack_name) |
126 | | - return { error: "Package '#{pack_name}' not found" } unless pack |
| 23 | + def package(package_path:) |
| 24 | + package = Helpers.find_package(package_path) |
127 | 25 |
|
128 | | - patterns = QueryPackwerk.anonymous_violation_counts_for(pack_name, threshold: threshold) |
129 | | - |
130 | | - result = { |
131 | | - package: pack_name, |
132 | | - patterns: {}, |
133 | | - total_patterns: 0 |
134 | | - } |
135 | | - |
136 | | - patterns.each do |pattern_type, pattern_counts| |
137 | | - result[:patterns][pattern_type] = pattern_counts.map do |pattern, count| |
138 | | - { |
139 | | - pattern: pattern, |
140 | | - count: count |
141 | | - } |
142 | | - end.sort_by { |p| -p[:count] } |
143 | | - |
144 | | - result[:total_patterns] += pattern_counts.size |
| 26 | + Views::PackageView.render(package:) |
| 27 | + rescue StandardError => e |
| 28 | + raise Chatwerk::Error.new(e, package_path:) |
145 | 29 | end |
146 | 30 |
|
147 | | - result |
148 | | - end |
149 | | - |
150 | | - # Get all code patterns that access a specific package |
151 | | - sig { params(pack_name: String).returns(T::Hash[String, T.untyped]) } |
152 | | - def package_access_patterns(pack_name) |
153 | | - violations = QueryPackwerk::Violations.where(producing_pack: QueryPackwerk.full_name(pack_name)) |
154 | | - return { error: "No access patterns found for package '#{pack_name}'" } if violations.count.zero? |
| 31 | + def package_todos(package_path:, constant_name: nil) |
| 32 | + package = Helpers.find_package(package_path) |
| 33 | + constant_name = Helpers.normalize_constant_name(constant_name) |
| 34 | + violations = package.todos |
155 | 35 |
|
156 | | - # Group violations by type and class name |
157 | | - patterns = {} |
158 | | - violations.each do |violation| |
159 | | - type = violation.type |
160 | | - class_name = violation.class_name |
161 | | - patterns[type] ||= {} |
162 | | - patterns[type][class_name] ||= [] |
163 | | - |
164 | | - # Get first sample file for example |
165 | | - next unless violation.sources_with_locations.any? |
166 | | - |
167 | | - patterns[type][class_name] << { |
168 | | - from_package: violation.consuming_pack.name, |
169 | | - location: violation.sources_with_locations.first, |
170 | | - file: violation.sources.first |
171 | | - } |
172 | | - end |
173 | | - |
174 | | - # Format the response |
175 | | - formatted_patterns = {} |
176 | | - patterns.each do |type, class_patterns| |
177 | | - formatted_patterns[type] = class_patterns.map do |class_name, examples| |
178 | | - { |
179 | | - constant: class_name, |
180 | | - count: examples.size, |
181 | | - examples: examples.uniq { |e| e[:from_package] }.first(3) # Limit to 3 examples |
182 | | - } |
| 36 | + if constant_name.empty? |
| 37 | + Views::ViolationsListView.render(package:, violations:) |
| 38 | + else |
| 39 | + Views::ViolationsDetailsView.render(package:, violations:, constant_name:) |
183 | 40 | end |
| 41 | + rescue StandardError => e |
| 42 | + raise Chatwerk::Error.new(e, package_path:, constant_name:) |
184 | 43 | end |
185 | 44 |
|
186 | | - { |
187 | | - package: pack_name, |
188 | | - access_patterns: formatted_patterns, |
189 | | - total_constants: patterns.values.flat_map(&:keys).uniq.size |
190 | | - } |
191 | | - end |
| 45 | + def package_violations(package_path:, constant_name: nil) |
| 46 | + package = Helpers.find_package(package_path) |
| 47 | + constant_name = Helpers.normalize_constant_name(constant_name) |
| 48 | + violations = package.violations |
192 | 49 |
|
193 | | - # Find all the places in the code where a package is used from other packages |
194 | | - sig { params(pack_name: String).returns(T::Hash[String, T.untyped]) } |
195 | | - def find_usage_locations(pack_name) |
196 | | - violations = QueryPackwerk::Violations.where(producing_pack: QueryPackwerk.full_name(pack_name)) |
197 | | - return { error: "No usage found for package '#{pack_name}'" } if violations.count.zero? |
198 | | - |
199 | | - locations = violations.sources_with_locations |
200 | | - |
201 | | - # Group by consuming package for better organization |
202 | | - usage_by_consumer = {} |
203 | | - violations.each do |violation| |
204 | | - consumer = violation.consuming_pack.name |
205 | | - usage_by_consumer[consumer] ||= [] |
206 | | - |
207 | | - violation.sources_with_locations.each do |loc| |
208 | | - usage_by_consumer[consumer] << { |
209 | | - constant: violation.class_name, |
210 | | - location: loc, |
211 | | - type: violation.type |
212 | | - } |
| 50 | + if constant_name.empty? |
| 51 | + Views::ViolationsListView.render(package:, violations:) |
| 52 | + else |
| 53 | + Views::ViolationsDetailsView.render(package:, violations:, constant_name:) |
213 | 54 | end |
| 55 | + rescue StandardError => e |
| 56 | + raise Chatwerk::Error.new(e, package_path:, constant_name:) |
214 | 57 | end |
215 | | - |
216 | | - { |
217 | | - package: pack_name, |
218 | | - usage_locations: usage_by_consumer, |
219 | | - total_consumers: usage_by_consumer.keys.size, |
220 | | - total_locations: locations.values.flatten.size |
221 | | - } |
222 | 58 | end |
223 | 59 | end |
224 | 60 | end |
0 commit comments