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

logging, api_update_gitlab

parent 4e81e33a
......@@ -128,3 +128,26 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
SITE_ID = 1
DJANGO_TABLES2_TEMPLATE = 'rainboard/tables.html'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/tmp/dashboard.log',
},
},
'loggers': {
'': {
'handlers': ['console', 'file'],
'propagate': True,
'level': 'INFO',
},
},
}
import logging
from django.core.management.base import BaseCommand
from rainboard.models import Forge, License
from rainboard.models import Forge, License, Repo
import requests
logger = logging.getLogger('rainboard.management.populate')
class Command(BaseCommand):
help = 'populates licenses, projets, namespaces and repos from forges'
......@@ -11,9 +14,18 @@ class Command(BaseCommand):
def handle(self, *args, **options):
github = Forge.objects.get(name='Github')
logger.info(f'updating licenses')
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(github_key=data['key'],
**{key: data[key] for key in ['name', 'spdx_id', 'url']})
defaults={key: data[key] for key in ['name', 'spdx_id', 'url']})
logger.info(f'updating forges')
for forge in Forge.objects.all():
logger.info(f' updating {forge}')
forge.get_projects()
logger.info(f'updating repos')
for repo in Repo.objects.all():
logger.info(f' updating {repo}')
repo.api_update()
......@@ -136,16 +136,40 @@ class Repo(TimeStampedModel):
open_pr = models.PositiveSmallIntegerField(blank=True, null=True)
repo_id = models.PositiveIntegerField()
forked_from = models.PositiveIntegerField(blank=True, null=True)
# TODO gitlab:
# description = models.TextField()
# created_at = models.DateTimeField()
# last_activity_at = models.DateTimeField()
def __str__(self):
return self.name
def api_url(self):
if self.forge.source == SOURCES.github:
return f'{self.forge.api_url()}/repos/{self.namespace.slug}/{self.slug}'
if self.forge.source == SOURCES.redmine:
return f'{self.forge.api_url()}/projects/{self.repo_id}.json'
if self.forge.source == SOURCES.gitlab:
return f'{self.forge.api_url()}/projects/{self.repo_id}'
def api_data(self, url=''):
return requests.get(self.api_url() + url, verify=self.forge.verify, headers=self.forge.headers()).json()
def api_update(self):
if self.forge.source == SOURCES.gitlab:
self.api_update_gitlab(self.api_data())
def api_update_gitlab(self, data):
# TODO Missing: license, homepage, open_pr
self.name = data['name']
self.slug = data['path']
self.url = data['web_url']
self.open_issues = data['open_issues_count']
self.default_branch = data['default_branch']
if 'forked_from_project' in data:
self.forked_from = data['forked_from_project']['id']
self.save()
class Commit(NamedModel, TimeStampedModel):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
......
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