populate.py 1.58 KB
Newer Older
Guilhem Saurel's avatar
Guilhem Saurel committed
1
2
import logging

Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
3
4
5
6
from django.core.management.base import BaseCommand

import requests

Guilhem Saurel's avatar
Guilhem Saurel committed
7
from rainboard.models import Forge, License, Repo, Project
Guilhem Saurel's avatar
Guilhem Saurel committed
8

Guilhem Saurel's avatar
Guilhem Saurel committed
9
LICENSES = 'https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json'
Guilhem Saurel's avatar
Guilhem Saurel committed
10
logger = logging.getLogger('rainboard.management.populate')
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
11

Guilhem Saurel's avatar
Guilhem Saurel committed
12

Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
13
14
15
16
class Command(BaseCommand):
    help = 'populates licenses, projets, namespaces and repos from forges'

    def handle(self, *args, **options):
Guilhem Saurel's avatar
Guilhem Saurel committed
17
        # github = Forge.objects.get(name='Github')
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
18

Guilhem Saurel's avatar
Guilhem Saurel committed
19
        logger.info(f'updating licenses')
Guilhem Saurel's avatar
Guilhem Saurel committed
20
        for data in requests.get(LICENSES).json()['licenses']:
Guilhem Saurel's avatar
Guilhem Saurel committed
21
            logger.info(f' updating license {data["name"]}')
Guilhem Saurel's avatar
Guilhem Saurel committed
22
23
24
25
26
            License.objects.get_or_create(spdx_id=data['licenseId'],
                                          defaults={'name': data['name'], 'url': data['detailsUrl']})
        # for data in requests.get(f'{github.api_url()}/licenses', headers=github.headers()).json():
            # logger.info(f' updating license {data["name"]}')
            # License.objects.get_or_create(spdx_id=data['spdx_id'],
Guilhem Saurel's avatar
Guilhem Saurel committed
27
            #                               defaults={key: data[key] for key in ['name', 'url']})
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
28

Guilhem Saurel's avatar
Guilhem Saurel committed
29
        logger.info(f'updating forges')
Guilhem Saurel's avatar
Guilhem Saurel committed
30
        for forge in Forge.objects.order_by('source'):
Guilhem Saurel's avatar
Guilhem Saurel committed
31
            logger.info(f' updating {forge}')
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
32
            forge.get_projects()
Guilhem Saurel's avatar
Guilhem Saurel committed
33
34
35
36
37

        logger.info(f'updating repos')
        for repo in Repo.objects.all():
            logger.info(f' updating {repo}')
            repo.api_update()
Guilhem Saurel's avatar
Guilhem Saurel committed
38
39
40

        logger.info(f'removing unwanted projects')
        Project.objects.filter(main_namespace__group=False).delete()