コード例 #1
0
ファイル: github_api.py プロジェクト: harikt/ionitron-issues
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
コード例 #2
0
def update_issue_score(repo_username, repo_id, number, data={}):
    try:
        if not data.get('issue'):
            data['issue'] = github_api.fetch_issue(repo_username, repo_id, number)
            if not data.get('issue'):
                return

        if not data.get('issue_comments'):
            data['issue_comments'] = github_api.fetch_issue_comments(repo_username, repo_id, number)

        if not data.get('org_members'):
            data['org_members'] = github_api.fetch_org_members_logins(repo_username)

        if not data.get('contributors'):
            data['contributors'] = github_api.fetch_repo_contributors(repo_username, repo_id)

        c = issue_score_calculator.ScoreCalculator(number=number, data=data)
        success = c.load_scores()

        if not success:
            data['load_scores'] = False
            return

        data = c.to_dict()

        cache_key = get_issue_cache_key(repo_username, repo_id, number)
        util.set_cached_data(cache_key, data, 60*60*24*7)

        issue_score = models.IssueScore(repo_username, repo_id, number)
        issue_score.score = data['score']
        issue_score.title = data['title']
        issue_score.comments = data['comments']
        issue_score.references = data['references']
        issue_score.username = data['username']
        issue_score.created = data['created']
        issue_score.updated = data['updated']
        issue_score.avatar = data['avatar']
        issue_score.score_data = json.dumps(data['score_data'])
        issue_score.assignee = data['assignee']
        issue_score.milestone = data['milestone']

        existing = models.get_issue(repo_username, repo_id, number)
        if existing:
            db.session.delete(existing)
            db.session.commit()

        db.session.add(issue_score)
        db.session.commit()

        print 'update_issue_score: %s, score: %s' % (number, data.get('score'))

        return data

    except Exception as ex:
        print 'update_issue_score error, issue %s: %s' % (number, ex)
        return { 'issue_updated': False, 'issue': number, 'error': '%s' % ex }
コード例 #3
0
ファイル: github_api.py プロジェクト: harikt/ionitron-issues
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 }