diff --git a/azure-sdk-tools/packaging_tools/__init__.py b/azure-sdk-tools/packaging_tools/__init__.py index 44026e4c6374..0b9d14c54f84 100644 --- a/azure-sdk-tools/packaging_tools/__init__.py +++ b/azure-sdk-tools/packaging_tools/__init__.py @@ -1,6 +1,7 @@ import logging +import os from pathlib import Path -from typing import Dict, Any +from typing import Dict, Any, Optional, List from jinja2 import Template, PackageLoader, Environment from .conf import read_conf, build_default_conf, CONF_NAME @@ -29,7 +30,28 @@ def build_config(config : Dict[str, Any]) -> Dict[str, str]: # Return result return result -def build_packaging(package_name: str, output_folder: str, build_conf: bool = False) -> None: + +def build_packaging(output_folder: str, gh_token: Optional[str]=None, jenkins: bool = False, packages: List[str]=None, build_conf: bool = False) -> None: + package_names = set(packages) or set() + if jenkins: + sdk_id = os.environ["ghprbGhRepository"] + pr_number = int(os.environ["ghprbPullId"]) + + from github import Github + con = Github(gh_token) + repo = con.get_repo(sdk_id) + sdk_pr = repo.get_pull(pr_number) + # "get_files" of Github only download the first 300 files. Might not be enough. + package_names |= {f.filename.split('/')[0] for f in sdk_pr.get_files() if f.filename.startswith("azure")} + + if not package_names: + raise ValueError("Was unable to find out the package names.") + + for package_name in package_names: + build_packaging_by_package_name(package_name, output_folder, build_conf) + + +def build_packaging_by_package_name(package_name: str, output_folder: str, build_conf: bool = False) -> None: _LOGGER.info("Building template %s", package_name) package_folder = Path(output_folder) / Path(package_name) diff --git a/azure-sdk-tools/packaging_tools/__main__.py b/azure-sdk-tools/packaging_tools/__main__.py index 2faec6d52012..da4c398b19d3 100644 --- a/azure-sdk-tools/packaging_tools/__main__.py +++ b/azure-sdk-tools/packaging_tools/__main__.py @@ -1,5 +1,6 @@ import argparse import logging +import os import sys from . import build_packaging @@ -23,7 +24,10 @@ parser.add_argument("--build-conf", dest="build_conf", action="store_true", help="Build a default TOML file, with package name, fake pretty name, as beta package and no doc page. Do nothing if the file exists, remove manually the file if needed.") -parser.add_argument('package_name', help='The package name.') +parser.add_argument("--jenkins", + dest="jenkins", action="store_true", + help="In Jenkins mode, try to find what to generate from Jenkins env variables. Package names are then optional.") +parser.add_argument('package_names', nargs='*', help='The package name.') args = parser.parse_args() @@ -31,8 +35,17 @@ logging.basicConfig() main_logger.setLevel(logging.DEBUG if args.debug else logging.INFO) +if not args.package_names and not args.jenkins: + raise ValueError("At least one package name or Jenkins mode is required") + try: - build_packaging(args.package_name, args.output, build_conf=args.build_conf) + build_packaging( + args.output, + os.environ.get("GH_TOKEN", None), + args.jenkins, + args.package_names, + build_conf=args.build_conf + ) except Exception as err: if args.debug: _LOGGER.exception(err)