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

[pipelines][create] Add partial support for github enterprise #1247

Open
wants to merge 3 commits 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: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Azure DevOps CLI Debug (Integrated Console)",
"type": "python",
"request": "launch",
"pythonPath": "${config:python.pythonPath}",
"python": "${command:python.interpreterPath}",
"program": "${workspaceRoot}/scripts/run_az.py",
"cwd": "${workspaceRoot}/azure-devops/",
"args": [
Expand All @@ -25,7 +25,7 @@
"WaitOnNormalExit",
"RedirectOutput"
],
"debugStdLib": true,
"justMyCode": false,
"preLaunchTask": "BuildWheel",
"env": {
"AZURE_EXTENSION_DIR": "${workspaceRoot}"
Expand Down
2 changes: 1 addition & 1 deletion azure-devops/azext_devops/dev/pipelines/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def load_build_arguments(self, _):
type=str.lower)

with self.argument_context('pipelines create') as context:
context.argument('repository_type', choices=['tfsgit', 'github'], type=str.lower)
context.argument('repository_type', choices=['tfsgit', 'github', 'githubenterprise'], type=str.lower)
context.argument('yml_path', options_list=('--yml-path', '--yaml-path'))
context.argument('skip_first_run', options_list=['--skip-first-run', '--skip-run'],
arg_type=get_three_state_flag())
Expand Down
32 changes: 26 additions & 6 deletions azure-devops/azext_devops/dev/pipelines/pipeline_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def __init__(self, name, id, content, description='Custom yaml', params=None, pa

_GITHUB_REPO_TYPE = 'github'
_AZURE_GIT_REPO_TYPE = 'TfsGit'
_GITHUBENTERPRISE_REPO_TYPE = 'githubenterprise'


# pylint: disable=too-many-statements
# pylint: disable=too-many-statements, disable=too-many-branches
def pipeline_create(name, description=None, repository=None, branch=None, yml_path=None, repository_type=None,
service_connection=None, organization=None, project=None, detect=None, queue_id=None,
skip_first_run=None, folder_path=None):
Expand Down Expand Up @@ -107,7 +108,10 @@ def pipeline_create(name, description=None, repository=None, branch=None, yml_pa
raise CLIError('The following arguments are required: --branch.')
# repository, repository-type, branch should be set by now
if not repository_name and is_valid_url(repository):
repository_name = _get_repo_name_from_repo_url(repository)
repository_name = _get_repo_name_from_repo_url(repository, repository_type)

if not repository_name and repository_type.lower() == _GITHUBENTERPRISE_REPO_TYPE:
repository_name = _get_repo_name_from_repo_url(repository, repository_type)
else:
repository_name = repository

Expand All @@ -123,6 +127,12 @@ def pipeline_create(name, description=None, repository=None, branch=None, yml_pa
repo_id = repository_name
repository_url = 'https://github.com/' + repository_name
api_url = get_github_repos_api_url(repository_name)
if repository_type.lower() == _GITHUBENTERPRISE_REPO_TYPE:
repo_id = repository_name
repository_url = repository
ghe_url = uri_parse(repository_url)
api_url = ghe_url.scheme + '://' + ghe_url.netloc + '/api/v3/repos/' + repository_name

if repository_type.lower() == _AZURE_GIT_REPO_TYPE.lower():
repo_id = _get_repository_id_from_name(organization, project, repository_name)

Expand All @@ -133,6 +143,8 @@ def pipeline_create(name, description=None, repository=None, branch=None, yml_pa
# No yml path => find or recommend yml scenario
queue_branch = branch
if not yml_path:
if repository_type.lower() == _GITHUBENTERPRISE_REPO_TYPE:
raise CLIError('The following arguments are required for GitHub Enterprise: --yaml-path.')
yml_path, queue_branch = _create_and_get_yml_path(new_cix_client, repository_type, repo_id,
repository_name, branch, service_connection, project,
organization)
Expand Down Expand Up @@ -226,13 +238,12 @@ def is_valid_url(url):
return False


def _get_repo_name_from_repo_url(repository_url):
def _get_repo_name_from_repo_url(repository_url, repo_type):
"""
Should be called with a valid github or azure repo url
returns owner/reponame for github repos, repo_name for azure repo type
"""
repo_type = try_get_repository_type(repository_url)
if repo_type == _GITHUB_REPO_TYPE:
if repo_type.lower() == _GITHUB_REPO_TYPE:
parsed_url = uri_parse(repository_url)
logger.debug('Parsing GitHub url: %s', parsed_url)
if parsed_url.scheme == 'https' and parsed_url.netloc == 'github.com':
Expand All @@ -248,6 +259,15 @@ def _get_repo_name_from_repo_url(repository_url):
if ('visualstudio.com' in item or 'dev.azure.com' in item) and len(parsed_list) > index + 4:
return parsed_list[index + 4]
index = index + 1
if repo_type.lower() == _GITHUBENTERPRISE_REPO_TYPE:
parsed_url = uri_parse(repository_url)
logger.debug('Parsing GitHubEnterprise url: %s', parsed_url)
stripped_path = parsed_url.path.strip('/')
if stripped_path.endswith('.git'):
stripped_path = stripped_path[:-4]
parts = stripped_path.split('/')
return parts[-2] + '/' + parts[-1]

raise CLIError('Could not parse repository url.')


Expand Down Expand Up @@ -377,7 +397,7 @@ def push_files_to_repository(organization, project, repo_name, branch, files, re


def _get_pipelines_trigger(repo_type):
if repo_type.lower() == _GITHUB_REPO_TYPE:
if repo_type.lower() == _GITHUB_REPO_TYPE or repo_type.lower() == _GITHUBENTERPRISE_REPO_TYPE:
return [{"settingsSourceType": 2, "triggerType": 2},
{"forks": {"enabled": "true", "allowSecrets": "false"},
"settingsSourceType": 2, "triggerType": "pullRequest"}]
Expand Down