Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for setting of project and organization values using env vars #1415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions azure-devops/azext_devops/dev/common/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
46 changes: 35 additions & 11 deletions azure-devops/azext_devops/dev/common/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down