Beispiel #1
0
def save_integration_tokens(user, task_id, provider):
    token_info = dict()
    if provider == GitHubProvider.id:
        social_token = get_social_token(user=user, provider=provider)
        if social_token:
            token_info = dict(
                token=social_token.token,
                token_secret=social_token.token_secret,
                token_expires_at=social_token.expires_at
            )
    else:
        app_integration = get_app_integration(user=user, provider=provider)
        if app_integration:
            token_info = dict(
                token=app_integration.token,
                token_secret=app_integration.token_secret,
                token_expires_at=app_integration.expires_at,
                token_extra=app_integration.extra
            )

            if provider == APP_INTEGRATION_PROVIDER_SLACK:
                token_info.pop('token_secret')
                response = json.loads(app_integration.extra)
                if 'bot' in response:
                    token_info['bot_access_token'] = response['bot'].get('bot_access_token')
                    token_info['bot_user_id'] = response['bot'].get('bot_user_id')
            elif provider == APP_INTEGRATION_PROVIDER_HARVEST:
                token_info.pop('token_secret')
                token_info['refresh_token'] = app_integration.token_secret
    save_task_integration_meta(task_id, provider, token_info)
Beispiel #2
0
def save_integration_tokens(user, task_id, provider):
    token_info = dict()
    if provider == GitHubProvider.id:
        social_token = get_social_token(user=user, provider=provider)
        if social_token:
            token_info = dict(token=social_token.token,
                              token_secret=social_token.token_secret,
                              token_expires_at=social_token.expires_at)
    else:
        app_integration = get_app_integration(user=user, provider=provider)
        if app_integration:
            token_info = dict(token=app_integration.token,
                              token_secret=app_integration.token_secret,
                              token_expires_at=app_integration.expires_at,
                              token_extra=app_integration.extra)

            if provider == APP_INTEGRATION_PROVIDER_SLACK:
                token_info.pop('token_secret')
                response = json.loads(app_integration.extra)
                if 'bot' in response:
                    token_info['bot_access_token'] = response['bot'].get(
                        'bot_access_token')
                    token_info['bot_user_id'] = response['bot'].get(
                        'bot_user_id')
            elif provider == APP_INTEGRATION_PROVIDER_HARVEST:
                token_info.pop('token_secret')
                token_info['refresh_token'] = app_integration.token_secret
    save_task_integration_meta(task_id, provider, token_info)
Beispiel #3
0
def get_webhook_url(user):
    app_integration = get_app_integration(user=user, provider=APP_INTEGRATION_PROVIDER_SLACK)
    if app_integration and app_integration.extra:
        extra = json.loads(app_integration.extra)
        incoming_webhook = extra.get('incoming_webhook', None)
        if incoming_webhook:
            return incoming_webhook.get('url', None)
    return None
Beispiel #4
0
def get_integration_token(user, provider, task=None):
    if task:
        task_integration = get_task_integration(task, provider)
        if task_integration and task_integration.token:
            return GenericObject(
                **dict(token=task_integration.token,
                       token_secret=task_integration.token_secret,
                       extra=task_integration.token_extra))

    if provider == GitHubProvider.id:
        return get_social_token(user=user, provider=provider)
    else:
        return get_app_integration(user=user, provider=provider)
Beispiel #5
0
def get_integration_token(user, provider, task=None):
    if task:
        task_integration = get_task_integration(task, provider)
        if task_integration and task_integration.token:
            return GenericObject(
                **dict(
                    token=task_integration.token,
                    token_secret=task_integration.token_secret,
                    extra=task_integration.token_extra
                )
            )

    if provider == GitHubProvider.id:
        return get_social_token(user=user, provider=provider)
    else:
        return get_app_integration(user=user, provider=provider)
Beispiel #6
0
def get_slack_token(user):
    app_integration = get_app_integration(
        user=user, provider=APP_INTEGRATION_PROVIDER_SLACK)
    if app_integration and app_integration.extra:
        return app_integration.token
    return None
Beispiel #7
0
def get_slack_token(user):
    app_integration = get_app_integration(user=user, provider=APP_INTEGRATION_PROVIDER_SLACK)
    if app_integration and app_integration.extra:
        return app_integration.token
    return None
Beispiel #8
0
def complete_harvest_integration(integration):
    integration = clean_instance(integration, Integration)
    if integration.provider != APP_INTEGRATION_PROVIDER_HARVEST:
        return

    user = integration.task.user
    app_integration = get_app_integration(
        user=integration.task.user, provider=APP_INTEGRATION_PROVIDER_HARVEST)
    if app_integration and app_integration.extra:
        token = json.loads(app_integration.extra)

        project_id = integration.project_id

        harvest_client = harvest_utils.get_api_client(token,
                                                      user=user,
                                                      return_response_obj=True)

        # Create the task and assign it to the project in Harvest
        resp_task_assignment = harvest_client.create_task_to_project(
            project_id,
            task=dict(name='Tunga: {}'.format(integration.task.title)))

        # TODO: Harvest isn't returning the created object, find a work around
        if resp_task_assignment and resp_task_assignment.headers:
            task_assignment_path = resp_task_assignment.headers.get(
                'Location', '')

            m = re.match(
                r'^/projects/(?P<project_id>\d+)/task_assignments/(?P<task_assignment_id>\d+)',
                task_assignment_path,
                flags=re.IGNORECASE)
            if m:
                matches = m.groupdict()
                if matches.get('project_id', None) == project_id:
                    task_assignment_id = matches.get('task_assignment_id',
                                                     None)

                    if task_assignment_id:
                        resp_task_assignment_retrieve = harvest_client.get_one_task_assigment(
                            project_id, task_assignment_id)
                        task_assignment = resp_task_assignment_retrieve.json()

                        defaults = {
                            'created_by':
                            user,
                            'meta_key':
                            'project_task_id',
                            'meta_value':
                            task_assignment['task_assignment']['task_id']
                        }

                        try:
                            IntegrationMeta.objects.update_or_create(
                                integration=integration,
                                meta_key=defaults['meta_key'],
                                defaults=defaults)
                        except:
                            pass

        # Create task participants in Harvest
        participants = integration.task.participation_set.filter(
            status=STATUS_ACCEPTED)
        for participant in participants:
            harvest_client.create_user(
                user={
                    'email': participant.user.email,
                    'first-name': participant.user.first_name,
                    'last-name': participant.user.last_name
                })