Bug 1648591: [build-decision] Add git support to `Repository.get_push_info`; r=aki
authorTom Prince <mozilla@hocat.ca>
Mon, 29 Jun 2020 20:55:22 +0000
changeset 245 7f68cdcb54f4675fb6bc757372776112c13b7413
parent 244 1b263ffda1a1e5675ccc5b1832f892038827b8da
child 246 79dd9f900e373d0ac381798239ed6315b88af5aa
push id177
push usermozilla@hocat.ca
push dateMon, 29 Jun 2020 21:00:20 +0000
treeherderci-admin@2b71b0f85bdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaki
bugs1648591
Bug 1648591: [build-decision] Add git support to `Repository.get_push_info`; r=aki Differential Revision: https://phabricator.services.mozilla.com/D81187
build-decision/src/build_decision/repository.py
--- a/build-decision/src/build_decision/repository.py
+++ b/build-decision/src/build_decision/repository.py
@@ -89,18 +89,45 @@ class Repository:
                 )
             [revision] = push_info["changesets"]
             return {
                 "owner": push_info["user"],
                 "pushlog_id": push_id,
                 "pushdate": push_info["date"],
                 "revision": revision,
             }
+        elif self.repository_type == "git":
+            if revision:
+                raise Exception("Can't get push information for a git revision.")
+            if branch is None:
+                branch = "master"  # FIXME: Use api to get default branch
+            repo_url = self.repo_url
+            if repo_url.startswith("https://github.com/"):
+                url = (
+                    f"https://api.github.com"
+                    f"/repos/{self.repo_path}/git/ref/heads/{branch}"
+                )
+                res = SESSION.get(url)
+                res.raise_for_status()
+                return {
+                    "branch": branch,
+                    "revision": res.json()["object"]["sha"],
+                }
+            elif repo_url.startswith("git@github.com:"):
+                raise Exception(
+                    "Don't know how to determine revision for private github "
+                    "repo: {}".format(repo_url)
+                )
+            else:
+                raise Exception(
+                    "Don't know how to determine revision for for non-github "
+                    "repo: {}".format(repo_url)
+                )
         else:
-            raise ValueError("Cannot get push information from non-hg repository.")
+            raise Exception("Unknown repository_type {}!".format(self.repository_type))
 
     @property
     def repo_path(self):
         if self.repository_type == "hg" and self.repo_url.startswith(
             "https://hg.mozilla.org/"
         ):
             return self.repo_url.replace("https://hg.mozilla.org/", "", 1).rstrip("/")
         elif self.repository_type == "git" and self.repo_url.startswith(