Skip to content
Snippets Groups Projects
0002_github.py 1.26 KiB
Newer Older
Guilhem Saurel's avatar
Guilhem Saurel committed
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-09 15:55
from __future__ import unicode_literals

from django.db import migrations

import requests

GITHUB_API = 'https://api.github.com'
PROJECTS = (
    'Humanoid Path Planner',
    'Stack of Tasks'
)


Guilhem Saurel's avatar
Guilhem Saurel committed
def github(apps, schema_editor):
Guilhem Saurel's avatar
Guilhem Saurel committed
    Project, License, Package, Repo = (apps.get_model('gepetto_packages', model)
                                       for model in ['Project', 'License', 'Package', 'Repo'])
    for project_name in PROJECTS:
        project = Project(name=project_name)
        project.save()
        for data in requests.get(f'{GITHUB_API}/orgs/{project.slug}/repos').json():
Guilhem Saurel's avatar
Guilhem Saurel committed
            package = Package(name=data['name'], project=project, homepage=data['homepage'])
Guilhem Saurel's avatar
Guilhem Saurel committed
            package.save()
Guilhem Saurel's avatar
Guilhem Saurel committed
            repo = Repo(package=package, url=data['html_url'], homepage=data['homepage'],
Guilhem Saurel's avatar
Guilhem Saurel committed
                        default_branch=data['default_branch'], open_issues=data['open_issues'])
Guilhem Saurel's avatar
Guilhem Saurel committed
            # repo.open_pr = len(requests.get(f'{GITHUB_API}/repos/{project.slug}/{package.slug}/pulls').json())
            repo.save()

class Migration(migrations.Migration):

    dependencies = [
        ('gepetto_packages', '0001_initial'),
    ]

    operations = [
Guilhem Saurel's avatar
Guilhem Saurel committed
        migrations.RunPython(github),
Guilhem Saurel's avatar
Guilhem Saurel committed
    ]