Commit d6ac68a2 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

travis

parent 9d25872b
import logging
import os
from django.core.management.base import BaseCommand
import requests
from rainboard.models import Repo, Namespace, Forge, SOURCES, Project
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)
# Generated by Django 2.0.1 on 2018-02-05 15:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0013_branch_repo'),
]
operations = [
migrations.AddField(
model_name='repo',
name='travis_id',
field=models.PositiveIntegerField(blank=True, null=True),
),
]
......@@ -65,6 +65,8 @@ class Forge(Links, NamedModel):
'application/vnd.github.drax-preview+json'},
SOURCES.gitlab: {'Private-Token': self.token},
SOURCES.redmine: {'X-Redmine-API-Key': self.token},
SOURCES.travis: {'Authorization': f'token {self.token}',
'TRAVIS-API-Version': '3'},
}[self.source]
def api_url(self):
......@@ -72,6 +74,7 @@ class Forge(Links, NamedModel):
SOURCES.github: 'https://api.github.com',
SOURCES.gitlab: f'{self.url}/api/v4',
SOURCES.redmine: self.url,
SOURCES.travis: 'https://api.travis-ci.org',
}[self.source]
def get_projects(self):
......@@ -246,6 +249,7 @@ class Repo(TimeStampedModel):
repo_id = models.PositiveIntegerField()
forked_from = models.PositiveIntegerField(blank=True, null=True)
clone_url = models.URLField(max_length=200, blank=True, null=True)
travis_id = models.PositiveIntegerField(blank=True, null=True)
# TODO gitlab:
# description = models.TextField()
# created_at = models.DateTimeField()
......
......@@ -4,7 +4,7 @@ import re
from django.utils.safestring import mark_safe
SOURCES = IntEnum('Sources', 'github gitlab redmine robotpkg')
SOURCES = IntEnum('Sources', 'github gitlab redmine robotpkg travis')
TARGETS = IntEnum('Targets', '12.04 14.04 16.04 dubnium')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment