Skip to content

Commit

Permalink
Add helper use to auditing
Browse files Browse the repository at this point in the history
- searches applications for references to code from GovukPublishingComponents and displays them in an easy to navigate list
  • Loading branch information
andysellick committed Jul 26, 2022
1 parent 07fa737 commit 26a5343
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 50 deletions.
18 changes: 18 additions & 0 deletions app/models/govuk_publishing_components/audit_applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(path, name)
@component_locations = {}
@gem_style_references = []
@jquery_references = []
@helper_references = {}

if application_found
templates = Dir["#{path}/app/views/**/*.erb"]
Expand Down Expand Up @@ -69,6 +70,7 @@ def initialize(path, name)
gem_style_references: @gem_style_references.flatten.uniq.sort,
jquery_references: @jquery_references.flatten.uniq.sort,
component_locations: @component_locations,
helper_references: @helper_references,
}
end

Expand All @@ -95,6 +97,8 @@ def find_components(files, find, type, keep_locations)
components_found << find_match(find, src, type)
end

get_helper_references(file, src) if %w[ruby templates].include?(type)

if type == "javascripts"
jquery_references = find_code_references(file, src, /\$\(/)
@jquery_references << jquery_references if jquery_references
Expand All @@ -109,6 +113,20 @@ def find_components(files, find, type, keep_locations)
components_found.flatten.uniq.sort
end

def get_helper_references(file, src)
helper_use = find_match(/GovukPublishingComponents::(?:AppHelpers|Presenters)::[a-zA-Z]+/, src, "helper")
if helper_use.any?
helper_use.each do |helper|
class_name = helper.gsub(/GovukPublishingComponents::(AppHelpers)?(Presenters)?::+/, "")
helper_sym = class_name.to_sym
@helper_references[helper_sym] = [] unless @helper_references[helper_sym]
@helper_references[helper_sym] << clean_file_path(file)
@helper_references[helper_sym].uniq!
@helper_references[helper_sym].sort!
end
end
end

# looks for components in the given src of a file
# returns an array of component names or an empty array
def find_match(find, src, type)
Expand Down
34 changes: 33 additions & 1 deletion app/models/govuk_publishing_components/audit_comparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(gem_data, results)
@gem_data = gem_data
@applications_data = sort_results(results)
@gem_data[:components_by_application] = get_components_by_application || []
@gem_data[:helpers_used_by_applications] = get_helpers_used_by_applications || []
end
end

Expand Down Expand Up @@ -107,6 +108,7 @@ def sort_results(results)
gem_style_references: result[:gem_style_references],
jquery_references: result[:jquery_references],
component_locations: result[:component_locations],
helper_references: result[:helper_references],
}
else
data << {
Expand Down Expand Up @@ -269,13 +271,43 @@ def get_components_by_application
locations = locations.flatten

results << {
component: component_name,
name: component_name,
count: locations.length,
locations: locations,
}
end

results if found_something
end

# returns data of components gem helpers used in applications
def get_helpers_used_by_applications
results = []

@applications_data.each do |application|
next unless application[:application_found] && !application[:helper_references].blank?

application[:helper_references].each do |key, value|
location = {
name: application[:name],
locations: value,
}

match = results.find { |x| x[:name] == key }
if match
match[:locations] << location
match[:count] = match[:count] + 1
else
results << {
name: key,
count: 1,
locations: [location],
}
end
end
end

results
end
end
end
61 changes: 12 additions & 49 deletions app/views/govuk_publishing_components/audit/_components.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,56 +56,19 @@
}
%>

<% if @other_applications %>
<% components_by_application = capture do %>
<% if @components[:components_by_application].any? %>
<dl class="govuk-summary-list">
<% @components[:components_by_application].each do |component| %>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<%= component[:component] %> (<%= pluralize(component[:count], 'use') %>)
</dt>
<dd class="govuk-summary-list__value">
<% component[:locations].each do |application| %>
<% github_link = 'https://github.com/alphagov/' + application[:name] + '/blob/main/' %>
<details class="govuk-details govuk-!-margin-bottom-2" data-module="govuk-details">
<summary class="govuk-details__summary">
<span class="govuk-details__summary-text">
<%= application[:name] %> (<%= application[:locations].length %>)
</span>
</summary>
<div class="govuk-details__text">
<ul class="govuk-list govuk-list--bullet">
<% application[:locations].each do |location| %>
<li>
<a href="<%= github_link %><%= location %>" class="govuk-link"><%= location %></a>
</li>
<% end %>
</ul>
</div>
</details>
<% end %>
</dd>
</div>
<% end %>
</dl>
<% end %>
<% end %>
<%= render 'items_in_applications',
heading: 'Components by application',
summary: 'Shows which applications use each component',
content: @components[:components_by_application],
items: component_items
%>

<%
component_items << {
heading: {
text: "Components by application",
},
summary: {
text: "Shows which applications use each component",
},
content: {
html: components_by_application
},
}
%>
<% end %>
<%= render 'items_in_applications',
heading: 'Helpers by application',
summary: 'Shows any applications that use helper classes from the components gem',
content: @components[:helpers_used_by_applications],
items: component_items
%>

<%= render "govuk_publishing_components/components/accordion", {
items: component_items
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<% if @other_applications %>
<% accordion_content = capture do %>
<% if content.any? %>
<dl class="govuk-summary-list">
<% content.each do |item| %>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<%= item[:name] %> (<%= pluralize(item[:count], 'use') %>)
</dt>
<dd class="govuk-summary-list__value">
<% item[:locations].each do |application| %>
<% github_link = 'https://github.com/alphagov/' + application[:name] + '/blob/main/' %>
<details class="govuk-details govuk-!-margin-bottom-2" data-module="govuk-details">
<summary class="govuk-details__summary">
<span class="govuk-details__summary-text">
<%= application[:name] %> (<%= application[:locations].length %>)
</span>
</summary>
<div class="govuk-details__text">
<ul class="govuk-list govuk-list--bullet">
<% application[:locations].each do |location| %>
<li>
<a href="<%= github_link %><%= location %>" class="govuk-link"><%= location %></a>
</li>
<% end %>
</ul>
</div>
</details>
<% end %>
</dd>
</div>
<% end %>
</dl>
<% end %>
<% end %>

<%
items << {
heading: {
text: heading,
},
summary: {
text: summary,
},
content: {
html: accordion_content
},
}
%>
<% end %>
6 changes: 6 additions & 0 deletions spec/component_guide/audit_applications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
tabs: ["app/views/welcome/error_summary.html.erb", "app/views/welcome/tabs_example.html.erb"],
title: ["app/views/welcome/contextual_navigation.html.erb", "app/views/welcome/error_summary.html.erb", "app/views/welcome/table.html.erb"],
},
helper_references: {
BrandHelper: ["lib/test_file_3.rb"],
ButtonHelper: ["lib/test_file_3.rb"],
SharedHelper: ["lib/test_file_3.rb"],
TableHelper: ["app/views/welcome/table.html.erb"],
},
}

expect(application.data).to match(expected)
Expand Down
Loading

0 comments on commit 26a5343

Please sign in to comment.