Skip to content

Commit

Permalink
adds support for Gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
apoclyps committed Jun 26, 2021
1 parent dec7995 commit 61cd808
Show file tree
Hide file tree
Showing 21 changed files with 598 additions and 102 deletions.
4 changes: 3 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ exclude = .svn,CVS,.bzr,.hg,.git,__pycache,.venv,migrations,settings,tests,.tox,
max-complexity = 20
max-line-length = 119
select = C,E,F,W,B,B950
ignore = E203,E501,D107,D102,W503
ignore = E203,E501,D107,D102,W503,R0801
# Ignore unused import warnings in __init__.py files
per-file-ignores = __init__.py:F401
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ repos:
types: [python]
args: ["--ignore-missing-imports", "--scripts-are-modules", "--config-file=mypy.ini"]
require_serial: true
additional_dependencies: []
additional_dependencies: [
types-click==7.1.2,
types-freezegun==0.1.4
]

- repo: local
hooks:
Expand Down
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ disable=print-statement,
no-self-use,
too-few-public-methods,
too-many-instance-attributes,
duplicate-code,

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you want to get up and running with Reviews immediately, run:
```bash
export GITHUB_USER="your-github-username"
export GITHUB_TOKEN="your personal GitHub token used for interacting with the API"
export REVIEWS_REPOSITORY_CONFIGURATION="apoclyps/reviews"
export REVIEWS_GITHUB_REPOSITORY_CONFIGURATION="apoclyps/reviews"

pip install --upgrade reviews

Expand All @@ -30,6 +30,8 @@ reviews dashboard --no-reload

### Additional Support

#### View Configuration

If you wish to view the configuration used by reviews at any time, you can use the following command to show all configuration (with secrets hidden or shown):

```bash
Expand All @@ -38,8 +40,22 @@ reviews config --hide
reviews config --show
```

#### Gitlab

If you wish to use `reviews` with Gitlab, you will need to specify the configuration like so: `product id:project name/repository` and use the `--provider=gitlab` flag when running `reviews`:

```bash
export GITLAB_USER=user
export GITLAB_TOKEN=token
export REVIEWS_GITLAB_REPOSITORY_CONFIGURATION="27629846:apoclyps/reviews"

reviews dashboard --provider=gitlab
```

**Experimental**

#### Github Enterprise

The following features have been implemented but support is considered experimental and it may be removed in a future release.

If you wish to use Reviews with a Github Enterprise instead of Github (https://api.github.com", you can set the `GITHUB_URL` with your custom hostname. You will also need to ensure your `GITHUB_USER` and `GITHUB_TOKEN` exist within your Enterprise Github instance:
Expand All @@ -65,7 +81,7 @@ $ source env/bin/activate
If you wish to keep a copy of Reviews on your host system, you can install and run it using:

```bash
$ export REVIEWS_REPOSITORY_CONFIGURATION="apoclyps/reviews"
$ export REVIEWS_GITHUB_REPOSITORY_CONFIGURATION="apoclyps/reviews"
$ python -m venv env
$ source env/bin/activate
$ python -m pip install -e .
Expand Down Expand Up @@ -101,15 +117,15 @@ Create a `settings.ini` next to your configuration module in the form:

```bash
[settings]
REVIEWS_REPOSITORY_CONFIGURATION=apoclyps/micropython-by-example
REVIEWS_GITHUB_REPOSITORY_CONFIGURATION=apoclyps/micropython-by-example
Note: Since ConfigParser supports string interpolation, to represent the character % you need to escape it as %%.
```

#### Env file
Create a `.env` text file on your repository's root directory in the form:

```bash
REVIEWS_REPOSITORY_CONFIGURATION=apoclyps/micropython-by-example
REVIEWS_GITHUB_REPOSITORY_CONFIGURATION=apoclyps/micropython-by-example
```

#### Providing a configuration path
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ humanize==3.9.0
keyboard==0.13.5
PyGithub==1.55
python-decouple==3.4
python-gitlab==2.8.0
rich==10.4.0
idna==2.10
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pre-commit==2.13.0
pylint==2.8.3
pytest-cov==2.12.1
pytest==6.2.4
pyupgrade==2.19.4
types-click==7.1.2
types-freezegun==0.1.4
vulture==2.3
pyupgrade==2.19.4
44 changes: 27 additions & 17 deletions reviews/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ def config(show: bool) -> None:


@cli.command(help="Visualize code review requests as a Dashboard")
@click.option("-p", "--provider", type=str, default="github", show_default="github")
@click.option("-r", "--reload/--no-reload", default=True, is_flag=True)
def dashboard(reload: bool) -> None:
def dashboard(reload: bool, provider: str) -> None:
"""
Command:\n
reviews dashboard

Usage:\n
reviews dashboard --reload \n
reviews dashboard --no-reload \n
reviews dashboard --provider=github \n
reviews dashboard --provider=gitlab \n
"""
console = Console()
style = "bold"
Expand All @@ -63,32 +66,39 @@ def dashboard(reload: bool) -> None:
"README[/link] for more methods on providing configuration."
"\n"
)
missing_github_token_msg = (
"\n[red]GITHUB_TOKEN[/] is required and has not been provided as "
"configuration needed to use Reviews. \n\n"
f"{github_token_msg}"
)
invalid_github_token_msg = (
"\n[red]GITHUB_TOKEN[/] is required configuration and an invalid "
"token has been supplied to Reviews. \n\n"
f"{github_token_msg}"
)

if not GITHUB_TOKEN:
console.print(
missing_github_token_msg,
style=style,
missing_github_token_msg = (
"\n[red]GITHUB_TOKEN[/] is required and has not been provided as "
"configuration needed to use Reviews. \n\n"
f"{github_token_msg}"
)
console.print(missing_github_token_msg, style=style)
return

provider = provider.lower()
if provider not in ["github", "gitlab"]:
invalid_providern_msg = (
"\n[red]--provider=[/] uses an unsupported value. "
"Please provider either `github` or `gitlab` as the provider value. \n\n"
f"{github_token_msg}"
)
console.print(invalid_providern_msg, style=style)
return

try:
click.echo("loading dashboard")
click.echo(f"loading dashboard for {provider}...")

if reload:
render()
render(provider=provider)
else:
single_render()
single_render(provider=provider)
except InvalidGithubToken:
invalid_github_token_msg = (
"\n[red]GITHUB_TOKEN[/] is required configuration and an invalid "
"token has been supplied to Reviews. \n\n"
f"{github_token_msg}"
)
console.print(
invalid_github_token_msg,
style=style,
Expand Down
60 changes: 35 additions & 25 deletions reviews/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from . import config
from .config import render_config_table
from .controller import PullRequestController
from .controller import GithubPullRequestController, GitlabPullRequestController, PullRequestController
from .layout import (
RenderLayoutManager,
generate_layout,
Expand All @@ -29,26 +29,18 @@ def add_log_event(message: str) -> List[Tuple[str, str]]:
return logs


def _render_pull_requests(controller: PullRequestController, configuration: List[Tuple[str, str]]) -> Panel:
"""Renders all pull requests for the provided configuration"""

tables = [controller.retrieve_pull_requests(org=org, repository=repo) for (org, repo) in configuration]

# filter unrenderable `None` results
return Panel(
RenderGroup(*[t for t in tables if t]),
title="Activity",
border_style="blue",
)


def single_render() -> None:
def single_render(provider: str) -> None:
"""Renders the Terminal UI Dashboard once before closing the application"""

configuration = config.get_configuration()
controller = PullRequestController()
configuration = None
body = None

body = _render_pull_requests(controller=controller, configuration=configuration)
if provider == "gitlab":
configuration = config.get_configuration(config=config.REVIEWS_GITLAB_REPOSITORY_CONFIGURATION)
body = GitlabPullRequestController().render(configuration=configuration)
else:
configuration = config.get_configuration(config=config.REVIEWS_GITHUB_REPOSITORY_CONFIGURATION)
body = GithubPullRequestController().render(configuration=configuration)

layout_manager = RenderLayoutManager(layout=generate_layout(log=False, footer=False))
layout_manager.render_layout(
Expand All @@ -67,7 +59,7 @@ def single_render() -> None:
add_log_event(message="updated")


def render() -> None:
def render(provider: str) -> None:
"""Renders Terminal UI Dashboard"""
(
job_progress,
Expand All @@ -79,8 +71,16 @@ def render() -> None:
# initial load should be from database
add_log_event(message="initializing...")

configuration = config.get_configuration()
controller = PullRequestController()
configuration = None
controller: PullRequestController

if provider == "gitlab":
configuration = config.get_configuration(config=config.REVIEWS_GITLAB_REPOSITORY_CONFIGURATION)
controller = GitlabPullRequestController()
else:
configuration = config.get_configuration(config=config.REVIEWS_GITHUB_REPOSITORY_CONFIGURATION)
controller = GitlabPullRequestController()
controller = GithubPullRequestController()

layout_manager = RenderLayoutManager(layout=generate_layout())
layout_manager.render_layout(
Expand Down Expand Up @@ -109,7 +109,7 @@ def render() -> None:
# update view (blocking operation)
layout_manager.render_layout(
progress_table=progress_table,
body=_render_pull_requests(controller=controller, configuration=configuration),
body=controller.render(configuration=configuration),
pull_request_component=generate_tree_layout(configuration=configuration),
log_component=generate_log_table(logs=logs),
)
Expand All @@ -132,19 +132,29 @@ def render_config(show: bool) -> None:
configurations = [
{
"name": "GITHUB_TOKEN",
"value": config.GITHUB_TOKEN if show else "".join("*" for _ in range(0, len(config.GITHUB_TOKEN))),
"value": config.GITHUB_TOKEN if show else "".join("*" for _ in range(len(config.GITHUB_TOKEN))),
},
{"name": "GITHUB_USER", "value": config.GITHUB_USER},
{"name": "GITHUB_URL", "value": config.GITHUB_URL},
{
"name": "GITLAB_TOKEN",
"value": config.GITLAB_TOKEN if show else "".join("*" for _ in range(len(config.GITLAB_TOKEN))),
},
{"name": "GITLAB_USER", "value": config.GITLAB_USER},
{"name": "GITLAB_URL", "value": config.GITLAB_URL},
{"name": "REVIEWS_PATH_TO_CONFIG", "value": f"{config.REVIEWS_PATH_TO_CONFIG}"},
{
"name": "GITHUB_DEFAULT_PAGE_SIZE",
"value": f"{config.GITHUB_DEFAULT_PAGE_SIZE}",
},
{"name": "REVIEWS_DELAY_REFRESH", "value": f"{config.REVIEWS_DELAY_REFRESH}"},
{
"name": "REVIEWS_REPOSITORY_CONFIGURATION",
"value": ", ".join(config.REVIEWS_REPOSITORY_CONFIGURATION),
"name": "REVIEWS_GITHUB_REPOSITORY_CONFIGURATION",
"value": ", ".join(config.REVIEWS_GITHUB_REPOSITORY_CONFIGURATION),
},
{
"name": "REVIEWS_GITLAB_REPOSITORY_CONFIGURATION",
"value": ", ".join(config.REVIEWS_GITLAB_REPOSITORY_CONFIGURATION),
},
{
"name": "REVIEWS_LABEL_CONFIGURATION",
Expand Down
7 changes: 6 additions & 1 deletion reviews/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
GITHUB_TOKEN,
GITHUB_URL,
GITHUB_USER,
GITLAB_DEFAULT_PAGE_SIZE,
GITLAB_TOKEN,
GITLAB_URL,
GITLAB_USER,
REVIEWS_DELAY_REFRESH,
REVIEWS_GITHUB_REPOSITORY_CONFIGURATION,
REVIEWS_GITLAB_REPOSITORY_CONFIGURATION,
REVIEWS_LABEL_CONFIGURATION,
REVIEWS_PATH_TO_CONFIG,
REVIEWS_REPOSITORY_CONFIGURATION,
)
Loading

0 comments on commit 61cd808

Please sign in to comment.