def test_create_label(client: Client, repo: Repository, label: Label) -> None: """Test that create_label() requests the label to be created and returns a Label instance. """ created_label = client.create_label(repo, label=label) assert created_label.params_dict == label.params_dict
def sync_cmd( client: Client, owner: str, repo: str, filename: str, dryrun: bool ) -> None: """Sync labels with a GitHub repository. On success this will also update the local labels file, so that section names match the `name` parameter. """ labels_to_delete = {} labels_to_update = {} labels_to_create = {} labels_to_ignore = {} local_labels = read_labels(filename) try: remote_labels = {l.name: l for l in client.list_labels(owner, repo)} except LabelsException as exc: click.echo(str(exc), err=True) sys.exit(1) for remote_name, local_label in local_labels.items(): if remote_name in remote_labels: remote_label = remote_labels[remote_name] if local_label.params_dict == remote_label.params_dict: labels_to_ignore[remote_name] = local_label else: labels_to_update[remote_name] = local_label else: if remote_name == local_label.name: labels_to_create[local_label.name] = local_label else: click.echo( f'There is no remote label "{remote_name}" and ' f"this name does not match the name " f'parameter: "{local_label.name}"', err=True, ) sys.exit(1) for remote_name, remote_label in remote_labels.items(): if remote_name in labels_to_update: continue if remote_name in labels_to_ignore: continue labels_to_delete[remote_name] = remote_label if dryrun: # Do not modify remote labels, but only print info dryrun_echo( labels_to_delete, labels_to_update, labels_to_create, labels_to_ignore ) sys.exit(0) failures = [] for name in labels_to_delete.keys(): try: client.delete_label(owner, repo, name=name) except LabelsException as exc: click.echo(str(exc), err=True) failures.append(name) for name, label in labels_to_update.items(): try: client.edit_label(owner, repo, name=name, label=label) except LabelsException as exc: click.echo(str(exc), err=True) failures.append(name) for name, label in labels_to_create.items(): try: client.create_label(owner, repo, label=label) except LabelsException as exc: click.echo(str(exc), err=True) failures.append(name) if failures: sys.exit(1) # Make sure to write the local labels file to update TOML sections write_labels( filename, sorted( local_labels.values(), key=operator.attrgetter("name", "description", "color"), ), )