diff --git a/azure-devops/azext_devops/dev/common/const.py b/azure-devops/azext_devops/dev/common/const.py index 73517019..c1ec6674 100644 --- a/azure-devops/azext_devops/dev/common/const.py +++ b/azure-devops/azext_devops/dev/common/const.py @@ -18,6 +18,8 @@ AZ_DEVOPS_CONFIG_DIR_ENVKEY = CLI_ENV_VARIABLE_PREFIX + 'CONFIG_DIR' AZ_DEVOPS_GITHUB_PAT_ENVKEY = CLI_ENV_VARIABLE_PREFIX + 'GITHUB_PAT' AZ_DEVOPS_PIPELINES_VARIABLES_KEY_PREFIX = CLI_ENV_VARIABLE_PREFIX + 'PIPELINE_VAR_' +AZ_DEVOPS_DEFAULT_ORGANIZATION = CLI_ENV_VARIABLE_PREFIX + 'DEFAULT_ORGANIZATION' +AZ_DEVOPS_DEFAULT_PROJECT = CLI_ENV_VARIABLE_PREFIX + 'DEFAULT_PROJECT' # Import request Environment Variable GIT_SOURCE_PASSWORD_OR_PAT = CLI_ENV_VARIABLE_PREFIX + 'GIT_SOURCE_PASSWORD_OR_PAT' @@ -47,3 +49,5 @@ ORG_IGNORED_FROM_CONFIG = 'OrgIgnoredFromConfig' PROJECT_PICKED_FROM_CONFIG = 'ProjectPickedFromConfig' PROJECT_IGNORED_FROM_CONFIG = 'ProjectIgnoredFromConfig' +ORG_PICKED_FROM_ENV = 'OrgPickedFromEnv' +PROJECT_PICKED_FROM_ENV = 'ProjectPickedFromEnv' diff --git a/azure-devops/azext_devops/dev/common/services.py b/azure-devops/azext_devops/dev/common/services.py index 630c8353..aec0ddb9 100644 --- a/azure-devops/azext_devops/dev/common/services.py +++ b/azure-devops/azext_devops/dev/common/services.py @@ -24,9 +24,13 @@ PROJECT_PICKED_FROM_GIT, REPO_PICKED_FROM_GIT, ORG_PICKED_FROM_CONFIG, + ORG_PICKED_FROM_ENV, ORG_IGNORED_FROM_CONFIG, PROJECT_PICKED_FROM_CONFIG, - PROJECT_IGNORED_FROM_CONFIG) + PROJECT_PICKED_FROM_ENV, + PROJECT_IGNORED_FROM_CONFIG, + AZ_DEVOPS_DEFAULT_ORGANIZATION, + AZ_DEVOPS_DEFAULT_PROJECT) from ._credentials import get_credential from .git import get_remote_url from .vsts_git_url_info import VstsGitUrlInfo @@ -346,17 +350,23 @@ def resolve_instance_project_and_repo( repo = git_info.repo vsts_tracking_data.properties[REPO_PICKED_FROM_GIT] = repo is not None if organization is None: - organization = _resolve_instance_from_config(organization) - vsts_tracking_data.properties[ORG_PICKED_FROM_CONFIG] = organization is not None - else: - orgFromConfig = _resolve_instance_from_config(organization) - vsts_tracking_data.properties[ORG_IGNORED_FROM_CONFIG] = orgFromConfig is not None + organization = _resolve_instance_from_env(organization) + vsts_tracking_data.properties[ORG_PICKED_FROM_ENV] = organization is not None + if organization is None: + organization = _resolve_instance_from_config(organization) + vsts_tracking_data.properties[ORG_PICKED_FROM_CONFIG] = organization is not None + else: + orgFromConfig = _resolve_instance_from_config(organization) + vsts_tracking_data.properties[ORG_IGNORED_FROM_CONFIG] = orgFromConfig is not None if project is None: - project = _resolve_project_from_config(project, project_required) - vsts_tracking_data.properties[PROJECT_PICKED_FROM_CONFIG] = organization is not None - else: - projectFromConfig = _resolve_project_from_config(project, False) - vsts_tracking_data.properties[PROJECT_IGNORED_FROM_CONFIG] = projectFromConfig is not None + project = _resolve_project_from_env(project, project_required) + vsts_tracking_data.properties[PROJECT_PICKED_FROM_ENV] = project is not None + if project is None: + project = _resolve_project_from_config(project, project_required) + vsts_tracking_data.properties[PROJECT_PICKED_FROM_CONFIG] = project is not None + else: + projectFromConfig = _resolve_project_from_config(project, False) + vsts_tracking_data.properties[PROJECT_IGNORED_FROM_CONFIG] = projectFromConfig is not None if not is_valid_url(organization): raise _team_organization_arg_error() if project_required and project is None: @@ -402,6 +412,20 @@ def _resolve_project_from_config(project, project_required=True): return project +def _resolve_instance_from_env(organization): + if organization is None: + organization = os.getenv(AZ_DEVOPS_DEFAULT_ORGANIZATION, None) + return organization + + +def _resolve_project_from_env(project, project_required=True): + if project is None: + project = os.getenv(AZ_DEVOPS_DEFAULT_PROJECT, None) + if project_required and (project is None or project == ''): + _raise_team_project_arg_error() + return project + + def get_vsts_info_from_current_remote_url(): start = datetime.datetime.now() info = VstsGitUrlInfo(get_remote_url(VstsGitUrlInfo.is_vsts_url_candidate))