assignee_to_reviewer.py 1.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import logging
import os
from argparse import ArgumentParser

from gitlab import Gitlab
from gitlab.exceptions import GitlabHttpError

from assignbot.__main__ import all_reviews


def assignee_to_reviewer(cnx):
    """Gitlab added a "reviewer" role on merge requests. This function changes
    the author to the assignee and the previous assignee to the reviewer.
    """
    merges_requests = all_reviews(cnx, assignee_id="Any")
    for merge_request in merges_requests:
        if merge_request.reviewers:
            continue
        assignee = merge_request.assignee
        author = merge_request.author
        logging.info(
22
23
            f'Assigning {author["name"]} and request review from '
            f'{assignee["name"]} on {merge_request.web_url}'
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        )
        try:
            cnx.http_put(
                f"/projects/{merge_request.project_id}/merge_requests/{merge_request.iid}",
                query_data={
                    "assignee_id": author["id"],
                    "reviewer_ids": [assignee["id"]],
                },
            )
        except GitlabHttpError:
            logging.error(
                f"no permission to assign a reviewer to {merge_request.title}"
                f" ({merge_request.web_url})"
            )


if __name__ == "__main__":
    logging.getLogger().setLevel(logging.INFO)
    parser = ArgumentParser()
    parser.add_argument("--gitlab-url", type=str, default=os.getenv("GITLAB_URL"))
    parser.add_argument("--gitlab-token", type=str, default=os.getenv("GITLAB_TOKEN"))
    args = parser.parse_args()
    cnx = Gitlab(args.gitlab_url, args.gitlab_token)
    assignee_to_reviewer(cnx)