-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
88ae9f2
initial version of setup command
sfc-gh-pczajka 7f38b53
Quickcheck whether repository already exists
sfc-gh-pczajka e93fc7d
unit tests
sfc-gh-pczajka 4576859
integration tests
sfc-gh-pczajka adeac9d
small fix; update help messages test
sfc-gh-pczajka 6d2beb5
check setup output in integration tests
sfc-gh-pczajka fa6fb1c
review: refactor git/commands::setup
sfc-gh-pczajka 161800a
review fixes
sfc-gh-pczajka 56d092a
Update src/snowflake/cli/plugins/git/commands.py
sfc-gh-pczajka 9bd803a
refactor setup command
sfc-gh-pczajka 8277a0f
update unit tests
sfc-gh-pczajka 74c733f
update integration tests
sfc-gh-pczajka 7e58841
Merge branch 'SNOW-1181759-git-setup' of github.com:snowflakedb/snowf…
sfc-gh-pczajka 75052e0
multiline string
sfc-gh-pczajka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,97 @@ def _repo_path_argument_callback(path): | |
) | ||
|
||
|
||
def _object_exists(object_type, identifier): | ||
try: | ||
ObjectManager().describe( | ||
object_type=object_type.value.cli_name, | ||
name=identifier, | ||
) | ||
return True | ||
except ProgrammingError: | ||
return False | ||
|
||
|
||
def _assure_repository_does_not_exist(repository_name: str) -> None: | ||
if _object_exists(ObjectType.GIT_REPOSITORY, repository_name): | ||
raise ClickException(f"Repository '{repository_name}' already exists") | ||
|
||
|
||
def _validate_origin_url(url: str) -> None: | ||
if not url.startswith("https://"): | ||
raise ClickException("Url address should start with 'https'") | ||
|
||
|
||
@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") | ||
_validate_origin_url(url) | ||
|
||
secret = {} | ||
secret_needed = typer.confirm("Use secret for authentication?") | ||
if secret_needed: | ||
secret_name = f"{repository_name}_secret" | ||
secret_name = typer.prompt( | ||
"Secret identifier (will be created if not exists)", default=secret_name | ||
) | ||
secret = {"name": secret_name} | ||
if _object_exists(ObjectType.SECRET, secret_name): | ||
cli_console.step(f"Using existing secret '{secret_name}'") | ||
else: | ||
cli_console.step(f"Secret '{secret_name}' will be created") | ||
Comment on lines
+98
to
+100
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. Personally I would hide this information. If secrets exists - just continue. If it does not exists - ask for required value. WDYT? |
||
secret["username"] = typer.prompt("username") | ||
secret["password"] = typer.prompt("password/token", hide_input=True) | ||
|
||
api_integration = f"{repository_name}_api_integration" | ||
api_integration = typer.prompt( | ||
"API integration identifier (will be created if not exists)", | ||
default=api_integration, | ||
) | ||
|
||
if "username" in secret: | ||
manager.create_password_secret(**secret) | ||
secret_name = secret["name"] | ||
cli_console.step(f"Secret '{secret_name}' successfully created.") | ||
|
||
if not _object_exists(ObjectType.INTEGRATION, api_integration): | ||
manager.create_api_integration( | ||
name=api_integration, | ||
api_provider="git_https_api", | ||
allowed_prefix=url, | ||
secret=secret.get("name"), | ||
) | ||
cli_console.step(f"API integration '{api_integration}' successfully created.") | ||
else: | ||
cli_console.step(f"Using existing API integration '{api_integration}'.") | ||
|
||
return QueryResult( | ||
manager.create( | ||
repo_name=repository_name, | ||
url=url, | ||
api_integration=api_integration, | ||
secret=secret.get("name"), | ||
) | ||
) | ||
|
||
|
||
@app.command( | ||
"list-branches", | ||
requires_connection=True, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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