ci: Fetch all user cerbero branches when matching branch names
authorNirbheek Chauhan <nirbheek@centricular.com>
Tue, 25 Oct 2022 14:27:49 +0000 (19:57 +0530)
committerNirbheek Chauhan <nirbheek@centricular.com>
Tue, 25 Oct 2022 14:57:23 +0000 (20:27 +0530)
Fixes CI on coordinated merges when the user has more than 20 branches
in their fork, which will happen very easily since new forks will
always have all the branches of the original remote.

```
ci/gitlab/trigger_cerbero_pipeline.py:48: UserWarning: Calling a `list()` method without specifying `get_all=True` or `iterator=True` will return a maximum of 20 items. Your query returned 20 of 37 items. See https://python-gitlab.readthedocs.io/en/v3.9.0/api-usage.html#pagination for more details. If this was done intentionally, then this warning can be supressed by adding the argument `get_all=False` to the `list()` call. (python-gitlab: /usr/local/lib/python3.7/site-packages/gitlab/client.py:979)
  if os.environ["CI_COMMIT_REF_NAME"] in [b.name for b in cerbero.branches.list()]:
```

ci/gitlab/trigger_cerbero_pipeline.py

index 6493a3f..7cccb23 100755 (executable)
@@ -38,26 +38,28 @@ if __name__ == "__main__":
                        private_token=os.environ.get('GITLAB_API_TOKEN'),
                        job_token=os.environ.get('CI_JOB_TOKEN'))
 
-    cerbero = None
-    cerbero_name = None
+    def get_matching_user_project(project, branch):
+        cerbero = gl.projects.get(project)
+        # Search for matching branches, return only if the branch name matches
+        # exactly
+        for b in cerbero.branches.list(search=cerbero_branch, iterator=True):
+            if branch == b.name:
+                return cerbero
+        return None
+
     # We do not want to run on (often out of date) user upstream branch
     if os.environ["CI_COMMIT_REF_NAME"] != os.environ['GST_UPSTREAM_BRANCH']:
         try:
             cerbero_name = f'{os.environ["CI_PROJECT_NAMESPACE"]}/cerbero'
-            cerbero = gl.projects.get(cerbero_name)
-            if os.environ["CI_COMMIT_REF_NAME"] in [b.name for b in cerbero.branches.list()]:
-                cerbero_branch = os.environ["CI_COMMIT_REF_NAME"]
-            else:
-                # No branch with a same name on the user cerbero repo... trigger
-                # on upstream project
-                cerbero = None
+            cerbero_branch = os.environ["CI_COMMIT_REF_NAME"]
+            cerbero = get_matching_user_project(cerbero_name, cerbero_branch)
         except gitlab.exceptions.GitlabGetError:
             pass
 
     if cerbero is None:
         cerbero_name = CERBERO_PROJECT
-        cerbero = gl.projects.get(cerbero_name)
         cerbero_branch = os.environ["GST_UPSTREAM_BRANCH"]
+        cerbero = gl.projects.get(cerbero_name)
 
     fprint(f"-> Triggering on branch {cerbero_branch} in {cerbero_name}\n")