cvetriager: refactoring to common.py

Completed some refactoring by moving common functionality between files
to common.py.

BUG=chromium:1093363
TEST=python setup.py test

Change-Id: I42de75e5761aaceaa2bdbb2d0d7e78c2bba5f5d4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2276099
Reviewed-by: Zubin Mithra <zsm@chromium.org>
Commit-Queue: Wanda Mora <morawand@chromium.org>
Tested-by: Wanda Mora <morawand@chromium.org>
diff --git a/contrib/cvetriager/cvelib/clgenerator.py b/contrib/cvetriager/cvelib/clgenerator.py
index 5053d22..bd57ab4 100644
--- a/contrib/cvetriager/cvelib/clgenerator.py
+++ b/contrib/cvetriager/cvelib/clgenerator.py
@@ -17,7 +17,7 @@
     cl_map = {}
 
     for kern in kernels:
-        branch = f'b{bug_id}-{kern}'
+        branch = common.get_cherry_pick_branch(bug_id, kern)
         kernel_path = os.path.join(os.getenv('CHROMIUMOS_KERNEL'), kern)
 
         common.do_checkout(kern, branch, kernel_path)
diff --git a/contrib/cvetriager/cvelib/common.py b/contrib/cvetriager/cvelib/common.py
index 59332dd..03470b6 100644
--- a/contrib/cvetriager/cvelib/common.py
+++ b/contrib/cvetriager/cvelib/common.py
@@ -21,6 +21,11 @@
     return f'chromeos-{branch}'
 
 
+def get_cherry_pick_branch(bug_id, kernel):
+    """Returns branch name to cherry-pick on."""
+    return f'b{bug_id}-{kernel}'
+
+
 def checkout_branch(kernel, branch, remote, remote_branch, kernel_path):
     """Checks into appropriate branch and keeps it up to date."""
     do_checkout(kernel, branch, kernel_path)
@@ -44,3 +49,16 @@
                               cwd=kernel_path)
     except subprocess.CalledProcessError:
         raise CommonException('Pull failed for %s' % kernel)
+
+
+def get_commit_message(kernel_path, sha):
+    """Returns commit message."""
+    try:
+        cmd = ['git', '-C', kernel_path, 'log', '--format=%B', '-n', '1', sha]
+        commit_message = subprocess.check_output(cmd, stderr=subprocess.DEVNULL,
+                                                 encoding='utf-8')
+
+        return commit_message.rstrip() +'\n'
+    except subprocess.CalledProcessError:
+        raise CommonException('Could not retrieve commit in kernal path %s for sha %s'
+                                    % (kernel_path, sha))
diff --git a/contrib/cvetriager/cvelib/contextgenerator.py b/contrib/cvetriager/cvelib/contextgenerator.py
index 2714c1a..903072b 100644
--- a/contrib/cvetriager/cvelib/contextgenerator.py
+++ b/contrib/cvetriager/cvelib/contextgenerator.py
@@ -8,7 +8,6 @@
 import re
 
 from cvelib import common
-from cvelib import patchapplier
 
 
 class ContextGeneratorException(Exception):
@@ -24,7 +23,7 @@
 
     def get_fixes_commit(self, linux_sha):
         """Returns Fixes: tag's commit sha."""
-        commit_message = patchapplier.get_commit_message(os.getenv('LINUX'), linux_sha)
+        commit_message = common.get_commit_message(os.getenv('LINUX'), linux_sha)
 
         # Collects 'Fixes: {sha}' string from commit_message.
         m = re.findall('^Fixes: [a-z0-9]{12}', commit_message, re.M)
diff --git a/contrib/cvetriager/cvelib/patchapplier.py b/contrib/cvetriager/cvelib/patchapplier.py
index e08ca72..42dbc91 100644
--- a/contrib/cvetriager/cvelib/patchapplier.py
+++ b/contrib/cvetriager/cvelib/patchapplier.py
@@ -14,23 +14,11 @@
     """Exception raised from patchapplier."""
 
 
-def get_commit_message(kernel_path, sha):
-    """Returns commit message."""
-    try:
-        cmd = ['git', '-C', kernel_path, 'log', '--format=%B', '-n', '1', sha]
-        commit_message = subprocess.check_output(cmd, encoding='utf-8')
-
-        return commit_message.rstrip() +'\n'
-    except subprocess.CalledProcessError:
-        raise PatchApplierException('Could not retrieve commit in kernal path %s for sha %s'
-                                    % (kernel_path, sha))
-
-
 def create_commit_message(kernel_path, sha, bug_id):
     """Generates new commit message."""
     bug_test_line = f'BUG=chromium:{bug_id}\nTEST=CQ\n\n'
 
-    org_msg = get_commit_message(kernel_path, sha)
+    org_msg = common.get_commit_message(kernel_path, sha)
 
     cherry_picked = f'(cherry picked from commit {sha})\n\n'
 
@@ -55,7 +43,7 @@
 
 def create_new_cherry_pick_branch(kernel, bug_id, kernel_path):
     """Creates and checks into new branch for cherry-picking"""
-    branch = f'b{bug_id}-{kernel}'
+    branch = common.get_cherry_pick_branch(bug_id, kernel)
 
     try:
         subprocess.check_call(['git', 'checkout', '-b', branch], stdout=subprocess.DEVNULL,
diff --git a/contrib/cvetriager/tests/patchapplier_test.py b/contrib/cvetriager/tests/patchapplier_test.py
index b214ac2..be26029 100644
--- a/contrib/cvetriager/tests/patchapplier_test.py
+++ b/contrib/cvetriager/tests/patchapplier_test.py
@@ -9,6 +9,7 @@
 import os
 
 from cvelib import patchapplier as pa
+from cvelib import common
 
 
 def get_sha(kernel_path):
@@ -111,7 +112,7 @@
         pa.cherry_pick(kernel_path, sha, bug_id)
 
         # Retrieves new cherry-picked message
-        msg = pa.get_commit_message(kernel_path, get_sha(kernel_path))
+        msg = common.get_commit_message(kernel_path, get_sha(kernel_path))
 
         check = False
         if 'UPSTREAM:' in msg and 'BUG=' in msg and 'TEST=' in msg:
@@ -167,7 +168,7 @@
         bug = '123'
         kernel_versions = [os.path.basename(self.linux_temp)]
 
-        self.assertRaises(pa.PatchApplierException, pa.apply_patch,
+        self.assertRaises(common.CommonException, pa.apply_patch,
                           sha, bug, kernel_versions)
 
     def test_invalid_linux_path(self):