<commit_before># -*- coding: utf-8 -*-

import os
import urlparse
import itertools

from github3 import GitHubError
from modularodm import fields

from framework.auth import Auth

from website import settings
from website.util import web_url_for
from website.addons.base import exceptions
from website.addons.base import AddonOAuthUserSettingsBase, AddonOAuthNodeSettingsBase
from website.addons.base import StorageAddonBase

from website.addons.github import utils
from website.addons.github.api import GitHubClient
from website.addons.github.serializer import GitHubSerializer
from website.addons.github import settings as github_settings
from website.addons.github.exceptions import ApiError, NotFoundError
from website.oauth.models import ExternalProvider


hook_domain = github_settings.HOOK_DOMAIN or settings.DOMAIN


class GitHubProvider(ExternalProvider):
    name = 'GitHub'
    short_name = 'github'

    client_id = github_settings.CLIENT_ID
    client_secret = github_settings.CLIENT_SECRET

    auth_url_base = github_settings.OAUTH_AUTHORIZE_URL
    callback_url = github_settings.OAUTH_ACCESS_TOKEN_URL
    default_scopes = github_settings.SCOPE

    def handle_callback(self, response):
        """View called when the OAuth flow is completed. Adds a new GitHubUserSettings
        record to the user and saves the account info.
        """
        client = GitHubClient(
            access_token=response['access_token']
        )

        user_info = client.user()

        return {
            'provider_id': str(user_info.id),
            'profile_url': user_info.html_url,
            'display_name': user_info.login
        }


class GitHubUserSettings(AddonOAuthUserSettingsBase):
    """Stores user-specific github information
    """
    oauth_provider = GitHubProvider
    serializer = GitHubSerializer

    # Required for importing username from social profile configuration page
    # Assumes oldest connected account is primary.
    @property
    def public_id(self):
        gh_accounts = [
            a for a in self.owner.external_accounts
            if a.provider == self.oauth_provider.short_name
        ]
        if gh_accounts:
            return gh_accounts[0].display_name
        return None


class GitHubNodeSettings(StorageAddonBase, AddonOAuthNodeSettingsBase):
    oauth_provider = GitHubProvider
    serializer = GitHubSerializer

    user = fields.StringField()
    repo = fields.StringField()
    hook_id = fields.StringField()<commit_msg>Remove remote grant when deauthorizing github     -For the last user using the account<commit_after># -*- coding: utf-8 -*-

import os
import urlparse
import itertools

from github3 import GitHubError
from modularodm import fields

from framework.auth import Auth

from website import settings
from website.util import web_url_for
from website.addons.base import exceptions
from website.addons.base import AddonOAuthUserSettingsBase, AddonOAuthNodeSettingsBase
from website.addons.base import StorageAddonBase

from website.addons.github import utils
from website.addons.github.api import GitHubClient
from website.addons.github.serializer import GitHubSerializer
from website.addons.github import settings as github_settings
from website.addons.github.exceptions import ApiError, NotFoundError
from website.oauth.models import ExternalProvider


hook_domain = github_settings.HOOK_DOMAIN or settings.DOMAIN


class GitHubProvider(ExternalProvider):
    name = 'GitHub'
    short_name = 'github'

    client_id = github_settings.CLIENT_ID
    client_secret = github_settings.CLIENT_SECRET

    auth_url_base = github_settings.OAUTH_AUTHORIZE_URL
    callback_url = github_settings.OAUTH_ACCESS_TOKEN_URL
    default_scopes = github_settings.SCOPE

    def handle_callback(self, response):
        """View called when the OAuth flow is completed. Adds a new GitHubUserSettings
        record to the user and saves the account info.
        """
        client = GitHubClient(
            access_token=response['access_token']
        )

        user_info = client.user()

        return {
            'provider_id': str(user_info.id),
            'profile_url': user_info.html_url,
            'display_name': user_info.login
        }


class GitHubUserSettings(AddonOAuthUserSettingsBase):
    """Stores user-specific github information
    """
    oauth_provider = GitHubProvider
    serializer = GitHubSerializer

    def revoke_remote_oauth_access(self, external_account):
        """Overrides default behavior during external_account deactivation.

        Tells GitHub to remove the grant for the OSF associated with this account.
        """
        connection = GitHubClient(external_account=external_account)
        try:
            connection.revoke_token()
        except GitHubError:
            pass

    # Required for importing username from social profile configuration page
    # Assumes oldest connected account is primary.
    @property
    def public_id(self):
        gh_accounts = [
            a for a in self.owner.external_accounts
            if a.provider == self.oauth_provider.short_name
        ]
        if gh_accounts:
            return gh_accounts[0].display_name
        return None


class GitHubNodeSettings(StorageAddonBase, AddonOAuthNodeSettingsBase):
    oauth_provider = GitHubProvider
    serializer = GitHubSerializer

    user = fields.StringField()
    repo = fields.StringField()
    hook_id = fields.StringField()
{'fraction_non_alphanumeric': 0.05980554026784076, 'fraction_numerical': 0.001100715465052284, 'mean_word_length': 3.4615384615384617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3493462', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1442, 'n_words': 402}