-
Notifications
You must be signed in to change notification settings - Fork 58
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
Snow 1181759 git setup #880
Changes from 5 commits
88ae9f2
7f38b53
e93fc7d
4576859
adeac9d
6d2beb5
fa6fb1c
161800a
56d092a
9bd803a
8277a0f
74c733f
7e58841
75052e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import logging | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import typer | ||
from click import ClickException | ||
from snowflake.cli.api.commands.flags import identifier_argument | ||
from snowflake.cli.api.commands.snow_typer import SnowTyper | ||
from snowflake.cli.api.console.console import cli_console | ||
from snowflake.cli.api.constants import ObjectType | ||
from snowflake.cli.api.output.types import CommandResult, QueryResult | ||
from snowflake.cli.api.utils.path_utils import is_stage_path | ||
from snowflake.cli.plugins.git.manager import GitManager | ||
from snowflake.cli.plugins.object.manager import ObjectManager | ||
from snowflake.connector import ProgrammingError | ||
|
||
app = SnowTyper( | ||
name="git", | ||
|
@@ -38,6 +43,80 @@ def _repo_path_argument_callback(path): | |
) | ||
|
||
|
||
@app.command("setup", requires_connection=True) | ||
def setup( | ||
repository_name: str = RepoNameArgument, | ||
**options, | ||
) -> CommandResult: | ||
""" | ||
Sets up a git repository object. | ||
|
||
You will be prompted for: | ||
|
||
* url - address of repository to be used for git clone operation | ||
|
||
* secret - Snowflake secret containing authentication credentials. Not needed if origin repository does not require | ||
authentication for RO operations (clone, fetch) | ||
|
||
* API integration - object allowing Snowflake to interact with git repository. | ||
""" | ||
|
||
manager = GitManager() | ||
|
||
def _assure_repository_does_not_exist() -> None: | ||
om = ObjectManager() | ||
try: | ||
om.describe( | ||
object_type=ObjectType.GIT_REPOSITORY.value.cli_name, | ||
name=repository_name, | ||
) | ||
raise ClickException(f"Repository '{repository_name}' already exists") | ||
except ProgrammingError: | ||
pass | ||
|
||
def _get_secret() -> Optional[str]: | ||
secret_needed = typer.confirm("Use secret for authentication?") | ||
if not secret_needed: | ||
return None | ||
|
||
use_existing_secret = typer.confirm("Use existing secret?") | ||
if use_existing_secret: | ||
existing_secret = typer.prompt("Secret identifier") | ||
return existing_secret | ||
|
||
cli_console.step("Creating new secret") | ||
secret_name = f"{repository_name}_secret" | ||
username = typer.prompt("username") | ||
password = typer.prompt("password/token", hide_input=True) | ||
manager.create_secret(username=username, password=password, name=secret_name) | ||
cli_console.step(f"Secret '{secret_name}' successfully created") | ||
return secret_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline the prompts and confirm into command. It will be easier to follow the command flow. Currently I find it pretty hard to grasp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def _get_api_integration(secret: Optional[str], url: str) -> str: | ||
use_existing_api = typer.confirm("Use existing api integration?") | ||
if use_existing_api: | ||
api_name = typer.prompt("API integration identifier") | ||
return api_name | ||
|
||
api_name = f"{repository_name}_api_integration" | ||
manager.create_api_integration(name=api_name, allowed_prefix=url, secret=secret) | ||
cli_console.step(f"API integration '{api_name}' successfully created.") | ||
return api_name | ||
|
||
_assure_repository_does_not_exist() | ||
url = typer.prompt("Origin url") | ||
secret = _get_secret() | ||
api_integration = _get_api_integration(secret=secret, url=url) | ||
return QueryResult( | ||
manager.create( | ||
repo_name=repository_name, | ||
url=url, | ||
api_integration=api_integration, | ||
secret=secret, | ||
) | ||
) | ||
|
||
|
||
@app.command( | ||
"list-branches", | ||
requires_connection=True, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Optional | ||
|
||
from snowflake.cli.plugins.object.stage.manager import StageManager | ||
from snowflake.connector.cursor import SnowflakeCursor | ||
|
||
|
@@ -18,3 +20,36 @@ def show_files(self, repo_path: str) -> SnowflakeCursor: | |
def fetch(self, repo_name: str) -> SnowflakeCursor: | ||
query = f"alter git repository {repo_name} fetch" | ||
return self._execute_query(query) | ||
|
||
def create( | ||
self, repo_name: str, api_integration: str, url: str, secret: str | ||
) -> SnowflakeCursor: | ||
query = ( | ||
f"create git repository {repo_name}" | ||
f" api_integration = {api_integration}" | ||
f" origin = '{url}'" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use multiline string, it makes things a bit more readable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if secret is not None: | ||
query += f" git_credentials = {secret}" | ||
return self._execute_query(query) | ||
|
||
def create_secret(self, name: str, username: str, password: str) -> SnowflakeCursor: | ||
query = ( | ||
f"create secret {name}" | ||
f" type = password" | ||
f" username = '{username}'" | ||
f" password = '{password}'" | ||
) | ||
return self._execute_query(query) | ||
|
||
def create_api_integration( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to SQLMixin as those are not specific to git repo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I renamed |
||
self, name: str, allowed_prefix: str, secret: Optional[str] | ||
) -> SnowflakeCursor: | ||
query = ( | ||
f"create api integration {name}" | ||
f" api_provider = git_https_api" | ||
f" api_allowed_prefixes = ('{allowed_prefix}')" | ||
f" allowed_authentication_secrets = ({secret if secret else ''})" | ||
f" enabled = true" | ||
) | ||
return self._execute_query(query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to move those functions outside the command, it will increase readability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done