Skip to content

Commit

Permalink
allow filtering stages by failed
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 4, 2022
1 parent a49201d commit 345637d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
15 changes: 11 additions & 4 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ def new
def show
stages = @project.stages

# for large projects we allow filtering by stage name
if (search = params[:stage_name]).presence
query = ActiveRecord::Base.send(:sanitize_sql_like, search)
# for large projects we allow filtering
if (name = params.dig(:search, :name)).presence
query = ActiveRecord::Base.send(:sanitize_sql_like, name)
stages = stages.where(Stage.arel_table[:name].matches("%#{query}%"))
end

@pagy, @stages = pagy(stages, page: params[:page], items: Integer(params[:per_page] || 25))
if params.dig(:search, :failed) == "true"
# inefficient and slow, but rarely used
@pagy = Pagy.new(count: stages.size, page: 1, items: stages.size)
@stages = stages.select { |s| ["cancelling", "cancelled", "errored", "failed"].include?(s.last_deploy&.status) }
else
@pagy, @stages = pagy(stages, page: params[:page], items: Integer(params[:per_page] || 25))
end

super
end

Expand Down
19 changes: 10 additions & 9 deletions app/views/projects/_stage_name_filter.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<% if @pagy.last != 1 || (search = params[:stage_name].presence)%>
<%= form_tag "?", method: :get, html: { class: "form-horizontal" } do |form| %>
<div class="input-group">
<%= text_field_tag :stage_name, search, class: 'form-control', placeholder: 'Search stage names' %>
<span class="input-group-btn">
<button type="submit" class="btn btn-default" type="button">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
<% if @pagy.pages > 1 || params[:search] %>
<%= search_form top_pad: false do %>
<div class="col-sm-4">
<%= label_tag 'Name' %>
<%= text_field_tag 'search[name]', params.dig(:search, :name), class: "form-control" %>
</div>

<div class="col-sm-1">
<%= label_tag "failed" %>&nbsp;&nbsp;&nbsp;
<%= check_box_tag 'search[failed]', "true", params.dig(:search, :failed) %>
</div>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</tr>
</thead>
<tbody>
<% if @stages.any? || @pagy.page != 1 %>
<% if @pagy.count != 0 %>
<%
# expire the cache when the project or any of the stages (and their deploy-groups/envs) gets locked
# a little expensive, but better than expiring when anything gets locked (confirmed that it's not doing N+1s)
Expand Down
9 changes: 8 additions & 1 deletion test/controllers/projects_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,17 @@ def validate_search_url(url, result)
end

it "can filter by name" do
get :show, params: {id: project.to_param, stage_name: "oduction"}
get :show, params: {id: project.to_param, search: {name: "oduction"}}
assert_response :success
assigns[:stages].map(&:name).must_equal ["Production", "Production Pod"]
end

it "can filter by failed" do
deploys(:succeeded_test).destroy # only leave the failed deploy
get :show, params: {id: project.to_param, search: {failed: "true"}}
assert_response :success
assigns[:stages].map(&:name).must_equal ["Staging"]
end
end

describe "as JSON" do
Expand Down

0 comments on commit 345637d

Please sign in to comment.