travis.py 1.9 KB
Newer Older
Guilhem Saurel's avatar
travis  
Guilhem Saurel committed
1
2
3
4
5
import logging
import os

from django.core.management.base import BaseCommand

Guilhem Saurel's avatar
Guilhem Saurel committed
6
from rainboard.models import SOURCES, Forge, Namespace, Project, Repo
Guilhem Saurel's avatar
travis  
Guilhem Saurel committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

logger = logging.getLogger('rainboard.management.travis')

GITHUB = Forge.objects.get(source=SOURCES.github)


def update_travis_id(data):
    namespace, project = data['slug'].lower().split('/')
    namespace = Namespace.objects.get(slug=namespace)
    project, _ = Project.objects.get_or_create(name=data['name'], defaults={'main_namespace': namespace})
    repo, created = Repo.objects.get_or_create(forge=GITHUB, namespace=namespace, project=project,
                                               defaults={'name': data['name'], 'repo_id': 0})
    if created:
        repo.api_update()

    repo.travis_id = data['id']
    repo.save()


class Command(BaseCommand):
    help = 'Gets Repo.travis_id'

    def handle(self, *args, **options):
        logger.info('Adding Travis Forge if needed')
        travis, created = Forge.objects.get_or_create(source=SOURCES.travis, defaults={
            'name': 'Travis', 'url': 'https://travis-ci.org/', 'token': os.getenv('TRAVIS_TOKEN')
        })

        logger.info('Gettings travis_id')
        for namespace in Namespace.objects.all():
            logger.info(f' Gettings travis_id for {namespace}')
            next_url = f'/owner/{namespace.slug}/repos'
            data = travis.api_data(next_url)
            if not data or data['@type'] != 'repositories':
                continue
            while True:
                for repository in data['repositories']:
                    if repository['active']:
                        logger.info('  update travis id for ' + repository['slug'])
                        update_travis_id(repository)
                if data['@pagination']['next'] is None:
                    break
                next_url = data['@pagination']['next']['@href']
                data = travis.api_data(next_url)