From ff8dd51bc77ae51866bbe59f0d89f8735ad3c7dd Mon Sep 17 00:00:00 2001 From: Silas Strawn Date: Fri, 6 May 2022 12:47:29 -0700 Subject: [PATCH] use Github repo's default branch for up and github actions --- src/containerapp/azext_containerapp/_params.py | 4 ++-- src/containerapp/azext_containerapp/_up_utils.py | 2 ++ src/containerapp/azext_containerapp/custom.py | 15 +++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/containerapp/azext_containerapp/_params.py b/src/containerapp/azext_containerapp/_params.py index fa33873602f..fae3c3d31f9 100644 --- a/src/containerapp/azext_containerapp/_params.py +++ b/src/containerapp/azext_containerapp/_params.py @@ -157,7 +157,7 @@ def load_arguments(self, _): with self.argument_context('containerapp github-action add') as c: c.argument('repo_url', help='The GitHub repository to which the workflow file will be added. In the format: https://github.com//') c.argument('token', help='A Personal Access Token with write access to the specified repository. For more information: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line') - c.argument('branch', options_list=['--branch', '-b'], help='The branch of the GitHub repo. Defaults to "main" if not specified.') + c.argument('branch', options_list=['--branch', '-b'], help='The branch of the Github repo. Assumed to be the Github repo\'s default branch if not specified.') c.argument('login_with_github', help='Interactively log in with Github to retrieve the Personal Access Token') c.argument('registry_url', help='The container registry server, e.g. myregistry.azurecr.io') c.argument('registry_username', help='The username of the registry. If using Azure Container Registry, we will try to infer the credentials if not supplied') @@ -236,7 +236,7 @@ def load_arguments(self, _): with self.argument_context('containerapp up', arg_group='Github Repo') as c: c.argument('repo', help='Create an app via Github Actions. In the format: https://github.com// or /') c.argument('token', help='A Personal Access Token with write access to the specified repository. For more information: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line. If missing (and using --repo), a browser page will be opened to authenticate with Github.') - c.argument('branch', options_list=['--branch', '-b'], help='The branch of the GitHub repo. Defaults to "main"') + c.argument('branch', options_list=['--branch', '-b'], help='The branch of the Github repo. Assumed to be the Github repo\'s default branch if not specified.') c.argument('context_path', help='Path in the repo from which to run the docker build. Defaults to "./". Dockerfile is assumed to be named "Dockerfile" and in this directory.') c.argument('service_principal_client_id', help='The service principal client ID. Used by Github Actions to authenticate with Azure.', options_list=["--service-principal-client-id", "--sp-cid"]) c.argument('service_principal_client_secret', help='The service principal client secret. Used by Github Actions to authenticate with Azure.', options_list=["--service-principal-client-secret", "--sp-sec"]) diff --git a/src/containerapp/azext_containerapp/_up_utils.py b/src/containerapp/azext_containerapp/_up_utils.py index fae718932c0..a54fd75f90e 100644 --- a/src/containerapp/azext_containerapp/_up_utils.py +++ b/src/containerapp/azext_containerapp/_up_utils.py @@ -404,6 +404,8 @@ def _get_dockerfile_content_from_repo( repo = repo_url_to_name(repo_url) try: r = g.get_repo(repo) + if not branch: + branch = r.default_branch except Exception as e: raise ValidationError(f"Could not find repo {repo_url}") from e try: diff --git a/src/containerapp/azext_containerapp/custom.py b/src/containerapp/azext_containerapp/custom.py index 4010086bbe8..c6058e761ac 100644 --- a/src/containerapp/azext_containerapp/custom.py +++ b/src/containerapp/azext_containerapp/custom.py @@ -1043,6 +1043,8 @@ def _validate_github(repo, branch, token): github_repo = None try: github_repo = g.get_repo(repo) + if not branch: + branch = github_repo.default_branch if not github_repo.permissions.push or not github_repo.permissions.maintain: raise ValidationError("The token does not have appropriate access rights to repository {}.".format(repo)) try: @@ -1062,6 +1064,7 @@ def _validate_github(repo, branch, token): if e.data and e.data['message']: error_msg += " Error: {}".format(e.data['message']) raise CLIInternalError(error_msg) from e + return branch def create_or_update_github_action(cmd, @@ -1071,7 +1074,7 @@ def create_or_update_github_action(cmd, registry_url=None, registry_username=None, registry_password=None, - branch="main", + branch=None, token=None, login_with_github=False, image=None, @@ -1091,7 +1094,7 @@ def create_or_update_github_action(cmd, repo = repo_url_to_name(repo_url) repo_url = f"https://github.com/{repo}" # allow specifying repo as / without the full github url - _validate_github(repo, branch, token) + branch = _validate_github(repo, branch, token) source_control_info = None @@ -1104,11 +1107,7 @@ def create_or_update_github_action(cmd, source_control_info = SourceControlModel source_control_info["properties"]["repoUrl"] = repo_url - - if branch: - source_control_info["properties"]["branch"] = branch - if not source_control_info["properties"]["branch"]: - source_control_info["properties"]["branch"] = "main" + source_control_info["properties"]["branch"] = branch azure_credentials = None @@ -2007,7 +2006,7 @@ def containerapp_up(cmd, logs_key=None, repo=None, token=None, - branch="main", + branch=None, browse=False, context_path=None, service_principal_client_id=None,