def update_issue_score(repo_username, repo_id, number, data={}):
        if not data.get('issue'):
            data['issue'] = github_api.fetch_issue(repo_username, repo_id, number)
            if not data.get('issue'):

        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

        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:


        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 issue_maintenance_number(repo_username, repo_id, number):
        issue = github_api.fetch_issue(repo_username, repo_id, number)

        if issue.get('error'):
            return issue

        return issue_maintenance(repo_username, repo_id, issue)

    except Exception as ex:
        print 'run_maintenance_tasks error: %s' % ex
        return { 'error': '%s' % ex }
def submit_issue_response(repo_username, repo_id, number, action_type, message_type, custom_message):
    data = {
        'repo_username': repo_username,
        'repo_id': repo_id,
        'number': number,
        'action_type': action_type,
        'message_type': message_type,
        'custom_message': custom_message

        issue = github_api.fetch_issue(repo_username, repo_id, number)
        if not issue or issue.get('error'):
            data['error'] = 'could not find issue %s' % number
            return data

        context = {
            'issue': issue,
            'user': issue.get('user')
        msg = None

        if message_type == 'expire':
            msg = util.get_template('EXPIRE_TEMPLATE', context)

        elif message_type == 'forum':
            msg = util.get_template('FORUM_TEMPLATE', context)

        elif message_type == 'inapplicable':
            msg = util.get_template('INAPPLICABLE_TEMPLATE', context)

        elif message_type == 'more':
            msg = util.get_template('MORE_TEMPLATE', context)

        elif message_type == 'feature':
            github_api.add_issue_labels(repo_username, repo_id, number, [cvar['FEATURE_REQUEST_LABEL']], issue=issue)
            msg = util.get_template('FEATURE_REQUEST_TEMPLATE', context)

        elif message_type == 'no_reply':
            msg = util.get_template('CLOSING_NOREPLY_TEMPLATE', context)

        elif message_type == 'pr_close':
            msg = util.get_template('CLOSE_PULL_REQUEST_TEMPLATE', context)

        elif message_type == 'custom':
            msg = custom_message

        elif message_type == 'close':
            msg = None

            data['error'] = 'invalid message_type: %s' % message_type
            return data

        if msg and len(msg.strip()):
            data['created_comment'] = github_api.create_issue_comment(repo_username, repo_id, number, msg)

        if action_type == 'close':
            data['issue_closed'] = github_api.close_issue(repo_username, repo_id, number, issue)

        elif action_type == 'reply':
            github_api.add_issue_labels(repo_username, repo_id, number, [cvar['NEEDS_REPLY_LABEL']], issue=issue)

    except Exception as ex:
        print 'submit_issue_response error, %s: %s' % (number, ex)
        data['error'] = '%s' % ex

    return data
def receive_webhook(event_type, data):
    # add additional event handlers here
    response = {
        'event_type': event_type
        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:
            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