def mutate_and_get_payload(cls, root, info, owner, labbook_name, username, permissions, client_mutation_id=None): #TODO(billvb/dmk) - Here "username" refers to the intended recipient username. # it should probably be renamed here and in the frontend to "collaboratorUsername" logged_in_username = get_logged_in_username() lb = InventoryManager().load_labbook(logged_in_username, owner, labbook_name, author=get_logged_in_author()) # TODO: Future work will look up remote in LabBook data, allowing user to select remote. default_remote = lb.client_config.config['git']['default_remote'] admin_service = None for remote in lb.client_config.config['git']['remotes']: if default_remote == remote: admin_service = lb.client_config.config['git']['remotes'][ remote]['admin_service'] break # Extract valid Bearer token if "HTTP_AUTHORIZATION" in info.context.headers.environ: token = parse_token( info.context.headers.environ["HTTP_AUTHORIZATION"]) else: raise ValueError( "Authorization header not provided. " "Must have a valid session to query for collaborators") if permissions == 'readonly': perm = ProjectPermissions.READ_ONLY elif permissions == 'readwrite': perm = ProjectPermissions.READ_WRITE elif permissions == 'owner': perm = ProjectPermissions.OWNER else: raise ValueError(f"Unknown permission set: {permissions}") mgr = GitLabManager(default_remote, admin_service, token) existing_collabs = mgr.get_collaborators(owner, labbook_name) if username not in [n[1] for n in existing_collabs]: logger.info(f"Adding user {username} to {owner}/{labbook_name}" f"with permission {perm}") mgr.add_collaborator(owner, labbook_name, username, perm) else: logger.warning(f"Changing permission of {username} on" f"{owner}/{labbook_name} to {perm}") mgr.delete_collaborator(owner, labbook_name, username) mgr.add_collaborator(owner, labbook_name, username, perm) create_data = {"owner": owner, "name": labbook_name} return AddLabbookCollaborator(updated_labbook=Labbook(**create_data))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) migrated = False with lb.lock(): t0 = time.time() workflow = LabbookWorkflow(lb) migrated = workflow.migrate() tf = time.time() if migrated: logger.info(f"Migrated {str(lb)} in {tf-t0:.2}sec") else: logger.info(f"No migration needed for {str(lb)}") return MigrateLabbookSchema( Labbook(id=f"{owner}&{labbook_name}", name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, name, description, repository, base_id, revision, is_untracked=False, client_mutation_id=None): username = get_logged_in_username() inv_manager = InventoryManager() if is_untracked: lb = inv_manager.create_labbook_disabled_lfs( username=username, owner=username, labbook_name=name, description=description, author=get_logged_in_author()) else: lb = inv_manager.create_labbook(username=username, owner=username, labbook_name=name, description=description, author=get_logged_in_author()) if is_untracked: FileOperations.set_untracked(lb, 'input') FileOperations.set_untracked(lb, 'output') input_set = FileOperations.is_set_untracked(lb, 'input') output_set = FileOperations.is_set_untracked(lb, 'output') if not (input_set and output_set): raise ValueError( f'{str(lb)} untracking for input/output in malformed state' ) if not lb.is_repo_clean: raise ValueError( f'{str(lb)} should have clean Git state after setting for untracked' ) adr = ActivityDetailRecord(ActivityDetailType.LABBOOK, show=False, importance=0) adr.add_value('text/plain', f"Created new LabBook: {username}/{name}") # Create activity record ar = ActivityRecord(ActivityType.LABBOOK, message=f"Created new LabBook: {username}/{name}", show=True, importance=255, linked_commit=lb.git.commit_hash) ar.add_detail_object(adr) store = ActivityStore(lb) store.create_activity_record(ar) cm = ComponentManager(lb) cm.add_base(repository, base_id, revision) return CreateLabbook(labbook=Labbook(owner=username, name=lb.name))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, other_branch_name, override_method="abort", client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): override = MergeOverride(override_method) bm = BranchManager(lb, username=username) if override == MergeOverride.ABORT: bm.merge_from(other_branch=other_branch_name) elif override == MergeOverride.OURS: bm.merge_use_ours(other_branch=other_branch_name) elif override == MergeOverride.THEIRS: bm.merge_use_theirs(other_branch=other_branch_name) else: raise ValueError(f"Unknown override method {override}") return MergeFromBranch( Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, branch_name, revision=None, description=None, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): bm = BranchManager(lb, username=username) full_branch_title = bm.create_branch(title=branch_name, revision=revision) logger.info( f"In {str(lb)} created new experimental feature branch: " f"{full_branch_title}") if description: cls._update_branch_description(lb, description) return CreateExperimentalBranch( Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, branch_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): wf = LabbookWorkflow(lb) wf.checkout(username, branch_name) return WorkonBranch(Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) cursor = base64.b64encode(f"{0}".encode('utf-8')) lbedge = LabbookConnection.Edge(node=Labbook(owner=owner, name=labbook_name), cursor=cursor) return FetchLabbookEdge(new_labbook_edge=lbedge)
def mutate_and_get_payload(cls, root, info, owner, labbook_name, content, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) # Write data with lb.lock(): lb.write_readme(content) return WriteLabbookReadme(updated_labbook=Labbook(owner=owner, name=labbook_name))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, branch_name, delete_local=False, delete_remote=False, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): bm = BranchManager(lb, username=username) if delete_local: bm.remove_branch(target_branch=branch_name) if delete_remote: bm.remove_remote_branch(target_branch=branch_name) return DeleteExperimentalBranch(Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def resolve_labbook(self, info, owner: str, name: str): """Method to return a graphene Labbook instance based on the name Uses the "currently logged in" user Args: owner(str): Username of the owner (aka namespace) name(str): Name of the LabBook Returns: Labbook """ # Load the labbook data via a dataloader return Labbook(id="{}&{}".format(owner, name), name=name, owner=owner)
def mutate_and_get_payload(cls, root, info, owner, labbook_name, content, client_mutation_id=None): logged_in_username = get_logged_in_username() lb = LabBook(author=get_logged_in_author()) lb.from_name(logged_in_username, owner, labbook_name) # Write data lb.write_readme(content) return WriteReadme( updated_labbook=Labbook(owner=owner, name=labbook_name))
def _update_branch_description(cls, lb: Labbook, description: str): # Update the description on branch creation lb.description = description lb.git.add(lb.config_path) commit = lb.git.commit('Updating description') adr = ActivityDetailRecord(ActivityDetailType.LABBOOK, show=False) adr.add_value('text/plain', description) ar = ActivityRecord(ActivityType.LABBOOK, message="Updated description of Project", linked_commit=commit.hexsha, tags=["labbook"], show=False) ar.add_detail_object(adr) ars = ActivityStore(lb) ars.create_activity_record(ar)
def mutate_and_get_payload(cls, root, info, owner, labbook_name, branch_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) # TODO - fail fast if already locked. with lb.lock(): bm = BranchManager(lb, username=username) bm.workon_branch(branch_name=branch_name) return WorkonBranch( Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, repository, base_id, revision, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) cm = ComponentManager(lb) cm.change_base(repository, base_id, revision) return ChangeLabbookBase(labbook=Labbook(owner=owner, name=lb.name))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, remote_url, client_mutation_id=None): username = get_logged_in_username() logger.info(f"Importing remote labbook from {remote_url}") lb = LabBook(author=get_logged_in_author()) default_remote = lb.client_config.config['git']['default_remote'] admin_service = None for remote in lb.client_config.config['git']['remotes']: if default_remote == remote: admin_service = lb.client_config.config['git']['remotes'][ remote]['admin_service'] break # Extract valid Bearer token if hasattr(info.context, 'headers' ) and "HTTP_AUTHORIZATION" in info.context.headers.environ: token = parse_token( info.context.headers.environ["HTTP_AUTHORIZATION"]) else: raise ValueError( "Authorization header not provided. Must have a valid session to query for collaborators" ) gl_mgr = GitLabManager(default_remote, admin_service=admin_service, access_token=token) gl_mgr.configure_git_credentials(default_remote, username) wf = LabbookWorkflow.import_from_remote(remote_url, username=username) import_owner = InventoryManager().query_owner(wf.labbook) # TODO: Fix cursor implementation, this currently doesn't make sense cursor = base64.b64encode(f"{0}".encode('utf-8')) lbedge = LabbookConnection.Edge(node=Labbook(owner=import_owner, name=wf.labbook.name), cursor=cursor) return ImportRemoteLabbook(new_labbook_edge=lbedge)
def mutate_and_get_payload(cls, root, info, owner, labbook_name, username, client_mutation_id=None): logged_in_username = get_logged_in_username() lb = LabBook(author=get_logged_in_author()) lb.from_name(logged_in_username, owner, labbook_name) # TODO: Future work will look up remote in LabBook data, allowing user to select remote. default_remote = lb.labmanager_config.config['git']['default_remote'] admin_service = None for remote in lb.labmanager_config.config['git']['remotes']: if default_remote == remote: admin_service = lb.labmanager_config.config['git']['remotes'][ remote]['admin_service'] break # Extract valid Bearer token if "HTTP_AUTHORIZATION" in info.context.headers.environ: token = parse_token( info.context.headers.environ["HTTP_AUTHORIZATION"]) else: raise ValueError( "Authorization header not provided. Must have a valid session to query for collaborators" ) # Add collaborator to remote service mgr = GitLabManager(default_remote, admin_service, token) mgr.add_collaborator(owner, labbook_name, username) # Prime dataloader with labbook you just created dataloader = LabBookLoader() dataloader.prime(f"{username}&{username}&{lb.name}", lb) create_data = {"owner": owner, "name": labbook_name} return AddLabbookCollaborator(updated_labbook=Labbook(**create_data))
def mutate_and_get_payload(cls, root, info, name, description, repository, base_id, revision, is_untracked=False, client_mutation_id=None): username = get_logged_in_username() inv_manager = InventoryManager() lb = inv_manager.create_labbook(username=username, owner=username, labbook_name=name, description=description, author=get_logged_in_author()) adr = ActivityDetailRecord(ActivityDetailType.LABBOOK, show=False, importance=0) adr.add_value('text/plain', f"Created new LabBook: {username}/{name}") # Create activity record ar = ActivityRecord(ActivityType.LABBOOK, message=f"Created new LabBook: {username}/{name}", show=True, importance=255, linked_commit=lb.git.commit_hash) ar.add_detail_object(adr) store = ActivityStore(lb) store.create_activity_record(ar) cm = ComponentManager(lb) cm.add_base(repository, base_id, revision) return CreateLabbook(labbook=Labbook(owner=username, name=lb.name))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, branch_name, client_mutation_id=None): """Method to perform mutation Args: input: context: info: Returns: """ username = get_logged_in_username() # Load an existing LabBook labbook_obj = LabBook(author=get_logged_in_author()) labbook_obj.from_name(username, owner, labbook_name) # Checkout labbook_obj.checkout_branch(branch_name) return CheckoutBranch(labbook=Labbook(owner=owner, name=labbook_name))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, other_branch_name, override_method="abort", client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): override = MergeOverride(override_method) bm = BranchManager(lb, username=username) if override == MergeOverride.ABORT: bm.merge_from(other_branch=other_branch_name) elif override == MergeOverride.OURS: bm.merge_use_ours(other_branch=other_branch_name) elif override == MergeOverride.THEIRS: bm.merge_use_theirs(other_branch=other_branch_name) else: raise ValueError(f"Unknown override method {override}") # Run update_linked_datasets() to initialize and cleanup dataset submodules. You don't need to schedule # auto-import jobs because the user will have switched to the branch to pull it before merging. InventoryManager().update_linked_datasets(lb, username) return MergeFromBranch(Labbook(id="{}&{}".format(owner, labbook_name), name=labbook_name, owner=owner))
def mutate_and_get_payload(cls, root, info, name, description, repository, component_id, revision, is_untracked=False, client_mutation_id=None): username = get_logged_in_username() # Create a new empty LabBook lb = LabBook(author=get_logged_in_author()) # TODO: Set owner/namespace properly once supported fully lb.new(owner={"username": username}, username=username, name=name, description=description, bypass_lfs=is_untracked) if is_untracked: FileOperations.set_untracked(lb, 'input') FileOperations.set_untracked(lb, 'output') input_set = FileOperations.is_set_untracked(lb, 'input') output_set = FileOperations.is_set_untracked(lb, 'output') if not (input_set and output_set): raise ValueError( f'{str(lb)} untracking for input/output in malformed state' ) if not lb.is_repo_clean: raise ValueError( f'{str(lb)} should have clean Git state after setting for untracked' ) # Create a Activity Store instance store = ActivityStore(lb) # Create detail record adr = ActivityDetailRecord(ActivityDetailType.LABBOOK, show=False, importance=0) adr.add_value('text/plain', f"Created new LabBook: {username}/{name}") # Create activity record ar = ActivityRecord(ActivityType.LABBOOK, message=f"Created new LabBook: {username}/{name}", show=True, importance=255, linked_commit=lb.git.commit_hash) ar.add_detail_object(adr) # Store store.create_activity_record(ar) # Add Base component cm = ComponentManager(lb) cm.add_component("base", repository, component_id, revision) # Prime dataloader with labbook you just created dataloader = LabBookLoader() dataloader.prime(f"{username}&{username}&{lb.name}", lb) # Get a graphene instance of the newly created LabBook return CreateLabbook(labbook=Labbook(owner=username, name=lb.name))