From 67cd8119a1e94602129f8e4d2b35bc60d1bac71a Mon Sep 17 00:00:00 2001
From: mirpedrol <mirp.julia@gmail.com>
Date: Wed, 27 Jul 2022 09:29:25 +0200
Subject: [PATCH 1/5] retrieve las common version of a module for Docker and
 Singularity

---
 nf_core/utils.py | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/nf_core/utils.py b/nf_core/utils.py
index df5e3d5fcb..5305ec2562 100644
--- a/nf_core/utils.py
+++ b/nf_core/utils.py
@@ -701,16 +701,28 @@ def get_tag_date(tag_date):
                 images = response.json()["images"]
                 singularity_image = None
                 docker_image = None
+                all_docker = {}
+                all_singularity = {}
                 for img in images:
-                    # Get most recent Docker and Singularity image
+                    # Get all Docker and Singularity images
                     if img["image_type"] == "Docker":
-                        modification_date = get_tag_date(img["updated"])
-                        if not docker_image or modification_date > get_tag_date(docker_image["updated"]):
-                            docker_image = img
-                    if img["image_type"] == "Singularity":
-                        modification_date = get_tag_date(img["updated"])
-                        if not singularity_image or modification_date > get_tag_date(singularity_image["updated"]):
-                            singularity_image = img
+                        # Obtain version and build
+                        match = re.search(r"(?::)+([A-Za-z\d\-_.]+)", img["image_name"])
+                        if match is not None:
+                            all_docker[match.group(1)] = {"date": get_tag_date(img["updated"]), "image": img}
+                    elif img["image_type"] == "Singularity":
+                        # Obtain version and build
+                        match = re.search(r"(?::)+([A-Za-z\d\-_.]+)", img["image_name"])
+                        if match is not None:
+                            all_singularity[match.group(1)] = {"date": get_tag_date(img["updated"]), "image": img}
+                # Obtain common builds from Docker and Singularity images
+                common_keys = list(all_docker.keys() & all_singularity.keys())
+                for k in common_keys:
+                    # Get the most recent common image
+                    if not docker_image or all_docker[k]["date"] > get_tag_date(docker_image["updated"]):
+                        docker_image = all_docker[k]["image"]
+                    if not singularity_image or all_singularity[k]["date"] > get_tag_date(singularity_image["updated"]):
+                        singularity_image = all_singularity[k]["image"]
                 return docker_image["image_name"], singularity_image["image_name"]
             except TypeError:
                 raise LookupError(f"Could not find docker or singularity container for {package}")

From c8b1311e3f417fff169b6d463e6c40682ae1f832 Mon Sep 17 00:00:00 2001
From: mirpedrol <mirp.julia@gmail.com>
Date: Wed, 27 Jul 2022 09:40:34 +0200
Subject: [PATCH 2/5] update changelog

---
 CHANGELOG.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a7aea79fbb..a0a3646c1d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -53,6 +53,7 @@
 - Fix inconsistencies in the `--save-diff` flag `nf-core modules update`. Refactor `nf-core modules update` ([#1536](https://github.com/nf-core/tools/pull/1536))
 - Fix bug in `ModulesJson.check_up_to_date` causing it to ask for the remote of local modules
 - Make `nf-core modules update --save-diff` work when files were created or removed ([#1694](https://github.com/nf-core/tools/issues/1694))
+- Get the latest common build for Docker and Singularity containers of a module ([#1702](https://github.com/nf-core/tools/pull/1702))
 
 ## [v2.4.1 - Cobolt Koala Patch](https://github.com/nf-core/tools/releases/tag/2.4) - [2022-05-16]
 

From 8fd6a625d9b82c1756fe0ebb7580f117799aae12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BAlia=20Mir=20Pedrol?= <mirp.julia@gmail.com>
Date: Thu, 28 Jul 2022 09:13:51 +0200
Subject: [PATCH 3/5] Apply suggestion from code review

Co-authored-by: Erik Danielsson <53212377+ErikDanielsson@users.noreply.github.com>
---
 nf_core/utils.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/nf_core/utils.py b/nf_core/utils.py
index 5305ec2562..dac87775a8 100644
--- a/nf_core/utils.py
+++ b/nf_core/utils.py
@@ -717,12 +717,14 @@ def get_tag_date(tag_date):
                             all_singularity[match.group(1)] = {"date": get_tag_date(img["updated"]), "image": img}
                 # Obtain common builds from Docker and Singularity images
                 common_keys = list(all_docker.keys() & all_singularity.keys())
+                current_date = None
                 for k in common_keys:
                     # Get the most recent common image
-                    if not docker_image or all_docker[k]["date"] > get_tag_date(docker_image["updated"]):
+                    date = max(get_tag_date(all_docker[k]["date"]]), get_tag_date(all_docker[k]["date"]))
+                    if docker_image is None or current_date < date:
                         docker_image = all_docker[k]["image"]
-                    if not singularity_image or all_singularity[k]["date"] > get_tag_date(singularity_image["updated"]):
                         singularity_image = all_singularity[k]["image"]
+                        current_date = date
                 return docker_image["image_name"], singularity_image["image_name"]
             except TypeError:
                 raise LookupError(f"Could not find docker or singularity container for {package}")

From 445402a932c47791161db5caf490e35c126d8f37 Mon Sep 17 00:00:00 2001
From: mirpedrol <mirp.julia@gmail.com>
Date: Thu, 28 Jul 2022 09:20:58 +0200
Subject: [PATCH 4/5] fix typo

---
 nf_core/utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nf_core/utils.py b/nf_core/utils.py
index dac87775a8..1d1a01bd0e 100644
--- a/nf_core/utils.py
+++ b/nf_core/utils.py
@@ -720,7 +720,7 @@ def get_tag_date(tag_date):
                 current_date = None
                 for k in common_keys:
                     # Get the most recent common image
-                    date = max(get_tag_date(all_docker[k]["date"]]), get_tag_date(all_docker[k]["date"]))
+                    date = max(get_tag_date(all_docker[k]["date"]), get_tag_date(all_docker[k]["date"]))
                     if docker_image is None or current_date < date:
                         docker_image = all_docker[k]["image"]
                         singularity_image = all_singularity[k]["image"]

From 30f7ea064870a0cbc248fbfb75467c13a53ed7b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BAlia=20Mir=20Pedrol?= <mirp.julia@gmail.com>
Date: Thu, 28 Jul 2022 10:01:14 +0200
Subject: [PATCH 5/5] Update nf_core/utils.py

Co-authored-by: Erik Danielsson <53212377+ErikDanielsson@users.noreply.github.com>
---
 nf_core/utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nf_core/utils.py b/nf_core/utils.py
index 1d1a01bd0e..9881ed0664 100644
--- a/nf_core/utils.py
+++ b/nf_core/utils.py
@@ -720,7 +720,7 @@ def get_tag_date(tag_date):
                 current_date = None
                 for k in common_keys:
                     # Get the most recent common image
-                    date = max(get_tag_date(all_docker[k]["date"]), get_tag_date(all_docker[k]["date"]))
+                    date = max(all_docker[k]["date"], all_docker[k]["date"])
                     if docker_image is None or current_date < date:
                         docker_image = all_docker[k]["image"]
                         singularity_image = all_singularity[k]["image"]