Skip to content

Commit

Permalink
Fixes #181 (WIP) (#382)
Browse files Browse the repository at this point in the history
* Fixes #181 (WIP)
- Added PR Github Author column to the dashboard.

* Fixes #181 (WIP)
- Trying to add PR Gitlab Author column to the dashboard.

* refactors user to author

* adds configuration to show/hide author

* updates gitlab to handle changes to their API

Co-authored-by: apoclyps <kyle90adam@hotmail.com>
  • Loading branch information
paulosgf and apoclyps committed Feb 7, 2022
1 parent 1c95f30 commit 8da0734
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ python -m reviews config

If at any time, you want to confirm your configuration reflects the file you have provided, you can use `reviews config` to view what current configuration of Reviews.

#### Configuring Layout

Reviews provides the option to configure some optional columns to display/hide those base on provided configuration.

For example, the following configuration can be used to show or hide authors:

```bash
export REVIEWS_AUTHOR=false
```

### Testing

A test suite has been included to ensure Reviews functions correctly.
Expand Down
5 changes: 4 additions & 1 deletion reviews/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def render_config(show: bool) -> None:
},
{"name": "GITLAB_USER", "value": settings.GITLAB_USER},
{"name": "GITLAB_URL", "value": settings.GITLAB_URL},
{"name": "REVIEWS_PATH_TO_CONFIG", "value": f"{settings.REVIEWS_PATH_TO_CONFIG}"},
{
"name": "REVIEWS_PATH_TO_CONFIG",
"value": f"{settings.REVIEWS_PATH_TO_CONFIG}",
},
{
"name": "GITHUB_DEFAULT_PAGE_SIZE",
"value": f"{settings.GITHUB_DEFAULT_PAGE_SIZE}",
Expand Down
1 change: 1 addition & 0 deletions reviews/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
GITLAB_TOKEN,
GITLAB_URL,
GITLAB_USER,
REVIEWS_AUTHOR,
REVIEWS_DELAY_REFRESH,
REVIEWS_GITHUB_REPOSITORY_CONFIGURATION,
REVIEWS_GITLAB_REPOSITORY_CONFIGURATION,
Expand Down
3 changes: 3 additions & 0 deletions reviews/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
else:
config = AutoConfig()

# Layout Config
REVIEWS_AUTHOR = config("REVIEWS_AUTHOR", cast=bool, default=True)

# Github Config
GITHUB_TOKEN = config("GITHUB_TOKEN", cast=str, default="")
GITHUB_USER = config("GITHUB_USER", cast=str, default="")
Expand Down
7 changes: 5 additions & 2 deletions reviews/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict, List, Tuple, Union

from github.PullRequest import PullRequest as ghPullRequest
from gitlab.v4.objects.merge_requests import MergeRequest as GitlabMergeRequest
from gitlab.v4.objects import ProjectMergeRequest as GitlabMergeRequest
from rich.console import Group
from rich.panel import Panel
from rich.table import Table
Expand Down Expand Up @@ -93,6 +93,7 @@ def _get_reviews(pull_request: ghPullRequest) -> Dict[str, str]:
deletions=pull_request.deletions,
created_at=pull_request.created_at.astimezone(tz=timezone.utc),
updated_at=pull_request.updated_at.astimezone(tz=timezone.utc),
author=pull_request.user.login,
approved=approved_by_me,
approved_by_others=approved_by_others,
labels=labels,
Expand Down Expand Up @@ -145,8 +146,9 @@ def update_pull_requests(self, project_id: str, namespace: str) -> List[PullRequ
def _get_reviews(pull_request: GitlabMergeRequest) -> Dict[str, str]:
"""Inner function to retrieve reviews for a pull request"""
reviews = pull_request.approvals.get()
approvers = getattr(reviews, "approvers", [])

return {reviewer["user"]["username"]: "approved" for reviewer in reviews.approvers}
return {approver["user"]["username"]: "approved" for approver in approvers} if approvers else {}

# ProjectMergeRequest
pull_requests = self.client.get_pull_requests(project_id=project_id, namespace=namespace)
Expand Down Expand Up @@ -199,6 +201,7 @@ def get_labels(labels: List[str]) -> List[Label]:
PullRequest(
number=pull_request.iid,
title=pull_request.title,
author=pull_request.author["username"],
draft=pull_request.draft,
additions=0,
deletions=0,
Expand Down
16 changes: 12 additions & 4 deletions reviews/layout/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def render_pull_request_table(
"""Renders a list of pull requests as a table"""

show_diff = False
show_author = settings.REVIEWS_AUTHOR

if pull_requests and pull_requests[0].repository_url:
link = f"[link={pull_requests[0].repository_url}]{title}[/link]"
Expand All @@ -64,7 +65,11 @@ def render_pull_request_table(

table = Table(show_header=True, header_style="bold white")
table.add_column("#", style="dim", width=5)
table.add_column(link, width=75)
table.add_column(link, width=55)

if show_author:
table.add_column("Author", width=20)

table.add_column("Labels", width=30)
table.add_column("Diff +/-", width=10)
table.add_column("Activity", width=15)
Expand All @@ -74,13 +79,16 @@ def render_pull_request_table(
label_colour_map = get_label_colour_map()

for pr in sorted(pull_requests, key=attrgetter("updated_at"), reverse=True):

row = [
f"[white]{pr.number} ",
pr.render_title(),
pr.render_labels(label_colour_map),
]

if show_author:
row.append(pr.render_author())

row.append(pr.render_labels(label_colour_map))

if show_diff:
row.append(pr.render_diff())
else:
Expand All @@ -105,7 +113,7 @@ def generate_layout(log: bool = True, footer: bool = True) -> Layout:
layout.split(*sections)

layout["main"].split_row( # type: ignore
Layout(name="left_side", size=40),
Layout(name="left_side", size=25),
Layout(name="body", ratio=2, minimum_size=90),
)

Expand Down
4 changes: 2 additions & 2 deletions reviews/source_control/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from github.PullRequest import PullRequest
from github.Repository import Repository
from gitlab import Gitlab
from gitlab.v4.objects.merge_requests import MergeRequest as GitlabMergeRequest
from gitlab.v4.objects import ProjectMergeRequest as GitlabMergeRequest
from gitlab.v4.objects.projects import Project as GitlabRepository

from .. import config
Expand Down Expand Up @@ -70,5 +70,5 @@ def _get_pull_requests(

def get_pull_requests(self, project_id: str, namespace: str) -> List[GitlabMergeRequest]:
"""Returns a list of pull requests for a given organization and repository."""
repository = self.get_repository(project_id=project_id, namespace=namespace)
repository = self.get_repository(project_id=project_id, namespace=namespace) # type: ignore
return repository.mergerequests.list(state="opened", order_by="created_at", sort="asc") # type: ignore
5 changes: 5 additions & 0 deletions reviews/source_control/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PullRequest:

number: int
title: str
author: str
draft: bool
repository_url: str
link: str
Expand Down Expand Up @@ -82,6 +83,10 @@ def render_updated_at(self, since: Optional[datetime] = None) -> str:

return f"{colour}{humanize.naturaltime(self.updated_at, when=now)}{suffix}"

def render_author(self) -> str:
"""Renders the author as a colourised string"""
return f"[grey]{self.author}[/]"

def render_diff(self) -> str:
"""Renders the additions and deletions using the Github convention of +/-"""
return f"[green]+{self.additions}[/green] [red]-{self.deletions}[/red]"
1 change: 1 addition & 0 deletions tests/source_control/models/test_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def pull_request(organization, repository) -> PullRequest:
return PullRequest(
number=1,
draft=True,
author="me",
repository_url=f"https://www.github.com/{organization}/{repository}",
link=f"https://www.github.com/{organization}/{repository}/pull/1",
title="[1] Initial Commit",
Expand Down

0 comments on commit 8da0734

Please sign in to comment.