Skip to content

Commit da623ed

Browse files
Make input manifest ci checks accept commit id as refs (opensearch-project#2856)
* Make input manifest ci checks accept commit id as refs Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * Resolve isort issues Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * Use try catch to resolve issues with component without refs Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * Resolve isort issues Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * Change commit to sha1 var name in initial verification Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> Signed-off-by: Peter Zhu <zhujiaxi@amazon.com>
1 parent b4e98c7 commit da623ed

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/ci_workflow/ci_check_list.py

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# this file be licensed under the Apache-2.0 license or a
66
# compatible open source license.
77

8+
import logging
89
from abc import ABC, abstractmethod
910
from typing import Any
1011

@@ -14,6 +15,11 @@
1415
class CiCheckList(ABC):
1516
def __init__(self, component: Any, target: CiTarget) -> None:
1617
self.component = component
18+
try:
19+
self.component_ref_is_sha1 = True if len(self.component.ref) == 40 and int(self.component.ref, 16) else False
20+
logging.debug("sha1 exists in ref")
21+
except AttributeError:
22+
logging.debug("sha1 not exist in ref")
1723
self.target = target
1824

1925
@abstractmethod

src/ci_workflow/ci_check_list_source_ref.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
# compatible open source license.
77

88
import logging
9+
import os
910
import subprocess
1011

1112
from ci_workflow.ci_check_list import CiCheckList
13+
from git.git_repository import GitRepository
1214

1315

1416
class CiCheckListSourceRef(CiCheckList):
@@ -17,12 +19,31 @@ def __init__(self, url: str, ref: str) -> None:
1719
super().__init__(f"Missing {url}@{ref}.")
1820

1921
def checkout(self, work_dir: str) -> None:
22+
23+
# If ref is commit id, checkout the repository instead, as ls-remote does not allow non-branch/non-tag to be checked
24+
if self.component_ref_is_sha1:
25+
self.git_repo = GitRepository(
26+
self.component.repository, self.component.ref, os.path.join(work_dir, self.component.name), self.component.working_directory
27+
)
28+
2029
return super().checkout(work_dir)
2130

2231
def check(self) -> None:
2332
logging.info(f"Checking {self.component.repository} at {self.component.ref}.")
24-
results = subprocess.check_output(f"git ls-remote {self.component.repository} {self.component.ref}", shell=True).decode().strip().split("\t")
25-
if len(results) != 2:
33+
34+
if self.component_ref_is_sha1:
35+
logging.info(f"Detect {self.component.name} with ref {self.component.ref} in sha1 format, treat as commit.")
36+
results = subprocess.check_output(f"git cat-file -t {self.component.ref}", shell=True, cwd=self.git_repo.working_directory).decode().strip().split("\t")
37+
check_failure = False if len(results) == 1 and results[0] == 'commit' else True
38+
else:
39+
logging.info(f"Treat {self.component.name} with ref {self.component.ref} as branch/tag since it is not sha1 commit")
40+
results = subprocess.check_output(f"git ls-remote {self.component.repository} {self.component.ref}", shell=True).decode().strip().split("\t")
41+
check_failure = True if len(results) != 2 else False
42+
43+
if check_failure:
2644
raise CiCheckListSourceRef.MissingRefError(self.component.repository, self.component.ref)
2745
else:
28-
logging.info(f"Found {self.component.repository} at {self.component.ref} ({results[0]}).")
46+
if self.component_ref_is_sha1:
47+
logging.info(f"Found {self.component.repository} at {self.component.ref}.")
48+
else:
49+
logging.info(f"Found {self.component.repository} at {self.component.ref} ({results[0]}).")

tests/tests_ci_workflow/test_ci_check_lists_source_ref.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class TestCiCheckListsSourceRef(unittest.TestCase):
1616
@patch("subprocess.check_output", return_value="invalid".encode())
17-
def test_ref_does_not_exist(self, mock_check_output: MagicMock) -> None:
17+
def test_ref_branch_tag_does_not_exist(self, mock_check_output: MagicMock) -> None:
1818
component = InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "ref"})
1919
with self.assertRaises(CiCheckListSourceRef.MissingRefError) as ctx:
2020
list = CiCheckListSourceRef(component, MagicMock())
@@ -23,8 +23,35 @@ def test_ref_does_not_exist(self, mock_check_output: MagicMock) -> None:
2323
mock_check_output.assert_called_with("git ls-remote url ref", shell=True)
2424

2525
@patch("subprocess.check_output", return_value="valid\tref".encode())
26-
def test_ref_exists(self, mock_check_output: MagicMock) -> None:
26+
def test_ref_branch_tag_exists(self, mock_check_output: MagicMock) -> None:
2727
component = InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "ref"})
2828
list = CiCheckListSourceRef(component, MagicMock())
2929
list.check()
3030
mock_check_output.assert_called_with("git ls-remote url ref", shell=True)
31+
32+
@patch("ci_workflow.ci_check_list_source_ref.GitRepository")
33+
def test_ref_commit_checkout(self, mock_git_repo: MagicMock) -> None:
34+
component = InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
35+
list = CiCheckListSourceRef(component, MagicMock())
36+
list.checkout("path")
37+
mock_git_repo.assert_called()
38+
39+
@patch("ci_workflow.ci_check_list_source_ref.GitRepository")
40+
@patch("subprocess.check_output", return_value="fatal".encode())
41+
def test_ref_commit_does_not_exist(self, mock_check_output: MagicMock, mock_git_repo: MagicMock) -> None:
42+
component = InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
43+
with self.assertRaises(CiCheckListSourceRef.MissingRefError) as ctx:
44+
list = CiCheckListSourceRef(component, MagicMock())
45+
list.checkout("path")
46+
list.check()
47+
self.assertEqual("Missing url@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.", str(ctx.exception))
48+
mock_check_output.assert_called_with("git cat-file -t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", shell=True, cwd=mock_git_repo().working_directory)
49+
50+
@patch("ci_workflow.ci_check_list_source_ref.GitRepository")
51+
@patch("subprocess.check_output", return_value="commit".encode())
52+
def test_ref_commit_exists(self, mock_check_output: MagicMock, mock_git_repo: MagicMock) -> None:
53+
component = InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
54+
list = CiCheckListSourceRef(component, MagicMock())
55+
list.checkout("path")
56+
list.check()
57+
mock_check_output.assert_called_with("git cat-file -t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", shell=True, cwd=mock_git_repo().working_directory)

0 commit comments

Comments
 (0)