-
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 9 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 |
---|---|---|
|
@@ -5,9 +5,13 @@ | |
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 +42,78 @@ def _repo_path_argument_callback(path): | |
) | ||
|
||
|
||
def _assure_repository_does_not_exist(repository_name: str) -> None: | ||
try: | ||
ObjectManager().describe( | ||
object_type=ObjectType.GIT_REPOSITORY.value.cli_name, | ||
name=repository_name, | ||
) | ||
raise ClickException(f"Repository '{repository_name}' already exists") | ||
except ProgrammingError: | ||
pass | ||
|
||
|
||
@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. | ||
""" | ||
_assure_repository_does_not_exist(repository_name) | ||
manager = GitManager() | ||
|
||
url = typer.prompt("Origin url") | ||
|
||
secret = None | ||
secret_needed = typer.confirm("Use secret for authentication?") | ||
if secret_needed: | ||
use_existing_secret = typer.confirm("Use existing secret?") | ||
if use_existing_secret: | ||
secret = typer.prompt("Secret identifier") | ||
else: | ||
cli_console.step("Creating new secret") | ||
secret = f"{repository_name}_secret" | ||
username = typer.prompt("username") | ||
password = typer.prompt("password/token", hide_input=True) | ||
manager.create_password_secret( | ||
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. I would move object creation to the end, in this way there's a clear separation of "prompts" and "actions" WDYT? 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 |
||
username=username, password=password, name=secret | ||
) | ||
cli_console.step(f"Secret '{secret}' successfully created") | ||
|
||
use_existing_api = typer.confirm("Use existing api integration?") | ||
if use_existing_api: | ||
api_integration = typer.prompt("API integration identifier") | ||
else: | ||
api_integration = f"{repository_name}_api_integration" | ||
manager.create_api_integration( | ||
name=api_integration, | ||
api_provider="git_https_api", | ||
allowed_prefix=url, | ||
secret=secret, | ||
) | ||
cli_console.step(f"API integration '{api_integration}' successfully created.") | ||
|
||
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 |
---|---|---|
|
@@ -18,3 +18,15 @@ 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) |
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.
Should we validate if not empty?
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.
In that case typer asks again:
I was considering validating whether it starts with
https://
, wdyt?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.
+1 for at least https validation
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