Skip to content

Commit

Permalink
Merge pull request #5307 from johnpmitsch/errata_search
Browse files Browse the repository at this point in the history
Fixes #10824 - Refactoring errata index to show available errata to content view filter usin…
  • Loading branch information
jlsherrill committed Jun 25, 2015
2 parents 591b1d1 + 9242ac7 commit 445015f
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 62 deletions.
Expand Up @@ -4,7 +4,7 @@ class Api::V2::ContentViewFiltersController < Api::V2::ApiController

before_filter :find_content_view
before_filter :find_filter, :except => [:index, :create, :auto_complete_search]
before_filter :load_search_service, :only => [:available_errata, :available_package_groups]
before_filter :load_search_service, :only => [:available_package_groups]

wrap_parameters :include => (ContentViewFilter.attribute_names + %w(repository_ids))

Expand Down Expand Up @@ -69,28 +69,6 @@ def destroy
respond_for_show :resource => @filter
end

api :GET, "/content_views/:content_view_id/filters/:id/available_errata",
N_("Get errata that are available to be added to the filter")
api :GET, "/content_view_filters/:id/available_errata",
N_("Get errata that are available to be added to the filter")
param :content_view_id, :identifier, :desc => N_("content view identifier")
param :id, :identifier, :desc => N_("filter identifier"), :required => true
param :types, Array, :desc => N_("Errata types array \\['security', 'bugfix', 'enhancement'\\]")
param :start_date, DateTime, :desc => N_("Start date that Errata was issued on to filter by")
param :end_date, DateTime, :desc => N_("End date that Errata was issued on to filter by")
def available_errata
current_errata_ids = @filter.erratum_rules.map(&:errata_id)

scoped = Erratum.in_repositories(@filter.applicable_repos)
scoped = scoped.where('errata_id not in (?)', current_errata_ids) unless current_errata_ids.empty?
scoped = scoped.where('issued >= ?', params[:start_date]) if params[:start_date]
scoped = scoped.where('issued <= ?', params[:end_date]) if params[:end_date]
scoped = scoped.of_type(params[:types]) if params[:types]

respond_for_index :template => '../errata/index',
:collection => scoped_search(scoped, 'issued', 'desc', :resource_class => Erratum)
end

api :GET, "/content_views/:content_view_id/filters/:id/available_package_groups",
N_("Get package groups that are available to be added to the filter")
api :GET, "/content_view_filters/:id/available_package_groups",
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/katello/api/v2/errata_controller.rb
Expand Up @@ -17,6 +17,16 @@ def index
super
end

def available_for_content_view_filter(filter, collection)
collection = filter_by_content_view(filter, collection)
ids = Katello::ContentViewErratumFilterRule.where(:content_view_filter_id => filter.id).pluck("errata_id")
collection = collection.where("errata_id not in (?)", ids) unless ids.empty?
collection = collection.where('issued >= ?', params[:start_date]) if params[:start_date]
collection = collection.where('issued <= ?', params[:end_date]) if params[:end_date]
collection = collection.of_type(params[:types]) if params[:types]
collection
end

def custom_index_relation(collection)
collection = filter_by_cve(params[:cve], collection) if params[:cve]
if ::Foreman::Cast.to_bool(params[:errata_restrict_applicable])
Expand All @@ -35,6 +45,12 @@ def filter_by_cve(cve, collection)
collection.joins(:cves).where('katello_erratum_cves.cve_id' => cve)
end

def filter_by_content_view(filter, collection)
repos = Katello::ContentView.find(filter.content_view_id).repositories
uuid = repos.map { |r| r.send("errata").pluck("uuid") }
filter_by_ids(uuid, collection)
end

def filter_by_content_view_filter(filter, collection)
collection.where(:errata_id => filter.erratum_rules.pluck(:errata_id))
end
Expand Down
Expand Up @@ -150,7 +150,7 @@ def check_repo_for_content_resource

def filter_by_content_view_filter(filter, options)
ids = filter.send("#{singular_resource_name}_rules").map(&:uuid)
repo_ids = filter.applicable_repos.readable.map(&:pulp_id)
repo_ids = filter.applicable_repos.readable.pluck("#{Repository.table_name}.pulp_id")

filter_by_ids(ids, options)
filter_by_repo_ids(repo_ids, options)
Expand Down
Expand Up @@ -35,15 +35,22 @@ def compare
respond_for_index(:collection => collection)
end

param :available_for, :string, :desc => N_("Show errata that can be added to content view filter")
param :filterId, :integer, :desc => N_("Content View Filter id")
def index_relation
collection = resource_class.scoped
collection = filter_by_repos(Repository.readable, collection)
collection = filter_by_repos([@repo], collection) if @repo && !@repo.puppet?
collection = filter_by_content_view_filter(@filter, collection) if @filter
collection = filter_by_content_view_version(@version, collection) if @version
collection = filter_by_environment(@environment, collection) if @environment
collection = filter_by_repos(Repository.readable.in_organization(@organization), collection) if @organization
collection = filter_by_ids(params[:ids], collection) if params[:ids]
@filter = ContentViewFilter.find(params[:filterId]) if params[:filterId]
if params[:available_for] == "content_view_filter" && self.respond_to?(:available_for_content_view_filter)
collection = self.available_for_content_view_filter(@filter, collection) if @filter
else
collection = filter_by_content_view_filter(@filter, collection) if @filter
end
collection = self.custom_index_relation(collection) if self.respond_to?(:custom_index_relation)
collection
end
Expand Down
5 changes: 3 additions & 2 deletions config/routes/api/v2.rb
Expand Up @@ -69,8 +69,10 @@ class ActionDispatch::Routing::Mapper
end
end
api_resources :filters, :controller => :content_view_filters do
collection do
get :auto_complete_search
end
member do
get :available_errata
get :available_package_groups
end
api_resources :errata, :only => [:index]
Expand All @@ -89,7 +91,6 @@ class ActionDispatch::Routing::Mapper
get :auto_complete_search
end
member do
get :available_errata
get :available_package_groups
end
end
Expand Down
Expand Up @@ -132,7 +132,7 @@ angular.module('Bastion.content-views').controller('ContentViewDetailsController
};

$scope.$watch('detailsTable.rows', function () {
if ($scope.detailsTable.rows.length > 0) {
if ($scope.detailsTable && $scope.detailsTable.rows.length > 0) {
processTasks($scope.detailsTable.rows);
}
});
Expand Down
Expand Up @@ -13,8 +13,8 @@
* functionality to create filter rules based off selected errata.
*/
angular.module('Bastion.content-views').controller('AvailableErrataFilterController',
['$scope', 'translate', 'Nutupane', 'Filter', 'Rule',
function ($scope, translate, Nutupane, Filter, Rule) {
['$scope', 'translate', 'Nutupane', 'Erratum', 'Rule',
function ($scope, translate, Nutupane, Erratum, Rule) {

var nutupane, filterByDate;

Expand All @@ -35,13 +35,15 @@ angular.module('Bastion.content-views').controller('AvailableErrataFilterControl
return rules.$save(params, success, failure);
}

$scope.nutupane = nutupane = new Nutupane(Filter, {
$scope.nutupane = nutupane = new Nutupane(Erratum, {
filterId: $scope.$stateParams.filterId,
'sort_order': 'DESC',
'sort_by': 'issued'
'sort_by': 'issued',
'available_for': 'content_view_filter'
},
'availableErrata'
'queryUnpaged'
);
nutupane.masterOnly = true;
nutupane.enableSelectAllResults();

filterByDate = function (date, type) {
Expand Down
Expand Up @@ -13,8 +13,8 @@
* to remove errata from the filter.
*/
angular.module('Bastion.content-views').controller('ErrataFilterListController',
['$scope', 'translate', 'Nutupane', 'Filter', 'Rule',
function ($scope, translate, Nutupane, Filter, Rule) {
['$scope', 'translate', 'Nutupane', 'Erratum', 'Rule',
function ($scope, translate, Nutupane, Erratum, Rule) {
var nutupane;

function findRules(errataIds) {
Expand Down Expand Up @@ -47,12 +47,12 @@ angular.module('Bastion.content-views').controller('ErrataFilterListController',
$scope.$parent.errorMessages = [response.data.displayMessage];
}

$scope.nutupane = nutupane = new Nutupane(Filter, {
$scope.nutupane = nutupane = new Nutupane(Erratum, {
filterId: $scope.$stateParams.filterId,
'sort_order': 'DESC',
'sort_by': 'issued'
},
'errata'
'queryUnpaged'
);

$scope.detailsTable = nutupane.table;
Expand Down
Expand Up @@ -17,7 +17,7 @@ angular.module('Bastion.content-views').factory('Filter',
update: {method: 'PUT'},
availableErrata: {
method: 'GET',
params: {action: 'available_errata'}
params: {action: 'errata', 'available_for': 'content_view_filter'}
},
errata: {
method: 'GET',
Expand Down
Expand Up @@ -74,10 +74,10 @@ describe('Factory: Filter', function() {
});

it('provides a way to get installable errata for a filter', function() {
$httpBackend.expectGET('/katello/api/v2/content_view_filters/1/available_errata?content_view_id=1')
$httpBackend.expectGET('/katello/api/v2/content_view_filters/1/errata?available_for=content_view_filter')
.respond({});

Filter.availableErrata({'filterId': '1', 'content_view_id': 1}, function (errata) {
Filter.availableErrata({'filterId': 1}, function (errata) {
expect(errata).toBeDefined();
});
});
Expand Down
3 changes: 1 addition & 2 deletions lib/katello/permissions/content_view_permissions.rb
Expand Up @@ -5,8 +5,7 @@
{
'katello/api/v2/content_views' => [:index, :show, :history, :available_puppet_modules,
:available_puppet_module_names],
'katello/api/v2/content_view_filters' => [:index, :show, :available_errata,
:available_package_groups],
'katello/api/v2/content_view_filters' => [:index, :show, :available_package_groups],
'katello/api/v2/content_view_filter_rules' => [:index, :show],
'katello/api/v2/content_view_puppet_modules' => [:index, :show],
'katello/api/v2/content_view_versions' => [:index, :show],
Expand Down
18 changes: 0 additions & 18 deletions test/controllers/api/v2/content_view_filters_controller_test.rb
Expand Up @@ -150,24 +150,6 @@ def test_destroy_protected
end
end

def test_available_errata
@filter = katello_content_view_filters(:populated_erratum_filter)
get :available_errata, :content_view_id => @filter.content_view_id, :id => @filter.id

assert_response :success
assert_template 'katello/api/v2/content_view_filters/../errata/index'
end

def test_available_errata_protected
@filter = katello_content_view_filters(:populated_erratum_filter)
allowed_perms = [@view_permission]
denied_perms = [@create_permission, @update_permission, @destroy_permission]

assert_protected_action(:available_errata, allowed_perms, denied_perms) do
get :available_errata, :content_view_id => @filter.content_view_id, :id => @filter.id
end
end

def test_available_package_groups
@filter = katello_content_view_filters(:populated_package_group_filter)
get :available_package_groups, :content_view_id => @filter.content_view_id, :id => @filter.id
Expand Down
16 changes: 14 additions & 2 deletions test/controllers/api/v2/errata_controller_test.rb
Expand Up @@ -10,6 +10,7 @@ def self.before_suite
def models
::Katello::Product.any_instance.stubs(:as_json).returns([])
@repo = Repository.find(katello_repositories(:rhel_6_x86_64))
@errata_filter = katello_content_view_filters(:populated_erratum_filter)
end

def permissions
Expand Down Expand Up @@ -71,13 +72,24 @@ def test_index_with_environment_id
end

def test_index_with_filters
errata_filter = ContentViewFilter.find(katello_content_view_filters(:populated_erratum_filter))
get :index, :content_view_filter_id => errata_filter
get :index, :content_view_filter_id => @errata_filter

package_group_filter = ContentViewFilter.find(katello_content_view_filters(:populated_package_group_filter))
get :index, :content_view_filter_id => package_group_filter
end

def test_index_available_errata_for_content_view_filter
filtered_id = @errata_filter.erratum_rules.first["errata_id"]

get :index, :filterId => @errata_filter, :available_for => "content_view_filter"
body = JSON.parse(response.body)
response_ids = body["results"].map { |item| item["errata_id"] }

assert_response :success
assert !(response_ids.include? filtered_id)
assert response_ids.length > 0
end

def test_index_with_cve
cve = katello_erratum_cves(:cve)

Expand Down

0 comments on commit 445015f

Please sign in to comment.