2020-10-21 07:30:49 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# internal use only for CI
|
|
|
|
# get latest MR information by source branch
|
|
|
|
#
|
2024-05-07 05:02:39 -04:00
|
|
|
# SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
|
2022-06-15 10:46:55 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2020-10-21 07:30:49 -04:00
|
|
|
#
|
|
|
|
import argparse
|
2024-05-29 03:28:46 -04:00
|
|
|
import logging
|
2020-10-21 07:30:49 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2022-11-29 04:11:33 -05:00
|
|
|
import typing as t
|
|
|
|
from pathlib import Path
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
from gitlab_api import Gitlab
|
2024-05-07 05:02:39 -04:00
|
|
|
from idf_ci_utils import IDF_PATH
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
from gitlab.v4.objects import ProjectCommit, ProjectMergeRequest
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2021-02-01 03:18:16 -05:00
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
def _get_mr_obj(source_branch: str) -> t.Optional['ProjectMergeRequest']:
|
2020-10-21 07:30:49 -04:00
|
|
|
gl = Gitlab(os.getenv('CI_PROJECT_ID', 'espressif/esp-idf'))
|
|
|
|
if not gl.project:
|
|
|
|
return None
|
2022-11-29 04:11:33 -05:00
|
|
|
|
2020-10-21 07:30:49 -04:00
|
|
|
mrs = gl.project.mergerequests.list(state='opened', source_branch=source_branch)
|
|
|
|
if mrs:
|
|
|
|
return mrs[0] # one source branch can only have one opened MR at one moment
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
def get_mr_iid(source_branch: str) -> str:
|
2020-10-21 07:30:49 -04:00
|
|
|
mr = _get_mr_obj(source_branch)
|
|
|
|
if not mr:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return str(mr.iid)
|
|
|
|
|
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
def get_mr_changed_files(source_branch: str) -> t.List[str]:
|
2020-10-21 07:30:49 -04:00
|
|
|
mr = _get_mr_obj(source_branch)
|
|
|
|
if not mr:
|
2022-11-29 04:11:33 -05:00
|
|
|
return []
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
git_output = subprocess.check_output(
|
2023-10-04 06:42:51 -04:00
|
|
|
['git', 'diff', '--name-only', '--diff-filter=d', f'origin/{mr.target_branch}...origin/{source_branch}']
|
2022-11-29 04:11:33 -05:00
|
|
|
).decode('utf8')
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
return [line.strip() for line in git_output.splitlines() if line.strip()]
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
|
|
|
|
def get_mr_commits(source_branch: str) -> t.List['ProjectCommit']:
|
2020-10-21 07:30:49 -04:00
|
|
|
mr = _get_mr_obj(source_branch)
|
|
|
|
if not mr:
|
2022-11-29 04:11:33 -05:00
|
|
|
return []
|
|
|
|
|
|
|
|
return list(mr.commits())
|
|
|
|
|
|
|
|
|
2024-05-07 05:02:39 -04:00
|
|
|
_COMPONENT_NAME_DIR_RECORDS = {}
|
|
|
|
|
|
|
|
|
|
|
|
def get_modified_component(filepath: str) -> t.Optional[str]:
|
|
|
|
"""Return the component name if the file is in a component directory, otherwise None."""
|
|
|
|
try:
|
|
|
|
f_path = Path(filepath).resolve().relative_to(IDF_PATH)
|
|
|
|
except ValueError: # not in IDF_PATH
|
|
|
|
return None
|
|
|
|
|
|
|
|
# skip md files, etc.
|
|
|
|
if f_path.suffix in ['.md', '.yml']:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# skip test_apps files
|
|
|
|
if 'test_apps' in f_path.parts:
|
|
|
|
return None
|
|
|
|
|
|
|
|
component_parent_dirs = [f_path.parts[0]]
|
|
|
|
for part in f_path.parts[1:]:
|
|
|
|
if component_parent_dirs[-1] == 'components' or component_parent_dirs[-1].endswith('common_components'):
|
|
|
|
if part not in _COMPONENT_NAME_DIR_RECORDS:
|
2024-05-29 03:28:46 -04:00
|
|
|
logging.debug('Found component "%s" in path "%s"' % (part, component_parent_dirs))
|
2024-05-07 05:02:39 -04:00
|
|
|
_COMPONENT_NAME_DIR_RECORDS[part] = component_parent_dirs
|
|
|
|
elif _COMPONENT_NAME_DIR_RECORDS.get(part) != component_parent_dirs:
|
2024-05-29 03:28:46 -04:00
|
|
|
logging.debug(
|
2024-05-07 05:02:39 -04:00
|
|
|
'WARNING!!! Found component "%s" in path "%s" and "%s"'
|
|
|
|
% (part, component_parent_dirs, _COMPONENT_NAME_DIR_RECORDS.get(part))
|
|
|
|
)
|
|
|
|
|
|
|
|
return part
|
|
|
|
|
|
|
|
component_parent_dirs.append(part)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-11-01 07:43:19 -04:00
|
|
|
def get_mr_components(
|
|
|
|
source_branch: t.Optional[str] = None, modified_files: t.Optional[t.List[str]] = None
|
|
|
|
) -> t.List[str]:
|
2022-11-29 04:11:33 -05:00
|
|
|
components: t.Set[str] = set()
|
2023-11-01 07:43:19 -04:00
|
|
|
if modified_files is None:
|
|
|
|
if not source_branch:
|
|
|
|
raise RuntimeError('--src-branch is required if --modified-files is not provided')
|
|
|
|
|
|
|
|
modified_files = get_mr_changed_files(source_branch)
|
|
|
|
|
|
|
|
for f in modified_files:
|
2024-05-07 05:02:39 -04:00
|
|
|
modified_component = get_modified_component(f)
|
|
|
|
if modified_component:
|
|
|
|
components.add(modified_component)
|
2022-11-29 04:11:33 -05:00
|
|
|
|
|
|
|
return list(components)
|
|
|
|
|
|
|
|
|
2023-08-31 00:55:28 -04:00
|
|
|
def get_target_in_tags(tags: str) -> str:
|
|
|
|
from idf_pytest.constants import TARGET_MARKERS
|
|
|
|
|
|
|
|
for x in tags.split(','):
|
|
|
|
if x in TARGET_MARKERS:
|
|
|
|
return x
|
|
|
|
|
|
|
|
raise RuntimeError(f'No target marker found in {tags}')
|
|
|
|
|
|
|
|
|
2022-11-29 04:11:33 -05:00
|
|
|
def _print_list(_list: t.List[str], separator: str = '\n') -> None:
|
|
|
|
print(separator.join(_list))
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Get the latest merge request info by pipeline')
|
2022-11-29 04:11:33 -05:00
|
|
|
actions = parser.add_subparsers(dest='action', help='info type', required=True)
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
common_args = argparse.ArgumentParser(add_help=False)
|
2023-11-01 07:43:19 -04:00
|
|
|
common_args.add_argument('--src-branch', help='source branch')
|
|
|
|
common_args.add_argument(
|
|
|
|
'--modified-files',
|
|
|
|
nargs='+',
|
|
|
|
help='space-separated list specifies the modified files. will be detected by --src-branch if not provided',
|
|
|
|
)
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
actions.add_parser('id', parents=[common_args])
|
|
|
|
actions.add_parser('commits', parents=[common_args])
|
2022-11-29 04:11:33 -05:00
|
|
|
actions.add_parser('components', parents=[common_args])
|
2023-08-31 00:55:28 -04:00
|
|
|
target = actions.add_parser('target_in_tags')
|
|
|
|
target.add_argument('tags', help='comma separated tags, e.g., esp32,generic')
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.action == 'id':
|
2023-11-01 07:43:19 -04:00
|
|
|
if not args.src_branch:
|
|
|
|
raise RuntimeError('--src-branch is required')
|
2020-10-21 07:30:49 -04:00
|
|
|
print(get_mr_iid(args.src_branch))
|
|
|
|
elif args.action == 'commits':
|
2023-11-01 07:43:19 -04:00
|
|
|
if not args.src_branch:
|
|
|
|
raise RuntimeError('--src-branch is required')
|
2022-11-29 04:11:33 -05:00
|
|
|
_print_list([commit.id for commit in get_mr_commits(args.src_branch)])
|
|
|
|
elif args.action == 'components':
|
2023-11-01 07:43:19 -04:00
|
|
|
_print_list(get_mr_components(args.src_branch, args.modified_files))
|
2023-08-31 00:55:28 -04:00
|
|
|
elif args.action == 'target_in_tags':
|
|
|
|
print(get_target_in_tags(args.tags))
|
2020-10-21 07:30:49 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError('not possible to get here')
|