Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor this function to accept a link #195

Closed
github-actions bot opened this issue Jun 23, 2021 · 0 comments · Fixed by #192
Closed

refactor this function to accept a link #195

github-actions bot opened this issue Jun 23, 2021 · 0 comments · Fixed by #192
Labels

Comments

@github-actions
Copy link

refactor this function to accept a link

# TODO: refactor this function to accept a link

            )

        return code_review_requests


class GitlabPullRequestController:
    """retrieve and store pull requests."""

    def __init__(self) -> None:
        self.client = GitlabAPI()

    def retrieve_pull_requests(self, project_id: int, namespace: str) -> Union[Table, None]:
        """Renders Terminal UI Dashboard"""
        pull_requests = []
        try:
            pull_requests = self.update_pull_requests(project_id=project_id, namespace=namespace)
        except RepositoryDoesNotExist:
            # TODO: refactor this function for usage with Gitlab
            return render_repository_does_not_exist(
                title=f"{namespace} does not exist",
                org="",
                repository="",
            )

        if not pull_requests:
            return None

        # TODO: refactor this function to accept a link
        return render_pull_request_table(
            title=namespace,
            pull_requests=pull_requests,
            org="",
            repository="",
        )

    def update_pull_requests(self, project_id: int, namespace: str) -> List[PullRequest]:
        """Updates repository models."""

        def _get_reviews(pull_request: ghPullRequest) -> Dict[str, str]:
            """Inner function to retrieve reviews for a pull request"""
            reviews = pull_request.approvals.get()

            return {reviewer["user"]["username"]: True for reviewer in reviews.approvers}

        # ProjectMergeRequest
        pull_requests = self.client.get_pull_requests(project_id=project_id, namespace=namespace)

        code_review_requests = []
        for pull_request in pull_requests:

            reviews = _get_reviews(pull_request=pull_request)

            if pull_request.author["username"] == config.GITLAB_USER:
                approved_by_me = "AUTHOR"
            else:
                approved_by_me = reviews.get(config.GITLAB_USER, "")  # NOQA: R1721

            approved_by_others = any(
                [True for user, status in reviews.items() if user != config.GITLAB_USER and status == "APPROVED"]
            )

            labels = [Label(name=label) for label in pull_request.labels]

            code_review_requests.append(
                PullRequest(
                    number=pull_request.iid,
                    title=pull_request.title,
                    draft=pull_request.draft,
                    additions=0,
                    deletions=0,
                    created_at=datetime.strptime(pull_request.created_at, "%Y-%m-%dT%H:%M:%S.%f%z"),
                    updated_at=datetime.strptime(pull_request.updated_at, "%Y-%m-%dT%H:%M:%S.%f%z"),
                    approved=approved_by_me,
                    approved_by_others=approved_by_others,
                    labels=labels,
                )
            )

        return code_review_requests

70abae0dcb7542d7368b5f7d18bf7860fa6ffd26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

0 participants