hwid/service: duplicates HWID DB as internal format

In order to simplify the efforts to maintain compatibility, we decide to
store two formats of HWID DB in chromeos-hwid repo.  This CL simply
duplicates the external format while creating CLs.  Further CLs will try
to pass internal information to hwid_repo.CommitHWIDDB method.

BUG=b:230826828
TEST=${FACTORY_REPO}/deploy/cros_hwid_service.sh test

Change-Id: Ibe943dbe04405e0fbd718bcb33e163adb5c605d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/factory/+/3621102
Commit-Queue: Clark Chung <ckclark@chromium.org>
Auto-Submit: Clark Chung <ckclark@chromium.org>
Tested-by: Clark Chung <ckclark@chromium.org>
Reviewed-by: Yong Hong <yhong@chromium.org>
diff --git a/py/hwid/service/appengine/hwid_api_helpers/self_service_helper.py b/py/hwid/service/appengine/hwid_api_helpers/self_service_helper.py
index 3d0c9f9..eb164ef 100644
--- a/py/hwid/service/appengine/hwid_api_helpers/self_service_helper.py
+++ b/py/hwid/service/appengine/hwid_api_helpers/self_service_helper.py
@@ -216,8 +216,9 @@
 
     try:
       cl_number = live_hwid_repo.CommitHWIDDB(
-          request.project, analysis.new_hwid_db_contents, commit_msg,
-          request.reviewer_emails, request.cc_emails, request.auto_approved)
+          name=request.project, hwid_db_contents=analysis.new_hwid_db_contents,
+          commit_msg=commit_msg, reviewers=request.reviewer_emails,
+          cc_list=request.cc_emails, auto_approved=request.auto_approved)
     except hwid_repo.HWIDRepoError:
       logging.exception(
           'Caught an unexpected exception while uploading a HWID CL.')
@@ -556,8 +557,9 @@
                                             f'v3/{project}')
     try:
       cl_number = live_hwid_repo.CommitHWIDDB(
-          project, db_content, commit_msg, request.reviewer_emails,
-          request.cc_emails, request.auto_approved, new_metadata)
+          name=project, hwid_db_contents=db_content, commit_msg=commit_msg,
+          reviewers=request.reviewer_emails, cc_list=request.cc_emails,
+          auto_approved=request.auto_approved, update_metadata=new_metadata)
     except hwid_repo.HWIDRepoError:
       logging.exception(
           'Caught an unexpected exception while uploading a HWID CL.')
diff --git a/py/hwid/service/appengine/hwid_repo.py b/py/hwid/service/appengine/hwid_repo.py
index dd602d5..f24eade 100644
--- a/py/hwid/service/appengine/hwid_repo.py
+++ b/py/hwid/service/appengine/hwid_repo.py
@@ -5,7 +5,7 @@
 
 import collections
 import logging
-from typing import NamedTuple, Tuple
+from typing import NamedTuple, Optional, Sequence, Tuple
 
 from cros.factory.hwid.service.appengine import git_util
 from cros.factory.hwid.v3 import filesystem_adapter
@@ -56,6 +56,8 @@
 
 class HWIDRepo:
 
+  INTERNAL_DB_NAME_SUFFIX = '.internal'
+
   def __init__(self, repo, repo_url, repo_branch):
     """Constructor.
 
@@ -112,8 +114,11 @@
       raise HWIDRepoError(
           f'failed to load the HWID DB (name={name}): {ex}') from None
 
-  def CommitHWIDDB(self, name, hwid_db_contents, commit_msg, reviewers, cc_list,
-                   auto_approved, update_metadata=None):
+  def CommitHWIDDB(self, name: str, hwid_db_contents: str, commit_msg: str,
+                   reviewers: Sequence[str], cc_list: Sequence[str],
+                   auto_approved: bool,
+                   update_metadata: Optional[HWIDDBMetadata] = None,
+                   hwid_db_contents_internal: Optional[str] = None):
     """Commit an HWID DB to the repo.
 
     Args:
@@ -125,6 +130,7 @@
       cc_list: List of emails of CC's.
       auto_approved: A bool indicating if this CL should be auto-approved.
       update_metadata: A HWIDDBMetadata object to update for the project.
+      hwid_db_contents_internal: The contents of the HWID DB in internal format.
 
     Returns:
       A numeric ID of the created CL.
@@ -146,12 +152,21 @@
       raise ValueError(f'invalid HWID DB name: {name}') from None
     new_files.append(
         (path, git_util.NORMAL_FILE_MODE, hwid_db_contents.encode('utf-8')))
+
+    if not hwid_db_contents_internal:
+      hwid_db_contents_internal = hwid_db_contents
+
+    internal_path = self.InternalDBPath(path)
+    new_files.append((internal_path, git_util.NORMAL_FILE_MODE,
+                      hwid_db_contents_internal.encode('utf-8')))
+
     try:
       author_email, unused_token = git_util.GetGerritCredentials()
       author = f'chromeoshwid <{author_email}>'
       change_id, cl_number = git_util.CreateCL(
-          self._repo_url, git_util.GetGerritAuthCookie(), self._repo_branch,
-          new_files, author, author, commit_msg, reviewers=reviewers,
+          git_url=self._repo_url, auth_cookie=git_util.GetGerritAuthCookie(),
+          branch=self._repo_branch, new_files=new_files, author=author,
+          committer=author, commit_msg=commit_msg, reviewers=reviewers,
           cc=cc_list, auto_approved=auto_approved, repo=self._repo)
       if cl_number is None:
         logging.warning(
@@ -177,6 +192,10 @@
     except Exception as ex:
       raise HWIDRepoError(f'invalid {_PROJECTS_YAML_PATH}: {ex}') from None
 
+  @classmethod
+  def InternalDBPath(cls, path):
+    return f'{path}{cls.INTERNAL_DB_NAME_SUFFIX}'
+
 
 HWIDDBCLInfo = git_util.CLInfo
 HWIDDBCLStatus = git_util.CLStatus
diff --git a/py/hwid/service/appengine/hwid_repo_test.py b/py/hwid/service/appengine/hwid_repo_test.py
index 6a22b01..da9302b 100755
--- a/py/hwid/service/appengine/hwid_repo_test.py
+++ b/py/hwid/service/appengine/hwid_repo_test.py
@@ -137,8 +137,14 @@
                                            expected_cl_number)
 
     actual_cl_number = self._hwid_repo.CommitHWIDDB(
-        'SBOARD', 'unused_test_str', 'unused_test_str', [], [], False)
+        'SBOARD', 'hwid_db_contents', 'unused_test_str', [], [], False, None,
+        'hwid_db_contents_internal')
     self.assertEqual(actual_cl_number, expected_cl_number)
+    kwargs = self._mocked_create_cl.call_args[1]
+    self.assertEqual(
+        [('SBOARD', 0o100644, b'hwid_db_contents'),
+         ('SBOARD.internal', 0o100644, b'hwid_db_contents_internal')],
+        kwargs['new_files'])
 
   def testHWIDRepoHasCommitProperty(self):
     self.assertEqual(self._hwid_repo.hwid_db_commit_id,