File indexing completed on 2024-12-01 08:16:20
0001 """ 0002 Module containing classes for listing merge requests 0003 """ 0004 0005 # SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im> 0006 # 0007 # SPDX-License-Identifier: GPL-2.0-or-later 0008 import argparse 0009 from typing import List 0010 0011 from gitlab.v4.objects import ProjectMergeRequest 0012 0013 from lab.repositoryconnection import RepositoryConnection 0014 from lab.utils import TextFormatting 0015 from lab.table import Table 0016 0017 0018 def parser( 0019 subparsers: argparse._SubParsersAction, # pylint: disable=protected-access 0020 ) -> argparse.ArgumentParser: 0021 """ 0022 Subparser for merge request list command 0023 :param subparsers: subparsers object from global parser 0024 :return: merge request list subparser 0025 """ 0026 lister_parser: argparse.ArgumentParser = subparsers.add_parser( 0027 "mrs", aliases=["list"], help="List open merge requests" 0028 ) 0029 lister_parser.add_argument( 0030 "--project", 0031 help="Show merge requests of the current project, not of the user", 0032 action="store_true", 0033 ) 0034 lister_parser.add_argument( 0035 "--opened", 0036 help="Show opened merge requests", 0037 action="store_true", 0038 ) 0039 lister_parser.add_argument( 0040 "--merged", 0041 help="Show merged merge requests", 0042 action="store_true", 0043 ) 0044 lister_parser.add_argument( 0045 "--closed", 0046 help="Show closed merge requests", 0047 action="store_true", 0048 ) 0049 lister_parser.add_argument( 0050 "--url", 0051 help="Show web url of merge requests (default false)", 0052 action="store_true", 0053 ) 0054 return lister_parser 0055 0056 0057 def run(args: argparse.Namespace) -> None: 0058 """ 0059 run merge request list command 0060 :param args: parsed arguments 0061 """ 0062 lister = MergeRequestList(args.project, args.merged, args.opened, args.closed, args.url) 0063 lister.print_formatted_list() 0064 0065 0066 class MergeRequestList(RepositoryConnection): 0067 """ 0068 Lists all merge requests of the current repository 0069 """ 0070 0071 for_project: bool = False 0072 merged: bool = True 0073 opened: bool = True 0074 closed: bool = True 0075 show_url: bool = True 0076 0077 def __init__( # pylint: disable=too-many-arguments 0078 self, for_project: bool, merged: bool, opened: bool, closed: bool, show_url: bool 0079 ) -> None: 0080 RepositoryConnection.__init__(self) 0081 self.for_project = for_project 0082 self.show_url = show_url 0083 0084 if not merged and not opened and not closed: 0085 return 0086 0087 self.merged = merged 0088 self.opened = opened 0089 self.closed = closed 0090 0091 def print_formatted_list(self) -> None: # pylint: disable=too-many-branches 0092 """ 0093 prints the list of merge requests to the terminal formatted as a table 0094 """ 0095 merge_requests: List[ProjectMergeRequest] = [] 0096 0097 if self.for_project: 0098 base = self._remote_project 0099 else: 0100 base = self._connection 0101 0102 if self.merged and self.opened and self.closed: 0103 merge_requests = base.mergerequests.list() 0104 else: 0105 if self.merged: 0106 merge_requests += base.mergerequests.list(state="merged") 0107 if self.opened: 0108 merge_requests += base.mergerequests.list(state="opened") 0109 if self.closed: 0110 merge_requests += base.mergerequests.list(state="closed") 0111 0112 table = Table() 0113 0114 for merge_request in merge_requests: 0115 row: List[str] = [] 0116 0117 # Show merge request reference or url according to command line options 0118 if self.show_url: 0119 row.append(merge_request.web_url) 0120 else: 0121 row.append( 0122 TextFormatting.BOLD + merge_request.references["full"] + TextFormatting.END 0123 ) 0124 0125 row.append(merge_request.title) 0126 0127 if merge_request.state == "merged": 0128 row.append(TextFormatting.GREEN + merge_request.state + TextFormatting.END) 0129 elif merge_request.state == "opened": 0130 row.append(merge_request.state) 0131 elif merge_request.state == "closed": 0132 row.append(TextFormatting.RED + merge_request.state + TextFormatting.END) 0133 0134 table.add_row(row) 0135 0136 table.print()