diff --git a/src/azure-cli/azure/cli/command_modules/appservice/_help.py b/src/azure-cli/azure/cli/command_modules/appservice/_help.py index 9db81ad7f0b..342355ad0ff 100644 --- a/src/azure-cli/azure/cli/command_modules/appservice/_help.py +++ b/src/azure-cli/azure/cli/command_modules/appservice/_help.py @@ -2307,7 +2307,10 @@ examples: - name: Create static app in a subscription. text: az staticwebapp create -n MyStaticAppName -g MyExistingRg - -s https://github.com/JohnDoe/my-first-static-web-app -l WestUs2 -b master + -s https://github.com/JohnDoe/my-first-static-web-app -l WestUs2 -b master -t MyAccessToken + - name: Create static app in a subscription, retrieving token interactively + text: az staticwebapp create -n MyStaticAppName -g MyExistingRg + -s https://github.com/JohnDoe/my-first-static-web-app -l WestUs2 -b master --login-with-github """ helps['staticwebapp update'] = """ @@ -2332,6 +2335,8 @@ examples: - name: Connect a repo and branch to static app. text: az staticwebapp reconnect -n MyStaticAppName --source MyGitHubRepo -b master --token MyAccessToken + - name: Connect a repo and branch to static app, retrieving token interactively + text: az staticwebapp reconnect -n MyStaticAppName --source MyGitHubRepo -b master --login-with-github """ helps['staticwebapp delete'] = """ diff --git a/src/azure-cli/azure/cli/command_modules/appservice/_params.py b/src/azure-cli/azure/cli/command_modules/appservice/_params.py index c2492eda865..cb26d49111b 100644 --- a/src/azure-cli/azure/cli/command_modules/appservice/_params.py +++ b/src/azure-cli/azure/cli/command_modules/appservice/_params.py @@ -984,8 +984,9 @@ def load_arguments(self, _): c.argument('token', options_list=['--token', '-t'], help="A user's github repository token. This is used to setup the Github Actions workflow file and " "API secrets. If you need to create a Github Personal Access Token, " - "please follow the steps found at the following link:\n" + "please run with the '--login-with-github' flag or follow the steps found at the following link:\n" "https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line") + c.argument('login_with_github', help="Interactively log in with Github to retrieve the Personal Access Token") c.argument('branch', options_list=['--branch', '-b'], help="The target branch in the repository.") with self.argument_context('staticwebapp environment') as c: c.argument('environment_name', diff --git a/src/azure-cli/azure/cli/command_modules/appservice/static_sites.py b/src/azure-cli/azure/cli/command_modules/appservice/static_sites.py index cf6d81527d9..1cb2775abc1 100644 --- a/src/azure-cli/azure/cli/command_modules/appservice/static_sites.py +++ b/src/azure-cli/azure/cli/command_modules/appservice/static_sites.py @@ -39,7 +39,8 @@ def disconnect_staticsite(cmd, name, resource_group_name=None, no_wait=False): resource_group_name=resource_group_name, name=name) -def reconnect_staticsite(cmd, name, source, branch, token=None, resource_group_name=None, no_wait=False): +def reconnect_staticsite(cmd, name, source, branch, token=None, resource_group_name=None, login_with_github=False, + no_wait=False): client = _get_staticsites_client_factory(cmd.cli_ctx) if not resource_group_name: resource_group_name = _get_resource_group_name_of_staticsite(client, name) @@ -47,7 +48,7 @@ def reconnect_staticsite(cmd, name, source, branch, token=None, resource_group_n location = _get_staticsite_location(client, name, resource_group_name) return create_staticsites(cmd, resource_group_name, name, location, - source, branch, token, no_wait=no_wait) + source, branch, token, login_with_github=login_with_github, no_wait=no_wait) def list_staticsite_environments(cmd, name, resource_group_name=None): @@ -192,9 +193,15 @@ def update_staticsite_users(cmd, name, roles, authentication_provider=None, user def create_staticsites(cmd, resource_group_name, name, location, source, branch, token=None, app_location='.', api_location='.', output_location='.github/workflows', - tags=None, no_wait=False, sku='Free'): - if not token: + tags=None, no_wait=False, sku='Free', login_with_github=False): + if not token and not login_with_github: _raise_missing_token_suggestion() + elif not token: + from ._github_oauth import get_github_access_token + scopes = ["admin:repo_hook", "repo", "workflow"] + token = get_github_access_token(cmd, scopes) + elif token and login_with_github: + logger.warning("Both token and --login-with-github flag are provided. Will use provided token") StaticSiteARMResource, StaticSiteBuildProperties, SkuDescription = cmd.get_models( 'StaticSiteARMResource', 'StaticSiteBuildProperties', 'SkuDescription') @@ -273,7 +280,8 @@ def _raise_missing_token_suggestion(): pat_documentation = "https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line" raise CLIError("GitHub access token is required to authenticate to your repositories. " "If you need to create a Github Personal Access Token, " - "please follow the steps found at the following link:\n{0}".format(pat_documentation)) + "please run with the '--login-with-github' flag or follow " + "the steps found at the following link:\n{0}".format(pat_documentation)) def _get_staticsite_location(client, static_site_name, resource_group_name): diff --git a/src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_staticapp_commands_thru_mock.py b/src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_staticapp_commands_thru_mock.py index 4c54ed3dbbf..3e1e234b430 100644 --- a/src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_staticapp_commands_thru_mock.py +++ b/src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_staticapp_commands_thru_mock.py @@ -200,7 +200,7 @@ def test_reconnect_staticapp_with_resourcegroup(self, create_staticsites_mock): resource_group_name=self.rg1) create_staticsites_mock.assert_called_once_with(self.mock_cmd, self.rg1, self.name1, self.location1, - self.source1, self.branch1, self.token1, no_wait=False) + self.source1, self.branch1, self.token1, login_with_github=False, no_wait=False) @mock.patch('azure.cli.command_modules.appservice.static_sites.create_staticsites', autospec=True) def test_reconnect_staticapp_without_resourcegroup(self, create_staticsites_mock): @@ -209,7 +209,7 @@ def test_reconnect_staticapp_without_resourcegroup(self, create_staticsites_mock reconnect_staticsite(self.mock_cmd, self.name1, self.source1, self.branch1, self.token1) create_staticsites_mock.assert_called_once_with(self.mock_cmd, self.rg1, self.name1, self.location1, - self.source1, self.branch1, self.token1, no_wait=False) + self.source1, self.branch1, self.token1, login_with_github=False, no_wait=False) def test_list_staticsite_environments_with_resourcegroup(self): list_staticsite_environments(self.mock_cmd, self.name1, self.rg1)