Skip to content

Commit

Permalink
fix: put some default options to stack group (#568)
Browse files Browse the repository at this point in the history
Global options `--token` and `--github-server` are related to the `stack` group and should not have their defaults computed for other groups since they may break others commands.

--debug is still a global option:

$ `mergify --debug stack push`

This does not work anymore:

$ `mergify --token foobar stack push`

Global `stack` options are to be passed like this:

$ `mergify stack --token foobar push`
  • Loading branch information
lecrepont01 authored Dec 3, 2024
1 parent 18ea280 commit aaa1d7f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
54 changes: 1 addition & 53 deletions mergify_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,28 @@

from __future__ import annotations

import asyncio
import os
from urllib import parse

import click
import click.decorators
import click_default_group

from mergify_cli import VERSION
from mergify_cli import console
from mergify_cli import utils
from mergify_cli.ci import cli as ci_cli_mod
from mergify_cli.stack import cli as stack_cli_mod


async def get_default_github_server() -> str:
try:
result = await utils.git("config", "--get", "mergify-cli.github-server")
except utils.CommandError:
result = ""

url = parse.urlparse(result or "https://api.github.com/")
url = url._replace(scheme="https")

if url.hostname == "api.github.com":
url = url._replace(path="")
else:
url = url._replace(path="/api/v3")
return url.geturl()


async def get_default_token() -> str:
token = os.environ.get("GITHUB_TOKEN", "")
if not token:
try:
token = await utils.run_command("gh", "auth", "token")
except utils.CommandError:
console.print(
"error: please make sure that gh client is installed and you are authenticated, or set the "
"'GITHUB_TOKEN' environment variable",
)
if utils.is_debug():
console.print(f"[purple]DEBUG: token: {token}[/]")
return token


@click.group(
cls=click_default_group.DefaultGroup,
default="stack",
default_if_no_args=True,
)
@click.option("--debug", is_flag=True, default=False, help="debug mode")
@click.version_option(VERSION)
@click.option(
"--github-server",
default=lambda: asyncio.run(get_default_github_server()),
)
@click.option(
"--token",
default=lambda: asyncio.run(get_default_token()),
help="GitHub personal access token",
)
@click.pass_context
def cli(
ctx: click.Context,
debug: bool,
github_server: str,
token: str,
) -> None:
ctx.obj = {
"debug": debug,
"github_server": github_server,
"token": token,
}
ctx.obj = {"debug": debug}


cli.add_command(stack_cli_mod.stack)
Expand Down
60 changes: 60 additions & 0 deletions mergify_cli/stack/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
import os
from urllib import parse

import click
import click_default_group

from mergify_cli import console
from mergify_cli import utils
from mergify_cli.stack import checkout as stack_checkout_mod
from mergify_cli.stack import edit as stack_edit_mod
Expand All @@ -25,11 +28,68 @@ def trunk_type(
return result[0], result[1]


async def get_default_github_server() -> str:
try:
result = await utils.git("config", "--get", "mergify-cli.github-server")
except utils.CommandError:
result = ""

url = parse.urlparse(result or "https://api.github.com/")
url = url._replace(scheme="https")

if url.hostname == "api.github.com":
url = url._replace(path="")
else:
url = url._replace(path="/api/v3")
return url.geturl()


async def get_default_token() -> str:
token = os.environ.get("GITHUB_TOKEN", "")
if not token:
try:
token = await utils.run_command("gh", "auth", "token")
except utils.CommandError:
console.print(
"error: please make sure that gh client is installed and you are authenticated, or set the "
"'GITHUB_TOKEN' environment variable",
)
if utils.is_debug():
console.print(f"[purple]DEBUG: token: {token}[/]")
return token


def token_to_context(ctx: click.Context, _param: click.Parameter, value: str) -> None:
ctx.obj["token"] = value


def github_server_to_context(
ctx: click.Context,
_param: click.Parameter,
value: str,
) -> None:
ctx.obj["github_server"] = value


stack = click_default_group.DefaultGroup(
"stack",
default="push",
default_if_no_args=True,
help="Manage pull requests stack",
params=[
click.Option(
param_decls=["--token"],
default=lambda: asyncio.run(get_default_token()),
help="GitHub personal access token",
callback=token_to_context,
),
click.Option(
param_decls=["--github-server"],
default=lambda: asyncio.run(get_default_github_server()),
help="GitHub API server",
callback=github_server_to_context,
),
],
)


Expand Down

0 comments on commit aaa1d7f

Please sign in to comment.