From e95e116123db87abf431a1d0b36da17f753da08d Mon Sep 17 00:00:00 2001
From: Amir MOHAMMADI <amir.mohammadi@idiap.ch>
Date: Tue, 18 Feb 2020 17:37:00 +0100
Subject: [PATCH] Automatically detect changes in bob.devtools

---
 bob/devtools/changelog.py         | 38 +++++++++++++++++++++++--------
 bob/devtools/scripts/changelog.py | 32 ++++++++++++++++++--------
 2 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/bob/devtools/changelog.py b/bob/devtools/changelog.py
index ddba8b29..355ba2b8 100644
--- a/bob/devtools/changelog.py
+++ b/bob/devtools/changelog.py
@@ -43,7 +43,7 @@ def _sort_tags(tags, reverse):
 def get_file_from_gitlab(gitpkg, path, ref="master"):
     """Retrieves a file from a Gitlab repository, returns a (StringIO) file."""
 
-    return io.StringIO(gitpkg.files.get(file_path=path, ref=branch).decode())
+    return io.StringIO(gitpkg.files.get(file_path=path, ref=ref).decode())
 
 
 def get_last_tag(package):
@@ -229,18 +229,21 @@ def _write_mergerequests_range(f, pkg_name, mrs):
         f.write("\n")
 
 
-def write_tags_with_commits(f, gitpkg, since, mode):
-    """Writes all tags and commits of a given package to the output file.
+def get_changes_since(gitpkg, since):
+    """Gets the list of MRs, tags, and commits since the provided date
 
-    Args:
+    Parameters
+    ----------
+    gitpkg : object
+        A gitlab pakcage object
+    since : object
+        A parsed date
 
-        f: A :py:class:`File` ready to be written at
-        gitpkg: A pointer to the gitlab package object
-        since: Starting date (as a datetime object)
-        mode: One of mrs (merge-requests), commits or tags indicating how to
-              list entries in the changelog for this package
+    Returns
+    -------
+    tuple
+        mrs, tags, commits
     """
-
     # get tags since release and sort them
     tags = gitpkg.tags.list()
 
@@ -265,6 +268,21 @@ def write_tags_with_commits(f, gitpkg, since, mode):
             )
         )
     )
+    return mrs, tags, commits
+
+def write_tags_with_commits(f, gitpkg, since, mode):
+    """Writes all tags and commits of a given package to the output file.
+
+    Args:
+
+        f: A :py:class:`File` ready to be written at
+        gitpkg: A pointer to the gitlab package object
+        since: Starting date (as a datetime object)
+        mode: One of mrs (merge-requests), commits or tags indicating how to
+              list entries in the changelog for this package
+    """
+    mrs, tags, commits = get_changes_since(gitpkg, since)
+
     f.write("* %s\n" % (gitpkg.attributes["path_with_namespace"],))
 
     # go through tags and writes each with its message and corresponding
diff --git a/bob/devtools/scripts/changelog.py b/bob/devtools/scripts/changelog.py
index aa4fbc35..ae1cd84e 100644
--- a/bob/devtools/scripts/changelog.py
+++ b/bob/devtools/scripts/changelog.py
@@ -1,16 +1,8 @@
 #!/usr/bin/env python
 
-import os
-import sys
-import datetime
-
 import click
 
 from . import bdt
-from ..changelog import get_last_tag_date, write_tags_with_commits
-from ..changelog import parse_date
-from ..release import get_gitlab_instance
-
 from ..log import verbosity_option, get_logger
 
 logger = get_logger(__name__)
@@ -104,6 +96,18 @@ def changelog(target, changelog, group, mode, since):
     starting date is not passed, we'll use the date of the last tagged value or
     the date of the first commit, if no tags are available in the package.
     """
+    import os
+    import sys
+    import datetime
+
+    from ..changelog import (
+        get_last_tag_date,
+        write_tags_with_commits,
+        parse_date,
+        get_changes_since,
+        get_last_tag,
+    )
+    from ..release import get_gitlab_instance
 
     gl = get_gitlab_instance()
 
@@ -125,8 +129,16 @@ def changelog(target, changelog, group, mode, since):
         since = parse_date(since)
 
     # Since tagging packages requires bob.devtools to be tagged first. Add that to the
-    # list as well. Note that bob.devtools can release itself.
-    if len(packages) > 1:
+    # list as well if bob.devtools has changed. Note that bob.devtools can release
+    # itself.
+    def bdt_has_changes():
+        gitpkg = gl.projects.get("bob/bob.devtools")
+        tag = get_last_tag(gitpkg)
+        last_tag_date = parse_date(tag.commit["committed_date"])
+        _, _, commits = get_changes_since(gitpkg, last_tag_date)
+        return len(commits)
+
+    if bdt_has_changes():
         packages.insert(0, "bob/bob.devtools")
 
     # iterates over the packages and dumps required information
-- 
GitLab