forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci_check_list_source_ref.py
49 lines (39 loc) · 2.26 KB
/
ci_check_list_source_ref.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright OpenSearch Contributors
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
import logging
import os
import subprocess
from ci_workflow.ci_check_list import CiCheckList
from git.git_repository import GitRepository
class CiCheckListSourceRef(CiCheckList):
class MissingRefError(Exception):
def __init__(self, url: str, ref: str) -> None:
super().__init__(f"Missing {url}@{ref}.")
def checkout(self, work_dir: str) -> None:
# If ref is commit id, checkout the repository instead, as ls-remote does not allow non-branch/non-tag to be checked
if self.component_ref_is_sha1:
self.git_repo = GitRepository(
self.component.repository, self.component.ref, os.path.join(work_dir, self.component.name), self.component.working_directory
)
return super().checkout(work_dir)
def check(self) -> None:
logging.info(f"Checking {self.component.repository} at {self.component.ref}.")
if self.component_ref_is_sha1:
logging.info(f"Detect {self.component.name} with ref {self.component.ref} in sha1 format, treat as commit.")
results = subprocess.check_output(f"git cat-file -t {self.component.ref}", shell=True, cwd=self.git_repo.working_directory).decode().strip().split("\t")
check_failure = False if len(results) == 1 and results[0] == 'commit' else True
else:
logging.info(f"Treat {self.component.name} with ref {self.component.ref} as branch/tag since it is not sha1 commit")
results = subprocess.check_output(f"git ls-remote {self.component.repository} {self.component.ref}", shell=True).decode().strip().split("\t")
check_failure = True if len(results) != 2 else False
if check_failure:
raise CiCheckListSourceRef.MissingRefError(self.component.repository, self.component.ref)
else:
if self.component_ref_is_sha1:
logging.info(f"Found {self.component.repository} at {self.component.ref}.")
else:
logging.info(f"Found {self.component.repository} at {self.component.ref} ({results[0]}).")