Skip to content
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

Feature/edit uri #19

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 42 additions & 3 deletions phabfive/cli.py
Expand Up @@ -55,17 +55,26 @@
phabfive diffusion repo create <name> [options]
phabfive diffusion uri create (--observe || --mirror) (<credential>) <repo> <uri> [options]
phabfive diffusion uri list <repo> [options]
phabfive diffusion uri edit <repo> <uri> [Uri Options] [options]
phabfive diffusion branch list <repo> [options]

Arguments:
<repo> Repository monogram (R123) or shortname, but currently
not the callsign
<uri> ex. git@bitbucket.org:dynamist/webpage.git
(<credential>) SSH Private Key for read-only observing, stored in Passphrase ex. K123
<credential> SSH Private Key for read-only observing, stored in Passphrase ex. K123

Options:
-h, --help Show this help message and exit
-u, --url Show url
-h, --help Show this help message and exit
-u, --url Show url

Uri Options:
-n <name>, --new_uri=<name> Change repository URI
-i <value>, --io=<value> Adjust I/O behavior. Value: default, read, write, never
-d <value>, --display=<value> Change display behavior. Value: default, always, hidden
-c <input>, --cred=<input> Change credential for this URI. Ex. K2
--enable Enable URI
--disable Disable URI
"""

sub_paste_args = """
Expand Down Expand Up @@ -180,6 +189,36 @@ def run(cli_args, sub_args):
print(created_uri)
elif sub_args["list"]:
d.print_uri(repo=sub_args["<repo>"])
elif sub_args["edit"]:
object_id = d.get_object_identifier(
repo_name=sub_args["<repo>"], uri_name=sub_args["<uri>"]
)
if sub_args["--enable"]:
disable = False
elif sub_args["--disable"]:
disable = True
else:
disable = None

if (
sub_args["--new_uri"] is None # noqa: W503
and sub_args["--io"] is None # noqa: W503
and sub_args["--display"] is None # noqa: W503
and sub_args["--cred"] is None # noqa: W503
and disable is None # noqa: W503
):
print("Please input minimum one option")

result = d.edit_uri(
uri=sub_args["--new_uri"],
io=sub_args["--io"],
display=sub_args["--display"],
credential=sub_args["--cred"],
disable=disable,
object_identifier=object_id,
)
if result:
print("OK")
elif sub_args["branch"] and sub_args["list"]:
d.print_branches(repo=sub_args["<repo>"])
elif cli_args["<command>"] == "paste":
Expand Down
73 changes: 66 additions & 7 deletions phabfive/diffusion.py
Expand Up @@ -37,13 +37,50 @@ def _validate_credential_type(self, credential):
"token",
):
raise PhabfiveDataException(
"{0} is not type of ssh-generated-key, ssh-key-text or token.".format(
credential
"{0} is not type of 'ssh-generated-key', 'ssh-key-text' or 'token' but type '{1}'".format(
credential[credential_phid]["monogram"],
credential[credential_phid]["type"],
)
)

return credential_phid

def get_object_identifier(self, repo_name=None, uri_name=None):
"""Phabfive wrapper that connects to Phabricator and identify repository or uri object_identifier.

:type repo_name: str
:type uri_name: str

:rtype object_identifier: str
"""
object_identifier = ""
repos = self.get_repositories(attachments={"uris": True})

for repo in repos:
name = repo["fields"]["shortName"]
if repo_name == name:
# object identifier for uri
if repo_name and uri_name:
if uri_name:
uris = repo["attachments"]["uris"]["uris"]
for i in range(len(uris)):
uri = uris[i]["fields"]["uri"]["display"]
if uri_name == uri:
if uris[i]["id"]:
object_identifier = uris[i]["id"]
if object_identifier == "":
raise (
PhabfiveDataException(
"Uri does not exist or other error"
)
)
break
# object identifier for repository
elif repo_name and not uri_name:
object_identifier = repo["id"]
break
return object_identifier

# TODO: create_repository() should call edit_repository(), they are using the same conduit
def create_repository(self, name=None, vcs=None, status=None):
"""Phabfive wrapper that connects to Phabricator and creates repositories.
Expand Down Expand Up @@ -144,7 +181,7 @@ def create_uri(

if not repos:
raise PhabfiveDataException("No data or other error.")

# Check if input of repository_name is an id
if self._validate_identifier(repository_name):
repository_id = repository_name.replace("R", "")

Expand All @@ -155,7 +192,6 @@ def create_uri(

# TODO: error handling, catch exception?
get_credential = self.passphrase.get_secret(ids=credential)

credential_phid = self._validate_credential_type(credential=get_credential)
# TODO: Validate repos existent - create its own function
for repo in repos:
Expand Down Expand Up @@ -204,28 +240,51 @@ def create_uri(

return new_uri

def edit_uri(self, uri=None, io=None, display=None, object_identifier=None):
def edit_uri(
self,
uri=None,
io=None,
display=None,
credential=None,
disable=None,
object_identifier=None,
):
"""Phabfive wrapper that connects to Phabricator and edit uri.

:type uri: str
:type io: str
:type display: str
:type credential: str
:type credential: bool
:type object_identifier: str

:rtype: bool
"""
transactions = [
if credential:
credential = self.passphrase.get_secret(credential)
# get PHID, phab.diffusion.uri.edit takes PHID in credential
credential = next(iter(credential))

transactions = []
transactions_values = [
{"type": "uri", "value": uri},
{"type": "io", "value": io},
{"type": "display", "value": display},
{"type": "disable", "value": disable},
{"type": "credential", "value": credential},
]
# Phabricator does not take None as a value, therefor only "type" that has valid value can be sent as an argument
for item in transactions_values:
if None not in item.values():
transactions.append(item)
# if not transactions:
try:
# object_identifier is neccessary when editing an exisiting URI but leave blank when creating new URI
self.phab.diffusion.uri.edit(
transactions=transactions, objectIdentifier=object_identifier
)
except APIError:
pass
raise PhabfiveDataException("No valid input or other error")
# TODO: The APIError raises an I/O error which is not "correct"
# due to different setting for uri depending on if it is default uri or observed/mirrored uri
return True
Expand Down
2 changes: 2 additions & 0 deletions tests/test_constants.py
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

# 3rd party imports


def test_status_choices():
from phabfive.constants import REPO_STATUS_CHOICES
Expand Down