|
| 1 | +import re |
| 2 | + |
| 3 | +import crosslink |
| 4 | +from crosslink.services import ( |
| 5 | + GitHubService, |
| 6 | + GitLabService, |
| 7 | + GravatarService, |
| 8 | + KeybaseService, |
| 9 | + LibravatarService, |
| 10 | + OpenHubService, |
| 11 | +) |
| 12 | +from crosslink.identifiers import ( |
| 13 | + Email, |
| 14 | + Identity, |
| 15 | + VerifiedAccount, |
| 16 | +) |
| 17 | + |
| 18 | +from errbot import BotPlugin, cmdfilter, re_botcmd |
| 19 | + |
| 20 | +_cache = {} |
| 21 | + |
| 22 | + |
| 23 | +class CrossLinkPlugin(BotPlugin): |
| 24 | + |
| 25 | + def __init__(self, bot, name=None): |
| 26 | + self._services = None |
| 27 | + super(CrossLinkPlugin, self).__init__(bot, name) |
| 28 | + |
| 29 | + def get_configuration_template(self): |
| 30 | + config = { |
| 31 | + 'CROSSLINK_GITHUB_TOKEN': '', |
| 32 | + 'CROSSLINK_GITLAB_TOKEN': '', |
| 33 | + 'CROSSLINK_OPENHUB_TOKEN': '', |
| 34 | + } |
| 35 | + |
| 36 | + for key in config: |
| 37 | + if hasattr(self.bot_config, key): |
| 38 | + config[key] = getattr(self.bot_config, key) |
| 39 | + |
| 40 | + return config |
| 41 | + |
| 42 | + def _get_config(self, key): |
| 43 | + if self.config and key in self.config: |
| 44 | + return self.config[key] |
| 45 | + return getattr(self.bot_config, key, None) |
| 46 | + |
| 47 | + @property |
| 48 | + def services(self): |
| 49 | + if not self._services: |
| 50 | + services = { |
| 51 | + 'gravatar': GravatarService(), |
| 52 | + 'libravatar': LibravatarService(), |
| 53 | + 'keybase': KeybaseService(), |
| 54 | + } |
| 55 | + token = self._get_config('CROSSLINK_GITHUB_TOKEN') |
| 56 | + if token and ':' in token: |
| 57 | + token = tuple(token.split(':', 1)) |
| 58 | + services['github'] = GitHubService(token=token) |
| 59 | + token = self._get_config('CROSSLINK_GITLAB_TOKEN') |
| 60 | + if token: |
| 61 | + services['gitlab'] = GitLabService(token=token) |
| 62 | + token = self._get_config('CROSSLINK_OPENHUB_TOKEN') |
| 63 | + if token: |
| 64 | + services['openhub'] = OpenHubService(token=token) |
| 65 | + |
| 66 | + self._services = services |
| 67 | + |
| 68 | + return self._services |
| 69 | + |
| 70 | + def _get_email_nick(self, email): |
| 71 | + """Tries to obtain a nickname for an email address. |
| 72 | +
|
| 73 | + Does not perform caching.""" |
| 74 | + nick = None |
| 75 | + self.log.debug('looking up nick for %s' % email) |
| 76 | + try: |
| 77 | + start = Email(email) |
| 78 | + except Exception as e: |
| 79 | + self.log.exception('crosslink failed: %s' % e) |
| 80 | + |
| 81 | + results = crosslink.resolve(start, self.services.values()) |
| 82 | + |
| 83 | + for item in results: |
| 84 | + for key in ['github', 'gitlab', 'openhub']: |
| 85 | + if key in self.services: |
| 86 | + service = self.services[key] |
| 87 | + if isinstance(item, service._identity_cls): |
| 88 | + self.log.debug( |
| 89 | + 'found %s username %s for %s' |
| 90 | + % (key, item.preferred_username, email)) |
| 91 | + nick = item.preferred_username |
| 92 | + break |
| 93 | + |
| 94 | + return nick |
| 95 | + |
| 96 | + @cmdfilter |
| 97 | + def add_nick(self, msg, cmd, args, dry_run): |
| 98 | + def get_user_nick(person_self): |
| 99 | + if hasattr(self, '_nick'): |
| 100 | + return person_self._nick |
| 101 | + else: |
| 102 | + self.log.warning('_nick was not set') |
| 103 | + |
| 104 | + if not msg.frm.nick: |
| 105 | + email = msg.frm.emails[0] |
| 106 | + |
| 107 | + if not _cache.get(email): |
| 108 | + _cache[email] = self._get_email_nick(email) |
| 109 | + |
| 110 | + msg.frm._nick = _cache[email] |
| 111 | + msg.frm.__class__.nick = property(get_user_nick) |
| 112 | + |
| 113 | + return msg, cmd, args |
| 114 | + |
| 115 | + @re_botcmd(pattern=r'^crosslink(?:\s+([\w@.\-\+]+))', |
| 116 | + re_cmd_name_help='crosslink', |
| 117 | + flags=re.IGNORECASE) |
| 118 | + def crosslink_cmd(self, msg, match): |
| 119 | + """Unassign from an issue.""" |
| 120 | + if len(match.groups()): |
| 121 | + email = match.group(1) |
| 122 | + else: |
| 123 | + email = msg.frm.emails[0] |
| 124 | + |
| 125 | + try: |
| 126 | + start = Email(email) |
| 127 | + except Exception as e: |
| 128 | + self.log.exception('crosslink failed: %s' % e) |
| 129 | + return 'Exception %r' % e |
| 130 | + |
| 131 | + results = crosslink.resolve(start, self.services.values()) |
| 132 | + |
| 133 | + identities = [] |
| 134 | + for item in results: |
| 135 | + if isinstance(item, Identity): |
| 136 | + identities.append(item) |
| 137 | + |
| 138 | + if not identities: |
| 139 | + return 'no identities found for `%s`!' % email |
| 140 | + |
| 141 | + output = ['Identities for `%s`:' % email] |
| 142 | + for identity in identities: |
| 143 | + output.append(str(identity)) |
| 144 | + arranged = [] |
| 145 | + for item in identity.get_fragments(): |
| 146 | + if isinstance(item, VerifiedAccount): |
| 147 | + arranged.insert(0, item) |
| 148 | + else: |
| 149 | + arranged.append(item) |
| 150 | + |
| 151 | + for item in arranged: |
| 152 | + if isinstance(item, VerifiedAccount): |
| 153 | + if item.verifier != identity.service: |
| 154 | + output.append(' + %s' % str(item)) |
| 155 | + else: |
| 156 | + output.append(' + %s' % str(item.account)) |
| 157 | + else: |
| 158 | + output.append(' ' + str(item)) |
| 159 | + |
| 160 | + return '\n'.join(output) |
0 commit comments