def test_load_labels(labels_file_load: str, labels: typing.List[Label]) -> None: """Test that read_lables() correctly reads the TOML file and returns a mapping of names to Label instances. """ want = {l.name: l for l in labels} got = read_labels(labels_file_load) assert got == want
def sync_cmd(context: LabelsContext, 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) repository = Repository(owner, repo) try: remote_labels = { l.name: l for l in context.client.list_labels(repository) } 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: context.client.delete_label(repository, name=name) except LabelsException as exc: click.echo(str(exc), err=True) failures.append(name) for name, label in labels_to_update.items(): try: context.client.edit_label(repository, 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: context.client.create_label(repository, 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"), ), )
def sync_cmd(client: Client, owner: str, repo: str, filename: str, dryrun: bool) -> None: """Load labels from disk, sync them for the specified remote repo and update the local labels file.""" 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"), ), )