def subscribe(self): self.payload = parse.parse_qs(self.payload) github_repo = self.payload.get('text').pop() github_repo = github_repo.rstrip('>').lstrip('<') user_slack_id = self.payload.get('user_id').pop() user = UserRepository.get_user_by_slack_id(user_slack_id) msg = { 'text': '', } if not user: return attach_message( msg, 'You must first register via /signin %your_github_username%') repo = sa.select([repository_t.c.repo_url]).where( sa.and_(repository_t.c.subscribed_user_id == user.id, repository_t.c.repo_url == github_repo.lower())).execute().fetchone() if repo: return attach_message( msg, f'You are already subscribed to this repository {github_repo}') log.info('Subscribing %s to %s', self.payload.get('user_name', []).pop(), github_repo) repository_t.insert().values({ 'subscribed_user_id': user.id, 'repo_url': github_repo }).execute() return attach_message(msg, f'Successfully subscribed to {github_repo}', color='#47a450')
def unsubscribe(self): self.payload = parse.parse_qs(self.payload) github_repo = self.payload.get('text').pop() github_repo = github_repo.rstrip('>').lstrip('<') user_slack_id = self.payload.get('user_id').pop() msg = {'text': ''} user = UserRepository.get_user_by_slack_id(user_slack_id) if not user: return attach_message(msg, 'You are not registered here') repo = sa.select([repository_t]).where( sa.and_(repository_t.c.subscribed_user_id == user.id, repository_t.c.repo_url.like( github_repo))).execute().fetchone() if not repo: return attach_message( msg, f'You are not subscribed to this repository {github_repo}') log.info('Unsubscribe %s to %s', self.payload.get('user_name', []).pop(), github_repo) repository_t.delete().where( sa.and_(repository_t.c.subscribed_user_id == user.id, repository_t.c.repo_url == repo.repo_url)).execute() return attach_message(msg, f'Successfully unsubscribed from {github_repo}', color='#47a450')
def signin(self): """ :return: response text (either error or an successful answer) """ self.payload = parse.parse_qs(self.payload) user_id = self.payload.get('user_id') msg = {'text': ''} if not user_id: err = 'User id is not present in payload @ signin action' log.error(err) return attach_message(msg, err) github_username = self.payload.get('text') if isinstance(github_username, list) and len(github_username) == 1: github_username, = github_username slack_username = self.payload.get('user_name').pop() log.info('Verifying user %s and it\'s github username %s', slack_username, github_username) url = f'https://api.github.com/users/{github_username}' res = requests.get(url) if res.status_code == 404: # todo back in slack with error log.error('Github user %s not found.', github_username) return attach_message( msg, f'No such github user: {github_username}.') user_slack_id = self.payload.get('user_id') if isinstance(user_slack_id, list) and len(user_slack_id) == 1: user_slack_id, = user_slack_id else: log.error('Error parsing slack user id with payload: %s', self.payload) return attach_message(msg, f'Error parsing your slack user id -_-') user = UserRepository.get_user_by_slack_id(user_slack_id) if user: log.warning( 'User %s is already registered in database with github username %s', user.slack_id, github_username) return attach_message( msg, f'You have been already registered with this github username: {github_username}' ) log.info('Registering new user in our database') UserRepository.create_new_user( **{ 'github_username': github_username.lower(), 'slack_username': slack_username, 'slack_id': user_slack_id }) # User.insert().values( # { # 'github_username': github_username.lower(), # normalize github username... # 'slack_username': slack_username, # 'slack_id': user_slack_id # } # ).execute() log.info('Successfully registered %s as %s', slack_username, github_username) return attach_message( msg, f'Successfully registered you as {github_username}.', color='#47a450') else: log.error('Error parsing github username') return attach_message(msg, f'error parsing {self.payload.get("text")}')