Skip to content

Commit

Permalink
Merge pull request #2292 from mmohring/master
Browse files Browse the repository at this point in the history
[webui][api] fix the color of the build flag preview when changing a flag
  • Loading branch information
hennevogel committed Nov 11, 2016
2 parents 2413109 + 969fac3 commit 40c9b34
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/api/app/mixins/get_flags.rb
Expand Up @@ -19,7 +19,7 @@ def get_flags(flag_type)
# If there is no flag create a temporary one.
unless flag
flag = flags.new( flag: flag_type, repo: repository, architecture: architecture )
flag.status = flag.default_status
flag.status = flag.effective_status
end
the_flags[repository] << flag
end
Expand Down
57 changes: 43 additions & 14 deletions src/api/app/models/flag.rb
Expand Up @@ -54,29 +54,58 @@ def discard_forbidden_project_cache
Relationship.discard_cache if flag == 'access'
end

def default_status
def compute_status(variant)
all_flag = main_object.flags.find_by("flag = ? AND repo IS NULL AND architecture_id IS NULL", flag)
repo_flag = main_object.flags.find_by("flag = ? AND repo = ? AND architecture_id IS NULL", flag, repo)
arch_flag = main_object.flags.find_by("flag = ? AND repo IS NULL AND architecture_id = ?", flag, architecture_id)
same_flag = main_object.flags.find_by("flag = ? AND repo = ? AND architecture_id = ?", flag, repo, architecture_id)
# Package settings only override project settings...
if main_object.kind_of? Package
# do the same_flag check first to see if all_flag or same_flag had been set on package level, they *both* overwrite the project level
same_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id = ?", flag, repo, architecture_id) unless
all_flag || same_flag || repo_flag || arch_flag
repo_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id IS NULL", flag, repo) unless
all_flag || repo_flag || arch_flag
arch_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id = ?", flag, architecture_id) unless
all_flag || arch_flag
all_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id IS NULL", flag) unless all_flag
if variant == 'effective'
same_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id = ?", flag, repo, architecture_id) unless
all_flag || same_flag || repo_flag || arch_flag
repo_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id IS NULL", flag, repo) unless
all_flag || repo_flag || arch_flag
arch_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id = ?", flag, architecture_id) unless
all_flag || arch_flag
all_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id IS NULL", flag) unless all_flag
elsif variant == 'default'
same_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id = ?", flag, repo, architecture_id) unless same_flag
repo_flag = main_object.project.flags.find_by("flag = ? AND repo = ? AND architecture_id IS NULL", flag, repo) unless repo_flag
arch_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id = ?", flag, architecture_id) unless arch_flag
all_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id IS NULL", flag) unless all_flag
end
end

if variant == 'effective'
return same_flag.status if same_flag
return repo_flag.status if repo_flag
return arch_flag.status if arch_flag
return all_flag.status if all_flag
elsif variant == 'default'
if same_flag
return repo_flag.status if repo_flag
return arch_flag.status if arch_flag
end
if same_flag || arch_flag || repo_flag
return all_flag.status if all_flag
end
if main_object.kind_of? Package
all_flag = main_object.project.flags.find_by("flag = ? AND repo IS NULL AND architecture_id IS NULL", flag)
return all_flag.status if all_flag
end
end

return same_flag.status if same_flag
return repo_flag.status if repo_flag
return arch_flag.status if arch_flag
return all_flag.status if all_flag
return Flag.default_status(flag)
end
private :compute_status

def default_status
return compute_status('default')
end

def effective_status
return compute_status('effective')
end

def has_children
return true if repo.blank? && architecture.blank?
Expand Down
Expand Up @@ -9,7 +9,7 @@
<div class="hidden flagtoggle">
<% if flag.new_record? %>
<% if flag.status == 'enable' %>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_disable_grey icon_24"></div></div>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_disable_blue icon_24"></div></div>
<%= link_to 'Disable', { action: :create_flag,
project: @project,
package: @package,
Expand All @@ -19,7 +19,7 @@
architecture: flag.arch },
id: "#{flag.fullname}_disable", class: "flag_spinner_trigger_#{flag.flag}", method: :post, remote: remote %>
<% else %>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_enable_grey icon_24"></div></div>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_enable_blue icon_24"></div></div>
<%= link_to 'Enable', { action: :create_flag,
project: @project,
package: @package,
Expand All @@ -37,8 +37,8 @@
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_enable_blue icon_24"></div></div>
<%= link_to 'Enable', { action: :toggle_flag, project: @project, package: @package, flag: flag }, id: "#{flag.fullname}_enable", class: "flag_spinner_trigger_#{flag.flag}", method: :put, remote: remote %>
<% end %>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_<%= Flag.default_status(flag.flag) %>_blue icon_24"></div></div>
<%= link_to "Take default (#{Flag.default_status(flag.flag)})", { action: :remove_flag, project: @project, package: @package, flag: flag }, id: "#{flag.fullname}_remove", class: "flag_spinner_trigger_#{flag.flag}", method: :delete, remote: remote %>
<div class="iconwrapper"><div class="icons-<%= flag.flag %>_<%= flag.default_status %>_grey icon_24"></div></div>
<%= link_to "Take default (#{flag.default_status})", { action: :remove_flag, project: @project, package: @package, flag: flag }, id: "#{flag.fullname}_remove", class: "flag_spinner_trigger_#{flag.flag}", method: :delete, remote: remote %>
<% end %>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/api/test/unit/code_quality_test.rb
Expand Up @@ -87,7 +87,7 @@ def setup
'PersonController#internal_register' => 112.01,
'Package#find_changed_issues' => 93.74,
'Package#close_requests' => 84.82,
'Flag#default_status' => 90.56,
'Flag#compute_status' => 145.55,
'PublicController#binary_packages' => 126.16,
'Repository#cleanup_before_destroy' => 82.98,
'RequestController#render_request_collection' => 82.38,
Expand Down
177 changes: 167 additions & 10 deletions src/api/test/unit/project_test.rb
Expand Up @@ -122,8 +122,12 @@ def test_flags_inheritance
<title>Iggy's Flag Testing Project</title>
<description>dummy</description>
<build>
<enable repository='SLE_11_SP4' />
<disable/>
</build>
<useforbuild>
<disable/>
</useforbuild>
<repository name='openSUSE_Leap_42.1'>
<arch>x86_64</arch>
</repository>
Expand All @@ -145,37 +149,190 @@ def test_flags_inheritance
project.store
project.reload

# test if all project build flags are 'disable' for the project
project.get_flags('build').each do |repo|
repo[1].each do |flag|
assert_equal 'disable', flag.status
end
end
assert_equal 5, project.get_flags('build').size
assert_equal 3, project.get_flags('build')["all"].size

flag_test = project.get_flags('build')["SLE_11_SP4"][0]
assert_equal 'enable', flag_test.status
assert_equal 'enable', flag_test.effective_status
assert_equal 'disable', flag_test.default_status

flag_all = project.get_flags('build')["all"][0]
assert_equal 'disable', flag_all.status
assert_equal 'disable', flag_all.effective_status
assert_equal 'enable', flag_all.default_status

assert_equal 5, project.get_flags('useforbuild').size
assert_equal 3, project.get_flags('useforbuild')["all"].size

flag_useforbuild_all = project.get_flags('useforbuild')["all"][0]
assert_equal 'disable', flag_useforbuild_all.status
assert_equal 'disable', flag_useforbuild_all.effective_status
assert_equal 'enable', flag_useforbuild_all.default_status

# package is given as axml
axml = Xmlhash.parse(
"<package name='test2' project='home:Iggy:flagtest'>
<title>My Test package 2</title>
<description></description>
<build>
<disable repository='SLE_12_SP1' />
<enable/>
</build>
<useforbuild>
<enable/>
</useforbuild>
</package>"
)

package2.update_from_xml(axml)
package2.store
package2.reload

# the all build 'enable' for the package shall overwrite the project setting
package2.get_flags('build').each do |repo|
repo[1].each do |flag|
assert_equal 'enable', flag.status
assert_equal 5, package2.get_flags('build').size
assert_equal 3, package2.get_flags('build')["all"].size

flag_test = package2.get_flags('build')["SLE_12_SP1"][0]
assert_equal 'disable', flag_test.status
assert_equal 'disable', flag_test.effective_status
assert_equal 'enable', flag_test.default_status

flag_build_all = package2.get_flags('build')["all"][0]
assert_equal 'enable', flag_build_all.status
assert_equal 'enable', flag_build_all.effective_status
assert_equal 'disable', flag_build_all.default_status

assert_equal 5, package2.get_flags('useforbuild').size
assert_equal 3, package2.get_flags('useforbuild')["all"].size

flag_useforbuild_all = package2.get_flags('useforbuild')["all"][0]
assert_equal 'enable', flag_useforbuild_all.status
assert_equal 'enable', flag_useforbuild_all.effective_status
assert_equal 'disable', flag_useforbuild_all.default_status

package3 = project.packages.create(name: "test3")
# package is given as axml
axml = Xmlhash.parse(
"<package name='test3' project='home:Iggy:flagtest'>
<title>My Test package 3</title>
<description></description>
<build>
<enable repository='SLE_11_SP4' />
<enable repository='SLE_12_SP1' />
<disable arch='i586' repository='SLE_12_SP1' />
<disable arch='i586' />
</build>
<useforbuild>
<enable/>
</useforbuild>
</package>"
)

package3.update_from_xml(axml)
package3.store
package3.reload

assert_equal 5, package3.get_flags('build').size
assert_equal 3, package3.get_flags('build')["all"].size

flag_test = package3.get_flags('build')["SLE_12_SP1"][1]
assert_equal 'i586', flag_test.architecture.name
assert_equal 'disable', flag_test.status
assert_equal 'disable', flag_test.effective_status
assert_equal 'enable', flag_test.default_status

flag_test = package3.get_flags('build')["SLE_11_SP4"][0]
assert_equal 'enable', flag_test.status
assert_equal 'enable', flag_test.effective_status
assert_equal 'disable', flag_test.default_status

flag_test = package3.get_flags('build')["all"][0]
assert_equal 'disable', flag_test.status
assert_equal 'disable', flag_test.effective_status
assert_equal 'disable', flag_test.default_status

# now the final test: check all flags default in project and package
project2 = Project.create(name: "home:Iggy:flagtest2")
axml = Xmlhash.parse(
"<project name='home:Iggy:flagtest2'>
<title>Iggy's Flag Testing Project 2</title>
<description>project to test all flag defaults</description>
<build>
</build>
<publish>
</publish>
<useforbuild>
</useforbuild>
<debuginfo>
</debuginfo>
<repository name='openSUSE_Leap_42.1'>
<arch>x86_64</arch>
</repository>
<repository name='openSUSE_Leap_42.2'>
<arch>x86_64</arch>
</repository>
<repository name='SLE_11_SP4'>
<arch>i586</arch>
<arch>x86_64</arch>
</repository>
<repository name='SLE_12_SP1'>
<arch>i586</arch>
<arch>x86_64</arch>
</repository>
</project>"
)

project2.update_from_xml(axml)
project2.store
project2.reload

# test if all project flags are default for a new project
allflags=['build', 'publish', 'useforbuild', 'binarydownload', 'access', 'lock', 'debuginfo']
allflags.each do |flagtype|
project2.get_flags(flagtype).each do |repo|
repo[1].each do |flag|
assert_equal Flag.default_status(flagtype), flag.effective_status
assert_equal Flag.default_status(flagtype), flag.default_status
assert_equal Flag.default_status(flagtype), flag.status
end
end
end

package4 = project2.packages.create(name: "test4")
axml = Xmlhash.parse(
"<package name='test4' project='home:Iggy:flagtest2'>
<title>My Test package 4</title>
<description>package to test all default flags</description>
<build>
</build>
<publish>
</publish>
<useforbuild>
</useforbuild>
<debuginfo>
</debuginfo>
</package>"
)

package4.update_from_xml(axml)
package4.store
package4.reload

# test if all package flags are default for a new package
allflags.each do |flagtype|
assert_equal 5, package4.get_flags(flagtype).size
package4.get_flags(flagtype).each do |repo|
repo[1].each do |flag|
assert_equal Flag.default_status(flagtype), flag.status
assert_equal Flag.default_status(flagtype), flag.effective_status
assert_equal Flag.default_status(flagtype), flag.default_status
end
end
end

# this is the end
project.destroy
project2.destroy
end

def test_release_targets_ng
Expand Down

0 comments on commit 40c9b34

Please sign in to comment.