예제 #1
0
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 }
예제 #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
def receive_webhook(event_type, data):
    # add additional event handlers here
    # https://developer.github.com/webhooks/#events
    # https://developer.github.com/v3/activity/events/types/
    response = {
        'event_type': event_type
    }
    try:
        if not event_type:
            print 'receive_webhook, missing event_type'
            response['error'] = 'missing event_type'
            return response

        if not data:
            print 'receive_webhook, missing data'
            response['error'] = 'missing data'
            return response

        action = data.get('action')

        if not action:
            print 'receive_webhook, missing action'
            response['error'] = 'missing action'
            return response

        response['action'] = action

        issue = data.get('issue')
        if not issue:
            print 'receive_webhook, missing issue'
            response['error'] = 'missing issue'
            return response

        number = issue.get('number')
        if not number:
            print 'receive_webhook, missing issue number'
            response['error'] = 'missing issue number'
            return response

        response['number'] = number

        repository = data.get('repository')
        if not repository:
            print 'receive_webhook, missing repository'
            response['error'] = 'missing repository'
            return response

        repo_id = repository.get('name')

        repository_owner = repository.get('owner')
        if not repository_owner:
            print 'receive_webhook, missing repository_owner'
            response['error'] = 'missing repository_owner'
            return response

        repo_username = repository_owner.get('login')

        if action == 'updated':
            # not an actual GITHUB action, but faking it from the custom submit form
            # so the issue data it provides is minimal, that's why we're looking it up again
            issue = github_api.fetch_issue(repo_username, repo_id, number)
            if not issue or issue.get('error'):
                response['error'] = 'unable to get updated issue'
                response['issue'] = issue
                return response

        response['html_url'] = issue.get('html_url')

        print 'receive_webhook, %s %s, issue %s, event: %s, action: %s' % (repo_username, repo_id, number, event_type, action)

        if event_type == 'issues' and action == 'opened':
            add_labels = github_issue_submit.add_label_from_content(repo_username, repo_id, issue)
            response['add_labels'] = add_labels
            if len(add_labels):
              github_api.add_issue_labels(repo_username, repo_id, number, add_labels, issue=issue)

            response['flagged_if_submitted_through_github'] = github_issue_submit.flag_if_submitted_through_github(repo_username, repo_id, issue)

        elif event_type == 'issues' and action == 'closed':
            existing = models.get_issue(repo_username, repo_id, number)
            if existing:
                db.session.delete(existing)
                db.session.commit()
            github_issue_submit.remove_flag_when_closed(repo_username, repo_id, issue)
            response['closed'] = True
            return response

        response['issue_maintenance'] = maintenance.issue_maintenance(repo_username, repo_id, issue)
        return response

    except Exception as ex:
        print 'receive_webhook, %s, %s: %s' % (event_type, data, ex)
        response['error'] = '%s' % ex

    return response