class Channel(EndpointScope): Endpoint = IRCendpoint() Parents = [Server] primary_keys = ['server', 'channel'] def __init__(self, server: Server, name: str): self._server = server self._name = name @property def server(self): return self._server @property def name(self): return self._name @classmethod def init_by_keys(cls, **query): return cls(Server.init_by_keys(**query), query.get('channel')) @cached_property def query(self) -> dict: return {'server': self._server.url, 'channel': self._name} @cached_property def parent(self): return self.Parents[0](self._server)
def run(self): """Running the task""" IRCendpoint().client.msg( '##bot-testing', f'PR#{self.statistics.my_pr_stats.number} has more than {self.PR_MAX_NUMBER_OF_COMMENTS} comments! ' f'({self.statistics.my_pr_stats.total_number_of_comments} comments)' )
class IRCAnswerQuestion(ConditionalTask): """This task answer once someone send message to the bot in IRC.""" Endpoint = IRCendpoint() # The third party Endpoint for this task is IRC. EndpointScope = Message # The scope of this task is pull request. NAME = 'IRCAnswerQuestion' # The name of the task. RUN_ONCE = False # Indicate that the task will always run. not only in the first occurrence. @property def condition(self): return self.event and isinstance(self.event, MessageMentionedMeEvent) def run(self): me = self.Endpoint.client.nick content, sender, channel = self.scope.content, self.scope.sender, self.scope.channel def answer(content): return self.Endpoint.client.msg(channel.name, f'{sender}, {content}') if f'{me}, ping' == content: answer('pong') elif f'{me}, #pr' == content: answer(', '.join([ f'{repo.name}: {repo.number_of_open_issues}' for repo in self.all_statistics.github_repository ])) else: answer(f'Unknown option "{content}"') self.Endpoint.client.msg(channel.name, 'options:') self.Endpoint.client.msg(channel.name, ' ping - Get pong back.') self.Endpoint.client.msg( channel.name, ' #pr - Number of open pull requests per repository.')
def run(self): mentioned, actor = self.event.artifacts[ 'comment'].mentioned_users, self.event.artifacts['actor'] # Getting IRC nick in case that the actor in the users list user = User.get_user(github_login=actor.login) actor = (user.irc_nick if user else actor.login) for mentioned_user in mentioned: # Getting IRC nick in case that the user in the users list user = User.get_user(github_login=mentioned_user.login) mentioned_user = (user.irc_nick if user else mentioned_user.login) # Composing the comment from the statistics, mentioned users and actor IRCendpoint().client.msg( '##bot-testing', f'{mentioned_user}, {actor} has ' f'mentioned you in {self.statistics.my_repo_statistics.organization}/' f'{self.statistics.my_repo_statistics.repository} ' f'@ PR#{self.statistics.my_pulls_statistics.issue_number}.')
class Server(EndpointScope): Endpoint = IRCendpoint() primary_keys = ['server'] key = 'irc_server' def __init__(self, url): self._url = url @property def url(self): return self._url @classmethod def init_by_keys(cls, **kwargs): return cls(kwargs.get('server')) @cached_property def query(self) -> dict: return {'server': self._url}
class IRCeventsFactory(EventsFactory): Endpoint = IRCendpoint() _check_for_new_events_interval = 1 def build_events(self) -> list: events = [] lines = self.Endpoint.client.read_lines() if lines: now = datetime.now() for line in lines: records = self.Endpoint.client.parse_messages(line) if records: for sender, channel, content in records: if self.Endpoint.client.nick in content: events.append( MessageMentionedMeEvent( self.Endpoint.client.server, sender, channel, content, now)) else: events.append( MessageEvent(self.Endpoint.client.server, sender, channel, content, now)) return events
def run(self): actor = self.event.data['sender']['login'] number = self.event.data['payload']['pull_request']['number'] IRCendpoint().client.msg('##bot-testing', f'{actor} has merged PR#{number}')
class IRCevent(Event): Endpoint = IRCendpoint()
class IRCbot(BotSlave): Endpoint = IRCendpoint() EventsFactory = IRCeventsFactory() handle_events_every = 1