def get_needs_resubmit_comment(repo_username, repo_id, issue, issue_comments=None, needs_resubmit_content_id=cvar['NEEDS_RESUBMIT_CONTENT_ID']):
    if issue_comments is None:
        issue_comments = github_api.fetch_issue_comments(repo_username, repo_id, issue.get('number'))

    if issue_comments and isinstance(issue_comments, list):
        for issue_comment in issue_comments:
            body = issue_comment.get('body')
            if body and needs_resubmit_content_id in body:
                return issue_comment
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 }
def remove_needs_reply_comment(repo_username, repo_id, number, issue_comments=None, needs_reply_content_id=cvar['NEEDS_REPLY_CONTENT_ID'], is_debug=cvar['DEBUG']):
    try:
        if issue_comments is None:
            issue_comments = github_api.fetch_issue_comments(repo_username, repo_id, number)

        if not issue_comments or not isinstance(issue_comments, list):
            return 'invalid comments'

        for issue_comment in issue_comments:
            body = issue_comment.get('body')
            comment_id = issue_comment.get('id')
            if body and needs_reply_content_id in body and comment_id:
                if not is_debug:
                    github_api.delete_issue_comment(repo_username, repo_id, comment_id, number=number)
                return 'removed auto comment'

        return 'no comment to remove'

    except Exception as ex:
        return 'remove_needs_reply_comment: %s' % ex
def manage_needs_reply_issue(repo_username, repo_id, issue):
    if not issue:
        return

    number = issue.get('number')
    if not number:
        return

    if not has_needs_reply_label(issue):
        return

    issue_events = github_api.fetch_issue_events(repo_username, repo_id, number)
    if not issue_events or not isinstance(issue_events, list):
        return

    need_reply_label_added = get_most_recent_datetime_need_reply_label_added(issue_events)
    if not need_reply_label_added:
        return

    issue_comments = github_api.fetch_issue_comments(repo_username, repo_id, number)
    if not issue_comments or not isinstance(issue_comments, list):
        return

    most_recent_response = get_most_recent_datetime_creator_response(issue, issue_comments)
    if not most_recent_response:
        return

    print 'Needs reply: %s, label added: %s, most recent response: %s' % (number, need_reply_label_added, most_recent_response)

    if has_replied_since_adding_label(need_reply_label_added, most_recent_response):
        print 'has_replied_since_adding_label, removing label: %s' % number
        return remove_needs_reply_label(repo_username, repo_id, number, issue)

    if not has_replied_in_timely_manner(need_reply_label_added):
        print 'not has_replied_in_timely_manner, closing issue: %s' % number
        return close_needs_reply_issue(repo_username, repo_id, number)