Repository: mozilla/bugbug

Issue:
Add the ability to fetch Phabricator Project Tag data for a commit
There's an interest from QA in gathering test policy data for commits so that they can understand which bugs they are tracking are getting automated tests, and which aren't.

Bugbug feels like the right place to gather this data, since it already knows about commit data for a bug. If we had this, we could build a tool to help them track test policy status across feature metabugs (and maybe ultimately tie in other data source like codecov / risk analysis).


Discussion:
For examples of querying Phabricator, see PhabricatorClient at https://github.com/mozilla-conduit/lando-api

Resolving patch:
diff --git a/bugbug/phabricator.py b/bugbug/phabricator.py
new file mode 100644
--- /dev/null
+++ b/bugbug/phabricator.py
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this file,
+# You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from libmozdata.phabricator import PhabricatorAPI
+
+DEPLOYMENT_URL = None
+API_KEY = None
+
+PROJECTS = {
+    "PHID-PROJ-h7y4cs7m2o67iczw62pp": "testing-approved",
+    "PHID-PROJ-e4fcjngxcws3egiecv3r": "testing-exception-elsewhere",
+    "PHID-PROJ-iciyosoekrczpf2a4emw": "testing-exception-other",
+    "PHID-PROJ-zjipshabawolpkllehvg": "testing-exception-ui",
+    "PHID-PROJ-cspmf33ku3kjaqtuvs7g": "testing-exception-unchanged",
+}
+
+
+def set_api_key(url, api_key):
+    global DEPLOYMENT_URL, API_KEY
+    DEPLOYMENT_URL = url
+    API_KEY = api_key
+
+
+def get(rev_id):
+    assert DEPLOYMENT_URL is not None
+    assert API_KEY is not None
+
+    phabricator_api = PhabricatorAPI(API_KEY, DEPLOYMENT_URL)
+    return phabricator_api.load_revision(rev_id=rev_id, attachments={"projects": True})
diff --git a/bugbug/repository.py b/bugbug/repository.py
--- a/bugbug/repository.py
+++ b/bugbug/repository.py
@@ -12,6 +12,7 @@
 import math
 import os
 import pickle
+import re
 import shelve
 import subprocess
 import sys
@@ -108,6 +109,10 @@
 
 EXT_TO_TYPES = {ext: typ for typ, exts in TYPES_TO_EXT.items() for ext in exts}
 
+PHABRICATOR_REVISION_REGEX = re.compile(
+    "Differential Revision: (https://phabricator.services.mozilla.com/D([0-9]+))"
+)
+
 
 def get_type(path: str) -> str:
     file_name = os.path.basename(path)
@@ -290,6 +295,13 @@ def get_commits(
     )
 
 
+def get_revision_id(commit):
+    match = PHABRICATOR_REVISION_REGEX.search(commit["desc"])
+    if not match:
+        return None
+    return int(match.group(2))
+
+
 def _init_process(repo_dir):
     global HG, REPO_DIR
     REPO_DIR = repo_dir


Test patch:
diff --git a/tests/test_repository.py b/tests/test_repository.py
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -1859,3 +1859,15 @@ def test_get_commits():
     }
     assert len(retrieved_commits) == 10
     assert included_commits.issubset({c["node"] for c in retrieved_commits})
+
+
+def test_get_revision_id():
+    commit = {
+        "desc": "My desc",
+    }
+    assert repository.get_revision_id(commit) is None
+
+    commit = {
+        "desc": "Bug 1667333: Remove unnecessary prefs for mime type checking r=necko-reviewers,evilpie,valentin\n\nDifferential Revision: https://phabricator.services.mozilla.com/D91406",
+    }
+    assert repository.get_revision_id(commit) == 91406