From 0e644c0ac723dfcf3d215a99408965989f2d98f4 Mon Sep 17 00:00:00 2001
From: Michael Calmer <michael.calmer@suse.com>
Date: Fri, 6 Dec 2024 16:37:06 +0100
Subject: [PATCH 1/2] Make the list for package actions unique so it can be
 passed to salt (bsc#1232042)

---
 .../rhn/manager/action/ActionManager.java       | 17 ++++++++++++++++-
 ...mcalmer.Manager-4.3-unique-pkglist-in-action |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 java/spacewalk-java.changes.mcalmer.Manager-4.3-unique-pkglist-in-action

diff --git a/java/code/src/com/redhat/rhn/manager/action/ActionManager.java b/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
index 3cdc484115c..955a2b1dc43 100644
--- a/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
+++ b/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
@@ -2006,10 +2006,25 @@ private static List<Map<String, Long>> removeDuplicatedName(List<Map<String, Lon
     public static void addPackageActionDetails(Collection<Action> actions,
             List<Map<String, Long>> packageMaps) {
         if (packageMaps != null) {
+            List<Map<String, Long>> pkgMaps;
+
+            if (actions.iterator().next().getActionType().equals(ActionFactory.TYPE_PACKAGES_REMOVE)) {
+                // our packages.pkgremove state is handling duplicates
+                pkgMaps = packageMaps;
+            }
+            else {
+                Map<String, Map<String, Long>> uPkgMap = new HashMap<>();
+                // For other salt pkg states (pkg.installed), name + arch must be unique.
+                for (Map<String, Long> p : packageMaps) {
+                    String key = p.get("name_id") + "_" + p.getOrDefault("arch_id", 0L);
+                    uPkgMap.put(key, p);
+                }
+                pkgMaps = new ArrayList<>(uPkgMap.values());
+            }
             List<Map<String, Object>> paramList =
                 actions.stream().flatMap(action -> {
                     String packageParameter = getPackageParameter(action);
-                    return packageMaps.stream().map(packageMap -> {
+                    return pkgMaps.stream().map(packageMap -> {
                         Map<String, Object> params = new HashMap<>();
                         params.put("action_id", action.getId());
                         params.put("name_id", packageMap.get("name_id"));
diff --git a/java/spacewalk-java.changes.mcalmer.Manager-4.3-unique-pkglist-in-action b/java/spacewalk-java.changes.mcalmer.Manager-4.3-unique-pkglist-in-action
new file mode 100644
index 00000000000..8066246cfc9
--- /dev/null
+++ b/java/spacewalk-java.changes.mcalmer.Manager-4.3-unique-pkglist-in-action
@@ -0,0 +1,2 @@
+- Make the list for package actions unique so it can be passed
+  to salt (bsc#1232042)

From 8436cd7dcc5244c0c157da3e7f122dcd2f7725b6 Mon Sep 17 00:00:00 2001
From: Michael Calmer <michael.calmer@suse.com>
Date: Wed, 11 Dec 2024 13:24:28 +0100
Subject: [PATCH 2/2] filter for noarch packages

---
 .../redhat/rhn/manager/action/ActionManager.java  | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/java/code/src/com/redhat/rhn/manager/action/ActionManager.java b/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
index 955a2b1dc43..0073d297f68 100644
--- a/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
+++ b/java/code/src/com/redhat/rhn/manager/action/ActionManager.java
@@ -2013,11 +2013,22 @@ public static void addPackageActionDetails(Collection<Action> actions,
                 pkgMaps = packageMaps;
             }
             else {
+                long noarch = PackageFactory.lookupPackageArchByLabel("noarch").getId();
+                long all = PackageFactory.lookupPackageArchByLabel("all-deb").getId();
                 Map<String, Map<String, Long>> uPkgMap = new HashMap<>();
                 // For other salt pkg states (pkg.installed), name + arch must be unique.
                 for (Map<String, Long> p : packageMaps) {
-                    String key = p.get("name_id") + "_" + p.getOrDefault("arch_id", 0L);
-                    uPkgMap.put(key, p);
+                    long archId = p.getOrDefault("arch_id", noarch);
+                    String name = String.valueOf(p.get("name_id"));
+                    if (archId == noarch || archId == all) {
+                        if (uPkgMap.keySet().stream().noneMatch(k -> k.startsWith(name + "."))) {
+                            uPkgMap.put(name, p);
+                        }
+                    }
+                    else if (!uPkgMap.containsKey(name)) {
+                        String key = name + "." + p.get("arch_id");
+                        uPkgMap.put(key, p);
+                    }
                 }
                 pkgMaps = new ArrayList<>(uPkgMap.values());
             }
