Exemple #1
0
def fetch_org_members_logins(repo_username):
    logins = []
    org = repo_username

    cache_key = '%s:members' % (org)
    cached_data = util.get_cached_data(cache_key)
    if cached_data is not None:
        return cached_data

    try:
        org_members = fetch('/orgs/%s/members' % org, 60*60*24*7)
        if not isinstance(org_members, list):
            logins.append(org)
            util.set_cached_data(cache_key, logins, 60*60*24*7)
            return logins

        for org_member in org_members:
            logins.append(org_member.get('login'))

        org_teams = fetch('/orgs/%s/teams' % org, 60*60*24*7)
        for org_team in org_teams:
            org_team_members = fetch('/teams/%s/members' % (org_team.get('id')), 60*60*24*7)

            for org_team_member in org_team_members:
                team_member_login = org_team_member.get('login')
                if team_member_login not in logins:
                    logins.append(team_member_login)

        if len(logins):
            util.set_cached_data(cache_key, logins, 60*60*24*7)

    except Exception as ex:
        print 'fetch_org_members_logins, %s' % ex

    return logins
def get_issue_scores(repo_username, repo_id):
    try:
        data = {
            'repo_username': repo_username,
            'repo_id': repo_id,
            'repo_url': 'https://github.com/%s/%s' % (repo_username, repo_id),
            'repo_issues_url': 'https://github.com/%s/%s/issues' % (repo_username, repo_id),
            'issues': []
        }

        open_issues = github_api.fetch_open_issues(repo_username, repo_id)
        if not open_issues or not isinstance(open_issues, list):
            return { 'error': 'Unable to fetch open issues: %s/%s' % (repo_username, repo_id) }

        for issue in open_issues:
            cache_key = get_issue_cache_key(repo_username, repo_id, issue.get('number'))
            cached_data = util.get_cached_data(cache_key)

            if cached_data:
                milestone = issue.get('milestone')
                if milestone:
                    cached_data['milestone'] = milestone.get('title', '') or ''

                if issue.get('pull_request') is not None:
                    cached_data['pull_request'] = True

                data['issues'].append(cached_data)
                continue

            db_data = models.get_issue(repo_username, repo_id, issue.get('number'))
            if db_data:
                issue_score = db_data.to_dict()

                milestone = issue.get('milestone')
                if milestone:
                    issue_score['milestone'] = milestone.get('title', '') or ''

                if issue.get('pull_request') is not None:
                    issue_score['pull_request'] = True

                data['issues'].append(issue_score)
                continue

            print 'could not find issue calculation: %s' % (cache_key)

        rank_inc = 1
        data['issues'] = sorted(data['issues'], key=lambda k: k['score'], reverse=True)
        for issue in data['issues']:
            issue['rank'] = rank_inc
            issue['repo_username'] = repo_username
            issue['repo_id'] = repo_id
            issue['repo_issue_url'] = 'https://github.com/%s/%s/issues/%s' % (repo_username, repo_id, issue.get('number'))
            rank_inc += 1

        return data

    except Exception as ex:
        print 'get_issue_scores error: %s' % ex
        return { 'error': '%s' % ex }
Exemple #3
0
def fetch(path, expires=180):
    try:
        url = '%s%s' % (GITHUB_API_URL, path)

        if expires > 0:
            cache_key = url
            cached_data = util.get_cached_data(cache_key)

            if cached_data is not None:
                print 'fetched from cache: %s' % path
                return cached_data

        print 'fetch: %s' % path

        data = None
        try:
            r = requests.get(url, auth=GITHUB_AUTH)
            if r.status_code == 204:
                print 'fetch %s, status_code: 204, no content' % (url)
                data = { 'error': 'no content' }
            elif r.status_code < 204:
                data = r.json()
            else:
                return { 'error': r.text, 'status_code': r.status_code }

        except Exception as ex:
            print 'fetch error, %s:, %s' % (path, ex)
            return { 'error': '%s' % ex, 'fetch': url }

        # Iterate through additional pages of data if available
        has_next = True if 'next' in r.links else False
        while has_next:
            try:
                url = r.links['next']['url']
                r = requests.get(url, auth=GITHUB_AUTH)
                if r.status_code < 204:
                    data += r.json()
                    has_next = True if 'next' in r.links else False
                else:
                    has_next = False
                    data['error'] = r.text
                    return data

            except Exception as ex:
                print 'fetch error, %s:, %s' % (path, ex)
                return { 'error': '%s' % ex, 'next_fetch': url }

        if expires > 0:
            util.set_cached_data(cache_key, data, expires)

        return data

    except Exception as ex:
        print 'fetch_data, %s: %s' % (url, ex)
        return { 'error': '%s' % ex, 'fetch': url }