Ejemplo n.º 1
0
def home(request):
    print '****** Welcome to home page ********'
    print >> sys.stderr, '****** Welcome to the error output ******'
    if 'target_repo' in request.GET:
        print "we are inside"
        target_repo = request.GET['target_repo']
        if target_repo.strip() == "" or len(target_repo.split('/')) != 2:
            return render(request, 'msg.html',
                          {'msg': 'please enter a valid repo'})
        init_g()
        # if not has_access_to_repo(target_repo):# this for the organization
        # return render(request,'msg.html',{'msg': 'repos under organizations are not supported at the moment'})
        if request.user.is_authenticated():
            ouser = OUser.objects.get(username=request.user.username)
            # this is initially to get the access key for the user
            webhook_access_url, state = webhook_access(
                client_id, host + '/get_access_token', ouser.private)
        else:  # private access in case not logged in
            webhook_access_url, state = webhook_access(
                client_id, host + '/get_access_token', True)
        request.session['target_repo'] = target_repo
        request.session['state'] = state
        # if '127.0.0.1:8000' not in request.META['HTTP_HOST']:  # Not testing   # or not settings.test_conf['local']:
        if True:
            request.session['access_token_time'] = '1'
            return HttpResponseRedirect(webhook_access_url)
        if request.user.is_authenticated():
            generateforall(target_repo, request.user.email)
    repos = Repo.objects.order_by('-last_used')[:10]
    return render(request, 'home.html', {'repos': repos, 'user': request.user})
Ejemplo n.º 2
0
def home(request):
    print '****** Welcome to home page ********'
    print >> sys.stderr, '****** Welcome to the error output ******'
    if 'target_repo' in request.GET:
        print "we are inside"
        target_repo = request.GET['target_repo']
        if target_repo.strip() == "" or len(target_repo.split('/')) != 2:
            return render(request, 'msg.html', {'msg': 'please enter a valid repo'})
        init_g()
        # if not has_access_to_repo(target_repo):# this for the organization
        # return render(request,'msg.html',{'msg': 'repos under organizations are not supported at the moment'})
        if request.user.is_authenticated():
            ouser = OUser.objects.get(username=request.user.username)
            # this is initially to get the access key for the user
            webhook_access_url, state = webhook_access(client_id, host + '/get_access_token', ouser.private)
        else:  # private access in case not logged in
            webhook_access_url, state = webhook_access(client_id, host + '/get_access_token', True)
        request.session['target_repo'] = target_repo
        request.session['state'] = state
        # if '127.0.0.1:8000' not in request.META['HTTP_HOST']:  # Not testing   # or not settings.test_conf['local']:
        if True:
            request.session['access_token_time'] = '1'
            return HttpResponseRedirect(webhook_access_url)
        if request.user.is_authenticated():
            generateforall(target_repo, request.user.email)
    repos = Repo.objects.order_by('-last_used')[:10]
    return render(request, 'home.html', {'repos': repos, 'user': request.user})
Ejemplo n.º 3
0
def home(request):
    global client_id, client_secret, is_private
    # sys.stdout = sys.stderr
    # print "******* output to stderror ********"
    sys.stdout.flush()
    sys.stderr.flush()
    if 'target_repo' in request.GET:
        print "we are inside"
        target_repo = request.GET['target_repo']
        if target_repo.strip() == "" or len(target_repo.split('/')) != 2:
            return render(request, 'msg.html', {'msg': 'please enter a valid repo'})
        init_g()
        # if not has_access_to_repo(target_repo):# this for the organization
        # return render(request,'msg.html',{'msg': 'repos under organizations are not supported at the moment'})
        wgets_dir = os.environ['wget_dir']
        if call('cd %s; wget %s;' % (wgets_dir, 'http://github.com/' + target_repo.strip()), shell=True) == 0:
            is_private = False
            client_id = client_id_public
            client_secret = client_secret_public
        else:
            is_private = True
            client_id = client_id_private
            client_secret = client_secret_private
        webhook_access_url, state = webhook_access(client_id, host + '/get_access_token', isprivate=is_private)
        request.session['target_repo'] = target_repo
        request.session['state'] = state
        request.session['access_token_time'] = '1'
        return HttpResponseRedirect(webhook_access_url)
    repos = Repo.objects.order_by('-last_used')[:10]
    num_of_users = len(User.objects.all())
    num_of_repos = len(Repo.objects.all())
    print "returning the request"
    print("The user: %s" % str(request.user) )
    return render(request, 'home.html', {'repos': repos, 'user': request.user, 'num_of_users': num_of_users,
                                         'num_of_repos': num_of_repos})
Ejemplo n.º 4
0
def home(request):
    print '****** Welcome to home page ********'
    print >> sys.stderr,  '****** Welcome to the error output ******'
    if 'target_repo' in request.GET:
        #print request.GET
        target_repo = request.GET['target_repo']
        init_g()
        if not has_access_to_repo(target_repo):# this for the organization
            return render(request,'msg.html',{'msg': 'repos under organizations are not supported at the moment'})
        webhook_access_url, state = webhook_access(client_id,host+'/get_access_token')
        request.session['target_repo'] = target_repo
        request.session['state'] = state 
        try: 
            repo = Repo.objects.get(url=target_repo)
        except Exception as e:
            print str(e)
            repo = Repo()
            repo.url=target_repo
            repo.save()            
        if request.user.is_authenticated():
            ouser = OUser.objects.get(email=request.user.email)
            if repo not in ouser.repos:
                ouser.repos.append(repo)
                ouser.save()
        sys.stdout.flush()
        sys.stderr.flush()        
        if '127.0.0.1:8000' not in request.META['HTTP_HOST'] or not settings.test_conf['local']:
            return  HttpResponseRedirect(webhook_access_url)
    sys.stdout.flush()
    sys.stderr.flush()
    repos = get_repos_formatted(Repo.objects.all())
    return render(request,'home.html',{'repos': repos, 'user': request.user })    
Ejemplo n.º 5
0
def home(request):
    print '****** Welcome to home page ********'
    print >> sys.stderr, '****** Welcome to the error output ******'
    if 'target_repo' in request.GET:
        #print request.GET
        target_repo = request.GET['target_repo']
        init_g()
        if not has_access_to_repo(target_repo):  # this for the organization
            return render(request, 'msg.html', {
                'msg':
                'repos under organizations are not supported at the moment'
            })
        webhook_access_url, state = webhook_access(client_id,
                                                   host + '/get_access_token')
        request.session['target_repo'] = target_repo
        request.session['state'] = state
        try:
            repo = Repo.objects.get(url=target_repo)
        except Exception as e:
            print str(e)
            repo = Repo()
            repo.url = target_repo
            repo.save()
        if request.user.is_authenticated():
            ouser = OUser.objects.get(email=request.user.email)
            if repo not in ouser.repos:
                ouser.repos.append(repo)
                ouser.save()
        sys.stdout.flush()
        sys.stderr.flush()
        if '127.0.0.1:8000' not in request.META[
                'HTTP_HOST'] or not settings.test_conf['local']:
            return HttpResponseRedirect(webhook_access_url)
    sys.stdout.flush()
    sys.stderr.flush()
    repos = get_repos_formatted(Repo.objects.all())
    return render(request, 'home.html', {'repos': repos, 'user': request.user})
Ejemplo n.º 6
0
def get_access_token(request):
    print "get_access_token"
    global is_private, client_id, client_secret
    if 'state' not in request.session or request.GET[
            'state'] != request.session['state']:
        return HttpResponseRedirect('/')
    data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'code': request.GET['code'],
        'redirect_uri': host + '/add_hook'
    }
    res = requests.post('https://github.com/login/oauth/access_token',
                        data=data)
    try:
        atts = res.text.split('&')
        d = {}
        for att in atts:
            keyv = att.split('=')
            d[keyv[0]] = keyv[1]
    except Exception as e:
        print "Exception: %s" % str(e)
        print "response: %s" % str(res.text)
        return render(request, 'msg.html', {
            'Error getting the token from GitHub. please try again or contact us'
        })
    if 'access_token' not in d:
        print 'access_token is not there'
        print d
        return HttpResponseRedirect('/')

    access_token = d['access_token']
    request.session['access_token'] = access_token
    update_g(access_token)
    print 'access_token: ' + access_token

    if request.user.is_authenticated(
    ) and request.session['access_token_time'] == '1':
        request.session['access_token_time'] = '2'  # so we do not loop
        webhook_access_url, state = webhook_access(client_id,
                                                   host + '/get_access_token',
                                                   is_private)
        request.session['state'] = state
        return HttpResponseRedirect(webhook_access_url)

    rpy_wh = add_webhook(request.session['target_repo'], host + "/add_hook")
    rpy_coll = add_collaborator(request.session['target_repo'], ToolUser)
    error_msg = ""
    if rpy_wh['status'] == False:
        error_msg += str(rpy_wh['error'])
        print 'error adding webhook: ' + error_msg
    if rpy_coll['status'] == False:
        error_msg += str(rpy_coll['error'])
        print 'error adding collaborator: ' + rpy_coll['error']
    else:
        print 'adding collaborator: ' + rpy_coll['msg']
    if error_msg != "":
        if 'Hook already exists on this repository' in error_msg:
            error_msg = 'This repository already watched'
        elif '404' in error_msg:  # so no enough access according to Github troubleshooting guide
            error_msg = """You don\'t have permission to add collaborators and create webhooks to this repo or this
            repo does not exist. Note that if you can fork this repo, you can add it here"""
            return render(request, 'msg.html', {'msg': error_msg})
        else:
            print "error message not hook and not 404: " + error_msg
            print "target repo: " + request.session['target_repo']
            print "ToolUser: "******"My repositories" page, you can see the
          status of each repo.'''
    target_repo = request.session['target_repo']
    try:
        repo = Repo.objects.get(url=target_repo)
    except Exception as e:
        print str(e)
        repo = Repo()
        repo.url = target_repo
        repo.save()
    if request.user.is_authenticated():
        ouser = OUser.objects.get(email=request.user.email)
        if repo not in ouser.repos:
            ouser.repos.append(repo)
            ouser.save()
            generateforall(repo.url, ouser.email)
    return render(request, 'msg.html', {'msg': msg})
Ejemplo n.º 7
0
def get_access_token(request):
    if 'state' not in request.session or request.GET[
            'state'] != request.session['state']:
        return HttpResponseRedirect('/')
    data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'code': request.GET['code'],
        'redirect_uri': host + '/add_hook'
    }
    res = requests.post('https://github.com/login/oauth/access_token',
                        data=data)
    atts = res.text.split('&')
    d = {}
    for att in atts:
        keyv = att.split('=')
        d[keyv[0]] = keyv[1]
    if 'access_token' not in d:
        print 'access_token is not there'
        return HttpResponseRedirect('/')
    access_token = d['access_token']
    request.session['access_token'] = access_token
    update_g(access_token)
    print 'access_token: ' + access_token

    if request.user.is_authenticated(
    ) and request.session['access_token_time'] == '1':
        request.session['access_token_time'] = '2'  # so we do not loop
        isprivate = get_proper_loggedin_scope(
            OUser.objects.get(username=request.user.username),
            request.session['target_repo'])
        print 'isprivate is: ' + str(isprivate)
        webhook_access_url, state = webhook_access(client_id,
                                                   host + '/get_access_token',
                                                   isprivate)
        request.session['state'] = state
        return HttpResponseRedirect(webhook_access_url)

    rpy_wh = add_webhook(request.session['target_repo'], host + "/add_hook")
    rpy_coll = add_collaborator(request.session['target_repo'], ToolUser)
    error_msg = ""
    if rpy_wh['status'] == False:
        error_msg += str(rpy_wh['error'])
        print 'error adding webhook: ' + error_msg
    if rpy_coll['status'] == False:
        error_msg += str(rpy_coll['error'])
        print 'error adding collaborator: ' + rpy_coll['error']
    else:
        print 'adding collaborator: ' + rpy_coll['msg']
    if error_msg != "":
        if 'Hook already exists on this repository' in error_msg:
            error_msg = 'This repository already watched'
        elif '404' in error_msg:  # so no enough access according to Github troubleshooting guide
            error_msg = """You don\'t have permission to add collaborators and create webhooks to this repo or this
            repo does not exist. Note that if you can fork this repo, you can add it here"""
            return render_to_response('msg.html', {'msg': error_msg},
                                      context_instance=RequestContext(request))
        msg = error_msg
    else:
        msg = 'webhook attached and user added as collaborator'
    target_repo = request.session['target_repo']
    try:
        repo = Repo.objects.get(url=target_repo)
    except Exception as e:
        print str(e)
        repo = Repo()
        repo.url = target_repo
        repo.save()
    if request.user.is_authenticated():
        ouser = OUser.objects.get(email=request.user.email)
        if repo not in ouser.repos:
            ouser.repos.append(repo)
            ouser.save()
            generateforall(repo.url, ouser.email)
    return render_to_response('msg.html', {'msg': msg},
                              context_instance=RequestContext(request))
Ejemplo n.º 8
0
def get_access_token(request):
    if 'state' not in request.session or request.GET['state'] != request.session['state']:
        return HttpResponseRedirect('/')
    data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'code': request.GET['code'],
        'redirect_uri': host + '/add_hook'
    }
    res = requests.post('https://github.com/login/oauth/access_token', data=data)
    atts = res.text.split('&')
    d = {}
    for att in atts:
        keyv = att.split('=')
        d[keyv[0]] = keyv[1]
    if 'access_token' not in d:
        print 'access_token is not there'
        return HttpResponseRedirect('/')
    access_token = d['access_token']
    request.session['access_token'] = access_token
    update_g(access_token)
    print 'access_token: ' + access_token

    if request.user.is_authenticated() and request.session['access_token_time'] == '1':
        request.session['access_token_time'] = '2'  # so we do not loop
        isprivate = get_proper_loggedin_scope(OUser.objects.get(username=request.user.username),
                                              request.session['target_repo'])
        print 'isprivate is: ' + str(isprivate)
        webhook_access_url, state = webhook_access(client_id, host + '/get_access_token', isprivate)
        request.session['state'] = state
        return HttpResponseRedirect(webhook_access_url)

    rpy_wh = add_webhook(request.session['target_repo'], host + "/add_hook")
    rpy_coll = add_collaborator(request.session['target_repo'], ToolUser)
    error_msg = ""
    if rpy_wh['status'] == False:
        error_msg += str(rpy_wh['error'])
        print 'error adding webhook: ' + error_msg
    if rpy_coll['status'] == False:
        error_msg += str(rpy_coll['error'])
        print 'error adding collaborator: ' + rpy_coll['error']
    else:
        print 'adding collaborator: ' + rpy_coll['msg']
    if error_msg != "":
        if 'Hook already exists on this repository' in error_msg:
            error_msg = 'This repository already watched'
        elif '404' in error_msg:  # so no enough access according to Github troubleshooting guide
            error_msg = """You don\'t have permission to add collaborators and create webhooks to this repo or this
            repo does not exist. Note that if you can fork this repo, you can add it here"""
            return render_to_response('msg.html', {'msg': error_msg}, context_instance=RequestContext(request))
        msg = error_msg
    else:
        msg = 'webhook attached and user added as collaborator'
    target_repo = request.session['target_repo']
    try:
        repo = Repo.objects.get(url=target_repo)
    except Exception as e:
        print str(e)
        repo = Repo()
        repo.url = target_repo
        repo.save()
    if request.user.is_authenticated():
        ouser = OUser.objects.get(email=request.user.email)
        if repo not in ouser.repos:
            ouser.repos.append(repo)
            ouser.save()
            generateforall(repo.url, ouser.email)
    return render_to_response('msg.html', {'msg': msg},
                              context_instance=RequestContext(request))