Skip to content

Commit

Permalink
Move the flag calculation into a model
Browse files Browse the repository at this point in the history
To make the webui code we did for repositories controller
also useful for the API to avoid having 2 ways for the same
  • Loading branch information
coolo committed Dec 16, 2018
1 parent f7f865c commit be95e08
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 98 deletions.
19 changes: 4 additions & 15 deletions src/api/app/controllers/webui2/repositories_controller.rb
Expand Up @@ -2,8 +2,7 @@ module Webui2::RepositoriesController
def webui2_index
@flags = {}
[:build, :debuginfo, :publish, :useforbuild].each do |flag_type|
@flags[flag_type] = {}
set_flag_for(@flags[flag_type], flag_type)
@flags[flag_type] = Flag::SpecifiedFlags.new(@main_object, flag_type)
end

@user_can_set_flags = policy(@project).update?
Expand All @@ -13,24 +12,14 @@ def webui2_index
end

def webui2_create_flag
@flags = {}
set_flag_for(@flags, @flag.flag.to_sym)
@flags = Flag::SpecifiedFlags.new(@main_object, @flag.flag)
end

def webui2_toggle_flag
@flags = {}
set_flag_for(@flags, @flag.flag.to_sym)
@flags = Flag::SpecifiedFlags.new(@main_object, @flag.flag)
end

def webui2_remove_flag
@flags = {}
set_flag_for(@flags, @flag.flag.to_sym)
end

# TODO: This should be private
def set_flag_for(flags, flag_type)
flags[:object] = @main_object.specified_flags(flag_type)
flags[:default] = Flag.new(flag: flag_type, status: Flag.default_status(flag_type))
flags[:project] = @project.specified_flags(flag_type) if @main_object.is_a?(Package)
@flags = Flag::SpecifiedFlags.new(@main_object, @flag.flag)
end
end
35 changes: 0 additions & 35 deletions src/api/app/helpers/webui/repository_helper.rb
@@ -1,41 +1,6 @@
module Webui::RepositoryHelper
def effective_flag(flags, repository, architecture)
dig_flag(:effective_flag_for, flags, repository, architecture)
end

def default_flag(flags, repository, architecture)
dig_flag(:default_flag_for, flags, repository, architecture)
end

def flag_set_by_user?(flags, repository, architecture)
flags[:object].dig(repository, architecture).present?
end

def html_id_for_flag(flag_type, repository, architecture)
# repository and architecture can be nil
valid_xml_id("flag-#{flag_type}-#{repository}-#{architecture}")
end

# TODO: This should be private
def dig_flag(callback_function, flags, repository, architecture)
flag = send(callback_function, flags[:object], repository, architecture)
flag ||= send(callback_function, flags[:project], repository, architecture) if flags[:project]
flag || flags[:default]
end

def effective_flag_for(flags, repository, architecture)
flags.dig(repository, architecture) || flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil)
end

# It finds out how the table would look like if the flag was not set.
# In case of specific flags this means lookup the rest, for flags that are
# only specifying one direction, we need to look at the overall state
# (default state is handled in the function above).
def default_flag_for(flags, repository, architecture)
if repository && architecture
flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil)
elsif architecture || repository
flags.dig(nil, nil)
end
end
end
11 changes: 0 additions & 11 deletions src/api/app/mixins/get_flags.rb
Expand Up @@ -26,15 +26,4 @@ def get_flags(flag_type)

the_flags
end

def specified_flags(flag_type)
all_flags = flags.where(flag: flag_type).group_by(&:repo)

all_flags.each do |repo, flag_array|
all_flags[repo] = {}
flag_array.each do |flag|
all_flags[repo][flag.architecture.try(&:name)] = flag
end
end
end
end
30 changes: 1 addition & 29 deletions src/api/app/models/flag.rb
Expand Up @@ -40,14 +40,7 @@ def validate_duplicates
end

def self.default_status(flag_name)
case flag_name.to_sym
when :lock, :debuginfo
'disable'
when :build, :publish, :useforbuild, :binarydownload, :access
'enable'
else
'disable'
end
FlagHelper.default_for(flag_name)
end

def discard_forbidden_project_cache
Expand Down Expand Up @@ -121,20 +114,6 @@ def to_xml(builder)
builder.send(status.to_s, options)
end

def is_explicit_for?(in_repo, in_arch)
return false unless is_relevant_for?(in_repo, in_arch)

arch = architecture ? architecture.name : nil

return false if arch.nil? && in_arch
return false if arch && in_arch.nil?

return false if repo.nil? && in_repo
return false if repo && in_repo.nil?

true
end

# returns true when flag is relevant for the given repo/arch combination
def is_relevant_for?(in_repo, in_arch)
arch = architecture ? architecture.name : nil
Expand All @@ -155,13 +134,6 @@ def specifics
count
end

def to_s
ret = status
ret += " arch=#{architecture.name}" unless architecture.nil?
ret += " repo=#{repo}" unless repo.nil?
ret
end

# TODO: used by bento. Remove when dropping old UI.
def fullname
ret = flag
Expand Down
55 changes: 55 additions & 0 deletions src/api/app/models/flag/specified_flags.rb
@@ -0,0 +1,55 @@
class Flag::SpecifiedFlags
def initialize(prj_or_pkg, flag_type)
@flags = {}
@flags[:object] = specified_flags(prj_or_pkg, flag_type)
@flags[:default] = Flag.new(flag: flag_type, status: FlagHelper.default_for(flag_type))
@flags[:project] = specified_flags(prj_or_pkg.project, flag_type) if prj_or_pkg.is_a?(Package)
end

def effective_flag(repository, architecture)
dig_flag(:effective_flag_for, repository, architecture)
end

def default_flag(repository, architecture)
dig_flag(:default_flag_for, repository, architecture)
end

def set_by_user?(repository, architecture)
@flags[:object].dig(repository, architecture).present?
end

private

def dig_flag(callback_function, repository, architecture)
flag = send(callback_function, @flags[:object], repository, architecture)
flag ||= send(callback_function, @flags[:project], repository, architecture) if @flags[:project]
flag || @flags[:default]
end

def effective_flag_for(flags, repository, architecture)
flags.dig(repository, architecture) || flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil)
end

# It finds out how the table would look like if the flag was not set.
# In case of specific flags this means lookup the rest, for flags that are
# only specifying one direction, we need to look at the overall state
# (default state is handled in the function above).
def default_flag_for(flags, repository, architecture)
if repository && architecture
flags.dig(repository, nil) || flags.dig(nil, architecture) || flags.dig(nil, nil)
elsif architecture || repository
flags.dig(nil, nil)
end
end

def specified_flags(prj_or_pkg, flag_type)
all_flags = prj_or_pkg.flags.where(flag: flag_type).group_by(&:repo)

all_flags.each do |repo, flag_array|
all_flags[repo] = {}
flag_array.each do |flag|
all_flags[repo][flag.architecture.try(&:name)] = flag
end
end
end
end
1 change: 0 additions & 1 deletion src/api/app/models/package.rb
Expand Up @@ -866,7 +866,6 @@ def activity
end

define_method :get_flags, GetFlags.instance_method(:get_flags)
define_method :specified_flags, GetFlags.instance_method(:specified_flags)

def open_requests_with_package_as_source_or_target
rel = BsRequest.where(state: [:new, :review, :declined]).joins(:bs_request_actions)
Expand Down
5 changes: 3 additions & 2 deletions src/api/app/models/package_build_status.rb
Expand Up @@ -21,6 +21,7 @@ def result(opts = {})
raise NoRepositoriesFound if tocheck_repos.empty?

@result = {}
@pkg_flags = Flag::SpecifiedFlags.new(@pkg, 'build')
tocheck_repos.each do |srep|
check_repo_status(srep)
end
Expand Down Expand Up @@ -69,8 +70,8 @@ def check_repo_arch_status(srep, arch)

# if the package does not appear in build history, check flags
unless @everbuilt
buildflag = @pkg.find_flag_state('build', srep['name'], arch)
@buildcode = 'disabled' if buildflag == 'disable'
buildflag = @pkg_flags.effective_flag(srep['name'], arch)
@buildcode = 'disabled' if buildflag.status == 'disable'
end

gather_current_buildcode(srep, arch) unless @buildcode
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/project.rb
Expand Up @@ -681,7 +681,6 @@ def to_axml_id
end

define_method :get_flags, GetFlags.instance_method(:get_flags)
define_method :specified_flags, GetFlags.instance_method(:specified_flags)

def can_be_released_to_project?(target_project)
# is this package source going to a project which is specified as release target ?
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/views/webui2/shared/_flag_popover.html.haml
Expand Up @@ -2,7 +2,7 @@
remote = architecture && repository
flag_id = html_id_for_flag(flag.flag, repository, architecture)

- if !flag_set_by_user?(flags, repository, architecture)
- if !flags.set_by_user?(repository, architecture)
%div
= link_to(create_repository_flag_path(project: project, package: package, flag: flag.flag,
status: 'disable', repository: repository, architecture: architecture), method: :post, remote: remote,
Expand All @@ -26,7 +26,7 @@
%i.fas.fa-check.text-success
Enable
.pt-2
- default_status = default_flag(flags, repository, architecture).status
- default_status = flags.default_flag(repository, architecture).status
= link_to(remove_repository_flag_path(project: project, package: package, flag: flag),
method: :delete, remote: remote, class: 'popover_flag_action', data: { flag_id: flag_id }) do
%i.fas{ class: default_status == 'disable' ? 'fa-ban text-danger' : 'fa-check text-success' }
Expand Down
@@ -1,6 +1,6 @@
:ruby
flag = effective_flag(flags, repository, architecture)
is_flag_set_by_user = flag_set_by_user?(flags, repository, architecture)
flag = flags.effective_flag(repository, architecture)
is_flag_set_by_user = flags.set_by_user?(repository, architecture)
title = flag.status.capitalize
title += is_flag_set_by_user ? ' (set by user)' : ' (calculated)'
if user_can_set_flags
Expand Down

0 comments on commit be95e08

Please sign in to comment.