def __init__(self, config_handler: ConfigHandler, repo: Repo, default_issue: Optional[IssueDefault], options: Options): self.__config_handler: ConfigHandler = config_handler self.__repo: Repo = repo self.__github = Github(self.__config_handler).with_repo(self.__repo) self.__default_issue: Optional[IssueDefault] = default_issue self.__options: Options = options
def test_get_user(self): r: Response = Github(self.config_handler).get_user() self.assertIs(r.status_code, 200) print(r.json()) falsy_config_handler = ConfigHandler(CONFIG_DIR) falsy_config_handler.config = Config().with_github( ConfigGithub(activate=True, user='******', token='dudu')) r: Response = Github(falsy_config_handler).get_user() self.assertIsNot(r.status_code, 200)
def read_issue_by_number(self, number: int) -> Issue: repo: Repo = GitCmd(self.state_handler, self.options.debug).get_repo() # repo: Repo = Repo(owner='flexiooss', repo='flexio-flow-punching-ball') resp: Response = Github( self.config_handler).with_repo(repo).read_issue( IssueGithub().with_number(number)) return IssueGithub.from_api_dict(resp.json())
def comment(self, issue: IssueGithub, text: str) -> Issue: repo: Repo = GitCmd(self.state_handler, self.options.debug).with_config_handler( self.config_handler).get_repo() resp: Response = Github( self.config_handler).with_repo(repo).create_comment(issue=issue, body=text) return IssueGithub.from_api_dict(resp.json())
class Create: def __init__(self, config_handler: ConfigHandler, repo: Repo, default_issue: Optional[IssueDefault], options: Options): self.__config_handler: ConfigHandler = config_handler self.__repo: Repo = repo self.__github = Github(self.__config_handler).with_repo(self.__repo) self.__default_issue: Optional[IssueDefault] = default_issue self.__options: Options = options def __start_message(self) -> Create: if not self.__options.default: CommonIssue.issuer_message() return self def __start_message_issue(self) -> Create: if not self.__options.default: print("""############################################### ######### {yellow}Create Github Issue{reset} ######### """.format(yellow=Fg.NOTICE.value, reset=Fg.RESET.value)) return self def __sanitize_list_input(self, v: List[str]) -> List[str]: return list(filter(lambda x: len(x) > 0, map(lambda x: x.strip(), v))) def __input_assignees(self, issue: IssueGithub) -> Create: if self.__options.default: assignees: List[str] = self.__default_issue.assignees else: message: str = """ Assignees {default} {bg_help}separator `;`{reset_bg} {bg_help}`-l` to list users{reset_bg} """.format(default=Fg.SUCCESS.value + ';'.join(self.__default_issue.assignees) + Fg.RESET.value + ' :' if len(self.__default_issue.assignees) else '', reset_bg=Fg.RESET.value, bg_help=Fg.NOTICE.value) assignees: str = input(message) assignees = assignees if assignees else self.__config_handler.config.github.user if assignees == '-l': r: Response = self.__github.get_users() members: List[str] = [] if r.status_code == 200: members_response: List[Dict[str, str]] = r.json() l: Dict[str, str] for l in members_response: members.append( '{login!s}'.format(login=l.get('login'))) if len(members): message: str = """{fg_cyan}{members!s} {reset_fg} Choose pseudo : """.format(fg_cyan=Fg.NOTICE.value, members=' | '.join(members), reset_fg=Fg.RESET.value) else: message: str = Fg.FAIL.value + 'No member, type `abort`' + Fg.RESET.value assignees: str = input(message) assignees: List[str] = assignees.split(';') assignees = self.__sanitize_list_input(assignees) if len(assignees): issue.assignees = assignees return self def __create_milestone(self) -> Milestone: milestone: Milestone = Milestone() title: str = '' while not len(title) > 0: title = input(Fg.FAIL.value + '[required]' + Fg.RESET.value + ' Title : ') milestone.title = title description: str = input('Description : ') if description: milestone.description = description return milestone def __resume_milestone(self, milestone: Dict[str, str]) -> Create: print("""############################################### ################ {green}Milestone created{reset} ################ ###############################################{green} title : {title!s} number : {number!s} url : {url!s} {reset}############################################### """.format(green=Fg.NOTICE.value, title=milestone.get('title'), number=milestone.get('number'), url=milestone.get('html_url'), reset=Fg.RESET.value)) return self def __input_milestone(self, issue: IssueGithub) -> Create: milestone: str = input("""Milestone number : {bg_help}`-l` to list the existing `-c` to create milestone{reset_bg}""".format(reset_bg=Fg.RESET.value, bg_help=Fg.NOTICE.value)) if milestone == '-c': r1: Response = self.__github.get_open_milestones() milestones_repo: List[str] = [] if r1.status_code == 200: milestones_response: List[Dict[str, str]] = r1.json() l: Dict[str, str] for l in milestones_response: milestones_repo.append('{number!s} : {title!s}'.format( number=l.get('number'), title=l.get('title'))) if len(milestones_repo): message: str = """{fg_cyan}{milestones!s}{fg_reset} Choose number : """.format(fg_cyan=Fg.NOTICE.value, milestones=' | '.join(milestones_repo), fg_reset=Fg.RESET.value) else: message: str = Fg.FAIL.value + 'No milestone, type `-c` to create milestone or `-a` for abort' + Fg.RESET.value milestone: str = input(message) if milestone == '-c': milestone_inst: Milestone = self.__create_milestone() r2: Response = self.__github.create_milestone(milestone_inst) if r2.status_code == 201: milestone_created: Dict[str, str] = r2.json() milestone = milestone_created.get('number') self.__resume_milestone(milestone_created) milestone = milestone if not milestone == '-a' else '' if milestone: issue.milestone = int(milestone) return self def __input_labels(self, issue: IssueGithub) -> Create: labels_lst: List[str] if self.__options.default: labels_lst = self.__default_issue.labels else: message: str = 'Labels ' r: Response = self.__github.get_labels() labels_repo: List[str] = [] if r.status_code == 200: labels_response: List[Dict[str, str]] = r.json() l: Dict[str, str] for l in labels_response: labels_repo.append(l.get('name')) default: str = ';'.join(self.__default_issue.labels) if len( self.__default_issue.assignees) else '' if len(labels_repo): message += """ {fg_cyan}{labels!s}{fg_reset} Choose label : {fg_green}{default}{fg_reset} {fg_cyan}separator `;` {fg_reset} """.format(fg_cyan=Fg.NOTICE.value, labels=' | '.join(labels_repo), fg_reset=Fg.RESET.value, fg_green=Fg.SUCCESS.value, default=default) labels: str = input(message) labels = labels if len(labels) > 0 else default labels_lst = labels.split(';') labels_lst = self.__sanitize_list_input(labels_lst) if len(labels_lst): issue.labels = labels_lst return self def __input_issue(self): issue: IssueGithub = IssueGithub() title: str = '' title_default: str = Fg.SUCCESS.value + self.__default_issue.title + Fg.RESET.value if self.__default_issue.title is not None else '' if self.__options.default and self.__default_issue.title is not None: title = self.__default_issue.title else: while not len(title) > 0: title = input(Fg.FAIL.value + '[required]' + Fg.RESET.value + ' Issue Title : ' + title_default) title = title if title else self.__default_issue.title issue.title = title if not self.__options.default: body: str = input('Description : ') if body: issue.body = body self.__input_assignees(issue).__input_labels(issue) return issue def __post_issue(self, issue: IssueGithub) -> Response: Log.info('waiting... Github create issue') return self.__github.create_issue(issue) def __resume_issue(self, issue: IssueGithub) -> Create: CommonIssue.print_resume_issue(issue) return self def __resume_issue_created(self, issue: IssueGithub) -> Create: CommonIssue.print_resume_issue(issue) return self def process(self) -> Issue: # self.__start_message() self.__start_message_issue() issue: IssueGithub = self.__input_issue() r: Response = self.__post_issue(issue) if r.status_code == 201: issue_created: IssueGithub = IssueGithub.from_api_dict(r.json()) self.__resume_issue_created(issue_created) else: raise GithubRequestApiError(r) return issue_created
def setup_github_repo(cls, config_handler: ConfigHandler) -> Github: return Github(config_handler).with_repo( Repo(owner='flexiooss', repo='flexio-flow-punching-ball'))
def __check_user(self): r: Response = Github(self.config_handler).get_user() if r.status_code != 200: raise ConnectionAbortedError('Bad api github token : retry')
def setUp(self): self.config_handler = ConfigHandler(CONFIG_DIR) self.config_handler.config = Config().with_github( ConfigGithub(activate=True, user=USER, token=TOKEN_TEST)) self.github_repo: Github = Github(self.config_handler).with_repo( Repo(owner='flexiooss', repo='flexio-flow-punching-ball'))
class AttachOrCreate: def __init__(self, config_handler: ConfigHandler, repo: Repo, default_issue: Optional[IssueDefault], options: Options): self.__config_handler: ConfigHandler = config_handler self.__repo: Repo = repo self.__github = Github(self.__config_handler).with_repo(self.__repo) self.__default_issue: Optional[IssueDefault] = default_issue self.__options: Options = options self.__issue: Optional[Issue] = None def __would_attach_issue(self) -> bool: if self.__options.default: return False else: issue: str = input( """Have already an issue y/{green}n{reset_fg} : """.format( green=Fg.SUCCESS.value, reset_fg=Fg.RESET.value, )) issue = issue if issue else 'n' return issue == 'y' def __number_issue(self) -> int: issue: str = input('Issue number : ') return int(issue) def __read_issue(self, issue: IssueGithub) -> Response: return self.__github.read_issue(issue) def __attach(self) -> bool: if self.__would_attach_issue(): issue_number = self.__number_issue() issue: IssueGithub = IssueGithub().with_number(issue_number) try: Log.info('waiting... from Github... Issue : ' + str(issue_number)) r: Response = self.__read_issue(issue) self.__issue: IssueGithub = IssueGithub.from_api_dict(r.json()) CommonIssue.print_resume_issue(self.__issue) except FileNotFoundError: Log.error(Fg.FAIL.value + 'Issue not found : retry' + Fg.RESET.value) return self.process() return True else: return False def __create(self) -> Issue: return Create(config_handler=self.__config_handler, repo=self.__repo, default_issue=self.__default_issue, options=self.__options).process() def process(self) -> Issue: if not self.__options.default: CommonIssue.issuer_message() if self.__attach(): return self.__issue else: return self.__create()