Repository: canonical/charmcraft

Issue:
The Status command confuses branches between architectures
When the charm is multi-architecture, and a branch is created, e.g.
```
charmcraft release testfacu-charm -r 1 --channel=edge/branch123
```
...there will be a branch for each architecture. Example of the channel map (after also releasing just to `edge`):
```
$ env/bin/python -m charmcraft status testfacu-charm 
Track    Base                    Channel         Version    Revision    Expires at                                                                                                          
latest   ubuntu 18.04 (amd64)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (arm64)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (armhf)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (i386)     stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (powerpc)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (ppc64el)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (riscv64)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (s390x)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
```

However, Charmcraft is confused because Charmhub returns the 7 releases to branches (one for each architecture) and when building the map that architecture is not verified and then all 7 releases appear for each architecture.

This is what is currently producing (wrong!):
```
Track    Base                    Channel         Version    Revision    Expires at                                                                                                          
latest   ubuntu 18.04 (amd64)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (arm64)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (armhf)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (i386)     stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (powerpc)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (ppc64el)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (riscv64)  stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
         ubuntu 18.04 (s390x)    stable          -          -                                                                                                                               
                                 candidate       -          -                                                                                                                               
                                 beta            -          -                                                                                                                               
                                 edge            1          1                                                                                                                               
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z                                                                                                
                                 edge/branch123  1          1           2022-09-17T00:00:00Z               
```


Discussion:

Patch:
diff --git a/charmcraft/commands/store/__init__.py b/charmcraft/commands/store/__init__.py
index cbccb213..b964d1d4 100644
--- a/charmcraft/commands/store/__init__.py
+++ b/charmcraft/commands/store/__init__.py
@@ -878,7 +878,10 @@ class StatusCommand(BaseCommand):
         per_track = {}
         branch_present = False
         for channel in channels:
-            nonbranches_list, branches_list = per_track.setdefault(channel.track, ([], []))
+            # the branches list is really a dict just to deduplicate them (sometimes they come
+            # repeated because of a Charmhub bug) without losing their order (that's why
+            # a set is not used)
+            nonbranches_list, branches = per_track.setdefault(channel.track, ([], {}))
             if channel.branch is None:
                 # insert branch right after its fallback
                 for idx, stored in enumerate(nonbranches_list, 1):
@@ -888,7 +891,7 @@ class StatusCommand(BaseCommand):
                 else:
                     nonbranches_list.append(channel)
             else:
-                branches_list.append(channel)
+                branches[channel] = None
                 branch_present = True
 
         headers = ["Track", "Base", "Channel", "Version", "Revision"]


Test patch:
diff --git a/tests/commands/test_store_commands.py b/tests/commands/test_store_commands.py
index 9c70c9eb..c171bf98 100644
--- a/tests/commands/test_store_commands.py
+++ b/tests/commands/test_store_commands.py
@@ -2624,6 +2624,56 @@ def test_status_multiplebases_everything_combined(emitter, store_mock, config):
     emitter.assert_messages(expected)
 
 
+def test_status_multiplebases_multiplebranches(emitter, store_mock, config):
+    """Validate specific mix between bases and branches.
+
+    This exposes a bug in Charmhub: https://bugs.launchpad.net/snapstore-server/+bug/1994613
+    """
+    other_base = Base(architecture="i386", channel="20.04", name="ubuntu")
+    tstamp = dateutil.parser.parse("2020-07-03T20:30:40Z")
+    channel_map = [
+        _build_release(revision=1, channel="latest/edge"),
+        _build_release(revision=1, channel="latest/edge/fix", expires_at=tstamp),
+        _build_release(revision=1, channel="latest/edge", base=other_base),
+        _build_release(revision=1, channel="latest/edge/fix", expires_at=tstamp, base=other_base),
+    ]
+    channels = _build_channels()
+    extra_channel = Channel(
+        name="latest/edge/fix",
+        fallback="latest/edge",
+        track="latest",
+        risk="edge",
+        branch="fix",
+    )
+    channels.extend([extra_channel, extra_channel])  # twice!! this is the reported bug in Charmhub
+    revisions = [
+        _build_revision(revno=1, version="v1"),
+    ]
+    store_mock.list_releases.return_value = (channel_map, channels, revisions)
+
+    args = Namespace(name="testcharm", format=False)
+    StatusCommand(config).run(args)
+
+    assert store_mock.mock_calls == [
+        call.list_releases("testcharm"),
+    ]
+
+    expected = [
+        "Track    Base                  Channel    Version    Revision    Expires at",
+        "latest   ubuntu 20.04 (amd64)  stable     -          -",
+        "                               candidate  -          -",
+        "                               beta       -          -",
+        "                               edge       v1         1",
+        "                               edge/fix   v1         1           2020-07-03T20:30:40Z",
+        "         ubuntu 20.04 (i386)   stable     -          -",
+        "                               candidate  -          -",
+        "                               beta       -          -",
+        "                               edge       v1         1",
+        "                               edge/fix   v1         1           2020-07-03T20:30:40Z",
+    ]
+    emitter.assert_messages(expected)
+
+
 @pytest.mark.parametrize("formatted", [None, JSON_FORMAT])
 def test_status_with_base_in_none(emitter, store_mock, config, formatted):
     """Support the case of base being None."""