Ejemplo n.º 1
0
class ServiceGithub(ServicesMgr):
    """
        Service Github
    """
    def __init__(self, token=None, **kwargs):
        super(ServiceGithub, self).__init__(token, **kwargs)
        self.scope = ['public_repo']
        self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
        self.AUTH_URL = 'https://github.com/login/oauth/authorize'
        self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
        self.username = settings.TH_GITHUB_KEY['username']
        self.password = settings.TH_GITHUB_KEY['password']
        self.consumer_key = settings.TH_GITHUB_KEY['consumer_key']
        self.consumer_secret = settings.TH_GITHUB_KEY['consumer_secret']
        self.token = token
        self.oauth = 'oauth1'
        self.service = 'ServiceGithub'
        if self.token:
            token_key, token_secret = self.token.split('#TH#')
            self.gh = GitHub(token=token_key)
        else:
            self.gh = GitHub(username=self.username, password=self.password)

    def gh_footer(self, trigger, issue):

        link = 'https://github.com/{0}/{1}/issues/{2}'.format(
            trigger.repo, trigger.project, issue.id)

        provided_by = _('Provided by')
        provided_from = _('from')
        footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>"

        return footer_from.format(provided_by, trigger.trigger.description,
                                  provided_from, link, link)

    def read_data(self, **kwargs):
        """
            get the data from the service
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
            :rtype: list
        """
        trigger_id = kwargs.get('trigger_id')
        date_triggered = str(kwargs.get('date_triggered')).replace(' ', 'T')
        data = list()
        if self.token:
            # check if it remains more than 1 access
            # then we can create an issue
            if self.gh.ratelimit_remaining > 1:

                import pypandoc

                trigger = Github.objects.get(trigger_id=trigger_id)
                issues = self.gh.issues_on(trigger.repo,
                                           trigger.project,
                                           since=date_triggered)

                for issue in issues:

                    content = pypandoc.convert(issue.body, 'md', format='html')
                    content += self.gh_footer(trigger, issue)

                    data.append({'title': issue.title, 'content': content})
                    # digester
                    self.send_digest_event(trigger_id, issue.title, '')
                cache.set('th_github_' + str(trigger_id), data)
            else:
                # rate limit reach, do nothing right now
                logger.warning("Rate limit reached")
                update_result(trigger_id,
                              msg="Rate limit reached",
                              status=True)
        else:
            logger.critical("no token provided")
            update_result(trigger_id, msg="No token provided", status=True)
        return data

    def save_data(self, trigger_id, **data):
        """
            let's save the data
            :param trigger_id: trigger ID from which to save data
            :param data: the data to check to be used and save
            :type trigger_id: int
            :type data:  dict
            :return: the status of the save statement
            :rtype: boolean
        """
        if self.token:
            title = self.set_title(data)
            body = self.set_content(data)
            # get the details of this trigger
            trigger = Github.objects.get(trigger_id=trigger_id)

            # check if it remains more than 1 access
            # then we can create an issue
            limit = self.gh.ratelimit_remaining
            if limit > 1:
                # repo goes to "owner"
                # project goes to "repository"
                r = self.gh.create_issue(trigger.repo, trigger.project, title,
                                         body)
            else:
                # rate limit reach
                logger.warn("Rate limit reached")
                update_result(trigger_id,
                              msg="Rate limit reached",
                              status=True)
                # put again in cache the data that could not be
                # published in Github yet
                cache.set('th_github_' + str(trigger_id), data, version=2)
                return True
            sentence = str('github {} created').format(r)
            logger.debug(sentence)
            status = True
        else:
            sentence = "no token or link provided for " \
                       "trigger ID {} ".format(trigger_id)
            logger.critical(sentence)
            update_result(trigger_id, msg=sentence, status=False)
            status = False

        return status

    def auth(self, request):
        """
            let's auth the user to the Service
            :param request: request object
            :return: callback url
            :rtype: string that contains the url to redirect after auth
        """
        auth = self.gh.authorize(self.username, self.password, self.scope, '',
                                 '', self.consumer_key, self.consumer_secret)
        request.session['oauth_token'] = auth.token
        request.session['oauth_id'] = auth.id
        return self.callback_url(request)

    def callback(self, request, **kwargs):
        """
            Called from the Service when the user accept to activate it
            :param request: request object
            :return: callback url
            :rtype: string , path to the template
        """
        access_token = request.session['oauth_token'] + "#TH#"
        access_token += str(request.session['oauth_id'])
        kwargs = {'access_token': access_token}
        return super(ServiceGithub, self).callback(request, **kwargs)
Ejemplo n.º 2
0
class Command(object):
    __metaclass__ = ABCMeta
    name = None
    usage = None
    repository = ()
    user = ''
    subcommands = {}
    SUCCESS = 0
    FAILURE = 1
    COMMAND_UNKNOWN = 127

    def __init__(self):
        super(Command, self).__init__()
        assert self.name
        commands[self.name] = self
        self.gh = GitHub()
        self.gh.set_user_agent('github-cli/{0} (http://git.io/MEmEmw)'.format(
            __version__
        ))
        self.parser = CustomOptionParser(usage=self.usage)

    @abstractmethod
    def run(self, options, args):
        return self.FAILURE

    def get_repo(self, options):
        self.repo = None
        if self.repository:
            self.repo = self.gh.repository(*self.repository)

        if not (self.repo or options.loc_aware):
            self.parser.error('A repository is required.')

    def get_user(self):
        if not self.user:
            self.login()
            self.user = self.gh.user()

    def login(self):
        # Get the full path to the configuration file
        config = github_config()
        parser = ConfigParser()

        # Check to make sure the file exists and we are allowed to read it
        if os.path.isfile(config) and os.access(config, os.R_OK | os.W_OK):
            parser.readfp(open(config))
            self.gh.login(token=parser.get('github', 'token'))
        else:
        # Either the file didn't exist or we didn't have the correct
        # permissions
            user = ''
            while not user:
                # We will not stop until we are given a username
                user = input('Username: '******''
            while not pw:
                # Nor will we stop until we're given a password
                pw = getpass('Password: '******'user', 'repo', 'gist'], 'github-cli',
                'http://git.io/MEmEmw'
            )
            parser.add_section('github')
            parser.set('github', 'token', auth.token)
            self.gh.login(token=auth.token)
            # Create the file if it doesn't exist. Otherwise completely blank
            # out what was there before. Kind of dangerous and destructive but
            # somewhat necessary
            parser.write(open(config, 'w+'))

    def help(self):
        self.parser.print_help()
        if self.subcommands:
            print('\nSubcommands:')
            for command in sorted(self.subcommands.keys()):
                print('  {0}:\n\t{1}'.format(
                    command, self.subcommands[command]
                ))
        sys.exit(0)
Ejemplo n.º 3
0
class ServiceGithub(ServicesMgr):

    def __init__(self, token=None):
        self.scope = ['public_repo']
        self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
        self.AUTH_URL = 'https://github.com/login/oauth/authorize'
        self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
        self.username = settings.TH_GITHUB['username']
        self.password = settings.TH_GITHUB['password']
        self.consumer_key = settings.TH_GITHUB['consumer_key']
        self.consumer_secret = settings.TH_GITHUB['consumer_secret']
        self.token = token
        if self.token:
            token_key, token_secret = self.token.split('#TH#')
            self.gh = GitHub(token=token_key)
        else:
            self.gh = GitHub(username=self.username, password=self.password)

    def read_data(self, **kwargs):
        """
            get the data from the service

            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict

            :rtype: list
        """
        # date_triggered = kwargs['date_triggered']
        trigger_id = kwargs['trigger_id']
        data = list()
        cache.set('th_github_' + str(trigger_id), data)

    def process_data(self, **kwargs):
        """
            get the data from the cache
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
        """
        kw = {'cache_stack': 'th_github', 'trigger_id': str(kwargs['trigger_id'])}
        return super(ServiceGithub, self).process_data(**kw)

    def save_data(self, trigger_id, **data):
        """
            let's save the data

            :param trigger_id: trigger ID from which to save data
            :param data: the data to check to be used and save
            :type trigger_id: int
            :type data:  dict
            :return: the status of the save statement
            :rtype: boolean
        """
        from th_github.models import Github
        status = False

        if self.token:
            title = self.set_title(data)
            body = self.set_content(data)
            # get the details of this trigger
            trigger = Github.objects.get(trigger_id=trigger_id)

            # check if it remains more than 1 access
            # then we can create an issue
            limit = self.gh.ratelimit_remaining
            if limit > 1:
                # repo goes to "owner"
                # project goes to "repository"
                r = self.gh.create_issue(trigger.repo,
                                         trigger.project,
                                         title,
                                         body)
            else:
                # rate limite reach
                logger.warn("Rate limit reached")
                # put again in cache the data that could not be
                # published in Github yet
                cache.set('th_github_' + str(trigger_id), data, version=2)
                return True
            sentance = str('github {} created').format(r)
            logger.debug(sentance)
            status = True
        else:
            logger.critical(
                "no token or link provided for trigger ID {} ".format(trigger_id))
            status = False

        return status

    def auth(self, request):
        """
            let's auth the user to the Service
        """
        callback_url = 'http://%s%s' % (
            request.get_host(), reverse('github_callback'))
        auth = self.gh.authorize(self.username,
                                 self.password,
                                 self.scope,
                                 '',
                                 '',
                                 self.consumer_key,
                                 self.consumer_secret)
        request.session['oauth_token'] = auth.token
        request.session['oauth_id'] = auth.id
        return callback_url

    def callback(self, request):
        """
            Called from the Service when the user accept to activate it
        """

        try:
            # finally we save the user auth token
            # As we already stored the object ServicesActivated
            # from the UserServiceCreateView now we update the same
            # object to the database so :
            # 1) we get the previous objet
            us = UserService.objects.get(
                user=request.user,
                name=ServicesActivated.objects.get(name='ServiceGithub'))
            # 2) Readability API require to use 4 parms consumer_key/secret +
            # token_key/secret instead of usually get just the token
            # from an access_token request. So we need to add a string
            # seperator for later use to slpit on this one
            access_token = request.session['oauth_token'] + "#TH#" + str(request.session['oauth_id'])
            us.token = access_token
            # 3) and save everything
            us.save()
        except KeyError:
            return '/'

        return 'github/callback.html'
Ejemplo n.º 4
0
class ServiceGithub(ServicesMgr):

    def __init__(self, token=None, **kwargs):
        super(ServiceGithub, self).__init__(token, **kwargs)
        self.scope = ['public_repo']
        self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
        self.AUTH_URL = 'https://github.com/login/oauth/authorize'
        self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
        self.username = settings.TH_GITHUB['username']
        self.password = settings.TH_GITHUB['password']
        self.consumer_key = settings.TH_GITHUB['consumer_key']
        self.consumer_secret = settings.TH_GITHUB['consumer_secret']
        self.token = token
        self.oauth = 'oauth1'
        self.service = 'ServiceGithub'
        if self.token:
            token_key, token_secret = self.token.split('#TH#')
            self.gh = GitHub(token=token_key)
        else:
            self.gh = GitHub(username=self.username, password=self.password)

    def gh_footer(self, trigger, issue):

        link = 'https://github.com/{0}/{1}/issues/{2}'.format(
            trigger.repo, trigger.project, issue.id)

        provided_by = _('Provided by')
        provided_from = _('from')
        footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>"

        return footer_from.format(provided_by, trigger.trigger.description,
                                  provided_from, link, link)

    def read_data(self, **kwargs):
        """
            get the data from the service
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
            :rtype: list
        """
        trigger_id = kwargs.get('trigger_id')
        date_triggered = str(kwargs.get('date_triggered')).replace(' ', 'T')
        data = list()
        if self.token:
            # check if it remains more than 1 access
            # then we can create an issue
            if self.gh.ratelimit_remaining > 1:

                import pypandoc

                trigger = Github.objects.get(trigger_id=trigger_id)
                issues = self.gh.issues_on(trigger.repo,
                                           trigger.project,
                                           since=date_triggered)

                for issue in issues:

                    content = pypandoc.convert(issue.body, 'md', format='html')
                    content += self.gh_footer(trigger, issue)

                    data.append({'title': issue.title, 'content': content})

                cache.set('th_github_' + str(trigger_id), data)
            else:
                # rate limit reach, do nothing right now
                logger.warn("Rate limit reached")
        else:
            logger.critical("no token provided")
        return data

    def save_data(self, trigger_id, **data):
        """
            let's save the data
            :param trigger_id: trigger ID from which to save data
            :param data: the data to check to be used and save
            :type trigger_id: int
            :type data:  dict
            :return: the status of the save statement
            :rtype: boolean
        """
        if self.token:
            title = self.set_title(data)
            body = self.set_content(data)
            # get the details of this trigger
            trigger = Github.objects.get(trigger_id=trigger_id)

            # check if it remains more than 1 access
            # then we can create an issue
            limit = self.gh.ratelimit_remaining
            if limit > 1:
                # repo goes to "owner"
                # project goes to "repository"
                r = self.gh.create_issue(trigger.repo,
                                         trigger.project,
                                         title,
                                         body)
            else:
                # rate limit reach
                logger.warn("Rate limit reached")
                # put again in cache the data that could not be
                # published in Github yet
                cache.set('th_github_' + str(trigger_id), data, version=2)
                return True
            sentence = str('github {} created').format(r)
            logger.debug(sentence)
            status = True
        else:
            sentence = "no token or link provided for trigger ID {} "
            logger.critical(sentence.format(trigger_id))
            status = False

        return status

    def auth(self, request):
        """
            let's auth the user to the Service
            :param request: request object
            :return: callback url
            :rtype: string that contains the url to redirect after auth
        """
        auth = self.gh.authorize(self.username,
                                 self.password,
                                 self.scope,
                                 '',
                                 '',
                                 self.consumer_key,
                                 self.consumer_secret)
        request.session['oauth_token'] = auth.token
        request.session['oauth_id'] = auth.id
        return self.callback_url(request)

    def callback(self, request, **kwargs):
        """
            Called from the Service when the user accept to activate it
            :param request: request object
            :return: callback url
            :rtype: string , path to the template
        """
        access_token = request.session['oauth_token'] + "#TH#"
        access_token += str(request.session['oauth_id'])
        kwargs = {'access_token': access_token}
        return super(ServiceGithub, self).callback(request, **kwargs)
Ejemplo n.º 5
0
class ServiceGithub(ServicesMgr):
    def __init__(self, token=None):
        super(ServiceGithub, self).__init__(token)
        self.scope = ['public_repo']
        self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
        self.AUTH_URL = 'https://github.com/login/oauth/authorize'
        self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
        self.username = settings.TH_GITHUB['username']
        self.password = settings.TH_GITHUB['password']
        self.consumer_key = settings.TH_GITHUB['consumer_key']
        self.consumer_secret = settings.TH_GITHUB['consumer_secret']
        self.token = token
        if self.token:
            token_key, token_secret = self.token.split('#TH#')
            self.gh = GitHub(token=token_key)
        else:
            self.gh = GitHub(username=self.username, password=self.password)

    def read_data(self, **kwargs):
        """
            get the data from the service
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
            :rtype: list
        """
        trigger_id = kwargs['trigger_id']
        data = list()
        cache.set('th_github_' + str(trigger_id), data)

    def process_data(self, **kwargs):
        """
            get the data from the cache
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
        """
        kw = {
            'cache_stack': 'th_github',
            'trigger_id': str(kwargs['trigger_id'])
        }
        return super(ServiceGithub, self).process_data(**kw)

    def save_data(self, trigger_id, **data):
        """
            let's save the data
            :param trigger_id: trigger ID from which to save data
            :param data: the data to check to be used and save
            :type trigger_id: int
            :type data:  dict
            :return: the status of the save statement
            :rtype: boolean
        """
        from th_github.models import Github
        if self.token:
            title = self.set_title(data)
            body = self.set_content(data)
            # get the details of this trigger
            trigger = Github.objects.get(trigger_id=trigger_id)

            # check if it remains more than 1 access
            # then we can create an issue
            limit = self.gh.ratelimit_remaining
            if limit > 1:
                # repo goes to "owner"
                # project goes to "repository"
                r = self.gh.create_issue(trigger.repo, trigger.project, title,
                                         body)
            else:
                # rate limit reach
                logger.warn("Rate limit reached")
                # put again in cache the data that could not be
                # published in Github yet
                cache.set('th_github_' + str(trigger_id), data, version=2)
                return True
            sentence = str('github {} created').format(r)
            logger.debug(sentence)
            status = True
        else:
            sentence = "no token or link provided for trigger ID {} "
            logger.critical(sentence.format(trigger_id))
            status = False

        return status

    def auth(self, request):
        """
            let's auth the user to the Service
            :param request: request object
            :return: callback url
            :rtype: string that contains the url to redirect after auth
        """
        callback_url = 'http://%s%s' % (request.get_host(),
                                        reverse('github_callback'))
        auth = self.gh.authorize(self.username, self.password, self.scope, '',
                                 '', self.consumer_key, self.consumer_secret)
        request.session['oauth_token'] = auth.token
        request.session['oauth_id'] = auth.id
        return callback_url

    def callback(self, request, **kwargs):
        """
            Called from the Service when the user accept to activate it
            :param request: request object
            :return: callback url
            :rtype: string , path to the template
        """
        try:
            # finally we save the user auth token
            # As we already stored the object ServicesActivated
            # from the UserServiceCreateView now we update the same
            # object to the database so :
            # 1) we get the previous objet
            us = UserService.objects.get(
                user=request.user,
                name=ServicesActivated.objects.get(name='ServiceGithub'))
            # 2) Readability API require to use 4 params consumer_key/secret +
            # token_key/secret instead of usually get just the token
            # from an access_token request. So we need to add a string
            # separator for later use to split on this one
            access_token = request.session['oauth_token'] + "#TH#"
            access_token += str(request.session['oauth_id'])
            us.token = access_token
            # 3) and save everything
            us.save()
        except KeyError:
            return '/'

        return 'github/callback.html'
Ejemplo n.º 6
0
class ServiceGithub(ServicesMgr):

    def __init__(self, token=None, **kwargs):
        super(ServiceGithub, self).__init__(token, **kwargs)
        self.scope = ['public_repo']
        self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
        self.AUTH_URL = 'https://github.com/login/oauth/authorize'
        self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
        self.username = settings.TH_GITHUB['username']
        self.password = settings.TH_GITHUB['password']
        self.consumer_key = settings.TH_GITHUB['consumer_key']
        self.consumer_secret = settings.TH_GITHUB['consumer_secret']
        self.token = token
        self.oauth = 'oauth1'
        self.service = 'ServiceGithub'
        if self.token:
            token_key, token_secret = self.token.split('#TH#')
            self.gh = GitHub(token=token_key)
        else:
            self.gh = GitHub(username=self.username, password=self.password)

    def read_data(self, **kwargs):
        """
            get the data from the service
            :param kwargs: contain keyword args : trigger_id at least
            :type kwargs: dict
            :rtype: list
        """
        trigger_id = kwargs.get('trigger_id')
        data = list()
        cache.set('th_github_' + str(trigger_id), data)

    def save_data(self, trigger_id, **data):
        """
            let's save the data
            :param trigger_id: trigger ID from which to save data
            :param data: the data to check to be used and save
            :type trigger_id: int
            :type data:  dict
            :return: the status of the save statement
            :rtype: boolean
        """
        from th_github.models import Github
        if self.token:
            title = self.set_title(data)
            body = self.set_content(data)
            # get the details of this trigger
            trigger = Github.objects.get(trigger_id=trigger_id)

            # check if it remains more than 1 access
            # then we can create an issue
            limit = self.gh.ratelimit_remaining
            if limit > 1:
                # repo goes to "owner"
                # project goes to "repository"
                r = self.gh.create_issue(trigger.repo,
                                         trigger.project,
                                         title,
                                         body)
            else:
                # rate limit reach
                logger.warn("Rate limit reached")
                # put again in cache the data that could not be
                # published in Github yet
                cache.set('th_github_' + str(trigger_id), data, version=2)
                return True
            sentence = str('github {} created').format(r)
            logger.debug(sentence)
            status = True
        else:
            sentence = "no token or link provided for trigger ID {} "
            logger.critical(sentence.format(trigger_id))
            status = False

        return status

    def auth(self, request):
        """
            let's auth the user to the Service
            :param request: request object
            :return: callback url
            :rtype: string that contains the url to redirect after auth
        """
        auth = self.gh.authorize(self.username,
                                 self.password,
                                 self.scope,
                                 '',
                                 '',
                                 self.consumer_key,
                                 self.consumer_secret)
        request.session['oauth_token'] = auth.token
        request.session['oauth_id'] = auth.id
        return self.callback_url(request)

    def callback(self, request, **kwargs):
        """
            Called from the Service when the user accept to activate it
            :param request: request object
            :return: callback url
            :rtype: string , path to the template
        """
        access_token = request.session['oauth_token'] + "#TH#"
        access_token += str(request.session['oauth_id'])
        kwargs = {'access_token': access_token}
        return super(ServiceGithub, self).callback(request, **kwargs)