scraper/code_gov/models.py
def from_stashy(klass, repository, labor_hours=True):
        """
        Handles crafting Code.gov Project for Bitbucket Server repositories
        """
        # if not isinstance(repository, stashy.repos.Repository):
        #     raise TypeError('Repository must be a stashy Repository object')
        if not isinstance(repository, dict):
            raise TypeError('Repository must be a dict')

        project = klass()

        logger.debug(
            'Stashy: project_key=%s repository_slug=%s',
            repository['name'],
            repository['project']['key'],
        )

        # -- REQUIRED FIELDS --

        project['name'] = repository['name']

        clone_urls = [clone['href'] for clone in repository['links']['clone']]
        for url in clone_urls:
            # Only rely on SSH Urls for repository urls
            if url.startswith('ssh://'):
                project['repositoryURL'] = url
                break

        description = repository['project'].get('description', '')
        if description:
            project['description'] = 'Project description: %s' % description

        project['permissions']['licenses'] = None

        web_url = repository['links']['self'][0]['href']
        public_server = web_url.startswith('https://bitbucket.org')
        if repository['public'] and public_server:
            project['permissions']['usageType'] = 'openSource'

        if labor_hours:
            project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        project['tags'] = ['bitbucket']

        project['contact']['email'] = ''
        project['contact']['URL'] = repository['links']['self'][0]['href']

        # -- OPTIONAL FIELDS --

        # project['version'] = ''

        # project['organization'] = organization.name

        # TODO: Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        project['status'] = 'Development'

        project['vcs'] = repository['scmId']

        project['homepageURL'] = repository['links']['self'][0]['href']

        # project['downloadURL'] =

        # project['languages'] =

        # project['partners'] = []

        # project['relatedCode'] = []

        # project['reusedCode'] = []

        # date: [object] A date object describing the release.
        #   created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
        #   lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
        #   metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
        # project['date'] = {
        #     'created': repository.pushed_at.isoformat(),
        #     'lastModified': repository.updated_at.isoformat(),
        #     'metadataLastUpdated': '',
        # }

        _prune_dict_null_str(project)

        return project
def from_stashy(klass, repository, labor_hours=True):
        """
        Handles crafting Code.gov Project for Bitbucket Server repositories
        """
        # if not isinstance(repository, stashy.repos.Repository):
        #     raise TypeError('Repository must be a stashy Repository object')
        if not isinstance(repository, dict):
            raise TypeError('Repository must be a dict')

        project = klass()

        logger.debug(
            'Stashy: project_key=%s repository_slug=%s',
            repository['name'],
            repository['project']['key'],
        )

        # -- REQUIRED FIELDS --

        project['name'] = repository['name']

        clone_urls = [clone['href'] for clone in repository['links']['clone']]
        for url in clone_urls:
            # Only rely on SSH Urls for repository urls
            if url.startswith('ssh://'):
                project['repositoryURL'] = url
                break

        description = repository['project'].get('description', '')
        if description:
            project['description'] = 'Project description: %s' % description

        project['permissions']['licenses'] = None

        web_url = repository['links']['self'][0]['href']
        public_server = web_url.startswith('https://bitbucket.org')
        if repository['public'] and public_server:
            project['permissions']['usageType'] = 'openSource'

        if labor_hours:
            project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        project['tags'] = ['bitbucket']

        project['contact']['email'] = ''
        project['contact']['URL'] = repository['links']['self'][0]['href']

        # -- OPTIONAL FIELDS --

        # project['version'] = ''

        # project['organization'] = organization.name

        # TODO: Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        project['status'] = 'Development'

        project['vcs'] = repository['scmId']

        project['homepageURL'] = repository['links']['self'][0]['href']

        # project['downloadURL'] =

        # project['languages'] =

        # project['partners'] = []

        # project['relatedCode'] = []

        # project['reusedCode'] = []

        # date: [object] A date object describing the release.
        #   created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
        #   lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
        #   metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
        # project['date'] = {
        #     'created': repository.pushed_at.isoformat(),
        #     'lastModified': repository.updated_at.isoformat(),
        #     'metadataLastUpdated': '',
        # }

        _prune_dict_null_str(project)

        return project
Handles crafting Code.gov Project for Bitbucket Server repositories