Example #1
0
def test_list_labels(client: Client, owner: str, repo: str) -> None:
    """Test that list_labels() requests the labels for the specified repo and
    returns a list of Label instances.
    """
    labels = client.list_labels(owner, repo)

    expected_params = [
        {
            "name": "infra",
            "description": "Tasks related to Docker/CI etc.",
            "color": "f9d03b",
        },
        {
            "name": "docs",
            "description": "Tasks to write and update documentation",
            "color": "2abf88",
        },
        {
            "name": "bug",
            "description": "Bugs and problems with cookiecutter",
            "color": "ea707a",
        },
    ]

    assert [l.params_dict for l in labels] == expected_params
Example #2
0
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
Example #3
0
def labels(ctx, username: str, token: str, verbose: bool) -> None:
    """labels - CLI to manage GitHub issue labels."""

    logger = create_logger()
    if verbose:
        logger.setLevel(logging.DEBUG)
        logger.debug("Logger initialized")
    else:
        logger.setLevel(logging.INFO)

    ctx.obj = LabelsContext(Client(HTTPBasicAuth(username, token)))
Example #4
0
def labels(ctx, token: str, verbose: bool) -> None:
    """labels - CLI to manage GitHub issue labels."""

    logger = create_logger()
    if verbose:
        logger.setLevel(logging.DEBUG)
        logging.getLogger("requests.packages.urllib3").setLevel("DEBUG")
    else:
        logger.setLevel(logging.INFO)

    ctx.obj = LabelsContext(Client(token))
Example #5
0
def test_get_label(client: Client, owner: str, repo: str) -> None:
    """Test that get_label() requests the specified label for the repo and
    returns a Label instance.
    """
    label = client.get_label(owner, repo, name="bug")

    expected_params = {
        "name": "bug",
        "description": "Bugs and problems with cookiecutter",
        "color": "ea707a",
    }

    assert label.params_dict == expected_params
Example #6
0
def test_edit_label(client: Client, repo: Repository, label: Label) -> None:
    """Test that edit_label() requests the label to be updated and returns
    the updated Label instance.
    """
    label = client.edit_label(repo, name="bug", label=label)

    expected_params = {
        "name": "bug",
        "description": "Bugs and problems with cookiecutter",
        "color": "ea707a",
    }

    assert label.params_dict == expected_params
Example #7
0
def fetch_cmd(client: Client, owner: str, repo: str, filename: str) -> None:
    """Fetch Labels for the specified repo and write them to disk."""

    try:
        labels = client.list_labels(owner, repo)
    except LabelsException as exc:
        click.echo(str(exc))
        sys.exit(1)

    write_labels(
        filename,
        sorted(labels, key=operator.attrgetter("name", "description",
                                               "color")),
    )
Example #8
0
def fetch_cmd(client: Client, owner: str, repo: str, filename: str) -> None:
    """Fetch labels for a GitHub repository.

    This will write the labels information to disk to the specified filename.
    """
    try:
        labels = client.list_labels(owner, repo)
    except LabelsException as exc:
        click.echo(str(exc))
        sys.exit(1)

    write_labels(
        filename,
        sorted(labels, key=operator.attrgetter("name", "description", "color")),
    )
Example #9
0
def test_list_labels_pagination(client: Client, repo: Repository) -> None:
    """Test that list_labels() supports pagination."""
    labels = client.list_labels(repo)

    expected_params = [
        {
            "name": "bug",
            "description": "Bugs and problems with cookiecutter",
            "color": "ea707a",
        },
        {
            "name": "docs",
            "description": "Tasks to write and update documentation",
            "color": "2abf88",
        },
        {
            "name": "infra",
            "description": "Tasks related to Docker/CI etc.",
            "color": "f9d03b",
        },
    ]

    assert [label.params_dict for label in labels] == expected_params
Example #10
0
def test_delete_label(client: Client, owner: str, repo: str) -> None:
    """Test that delete_label() performs the correct request."""

    client.delete_label(owner, repo, name="bug")
Example #11
0
def fixture_client(base_url: str) -> Client:
    """Return a GitHub API client."""
    return Client(HTTPBasicAuth("", ""), base_url=base_url)
Example #12
0
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"),
        ),
    )
Example #13
0
def test_delete_label(client: Client, repo: Repository) -> None:
    """Test that delete_label() performs the correct request."""

    client.delete_label(repo, name="bug")
Example #14
0
def fixture_client(base_url: str, username: str, token: str) -> Client:
    """Return a GitHub API client."""
    return Client(HTTPBasicAuth(username, token), base_url=base_url)
Example #15
0
def fixture_client(base_url: str, token: str) -> Client:
    """Return a GitHub API client."""
    return Client(token, base_url=base_url)