-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathviews.py
More file actions
87 lines (77 loc) · 2.92 KB
/
Copy pathviews.py
File metadata and controls
87 lines (77 loc) · 2.92 KB
1
2
3
4
5
6
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
#
# Copyright (C) GrimoireLab Contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.conf import settings
from django.db import IntegrityError, transaction
from .models import Repository
from ..scheduler.scheduler import schedule_task
from ..permissions import check_permissions
@api_view(['POST'])
@check_permissions(['datasources.add_repository'])
def add_repository(request):
"""Create a Repository and start a Task to fetch items
The body should contain a JSON similar to:
{
'uri': "<repository_uri>",
'datasource_type': 'git',
'datasource_category': 'commit',
'scheduler': {
'job_interval': 86400,
'job_max_retries': 3,
}
}
"""
data = request.data
# Get POST data
job_interval = settings.GRIMOIRELAB_JOB_INTERVAL
job_max_retries = settings.GRIMOIRELAB_JOB_MAX_RETRIES
if 'scheduler' in data:
job_interval = data['scheduler'].get('job_interval', job_interval)
job_max_retries = data['scheduler'].get('job_max_retries', job_max_retries)
uri = data.get('uri')
datasource_type = data.get('datasource_type')
datasource_category = data.get('datasource_category')
if not uri or not datasource_type or not datasource_category:
return Response({"error": "Missing parameters"}, status=400)
# Create the task and the repository
try:
with transaction.atomic():
repository = Repository.objects.create(uri=uri,
datasource_type=datasource_type,
datasource_category=datasource_category)
except IntegrityError:
return Response({"error": "Repository already exists"}, status=405)
task_args = {
'uri': data['uri']
}
task = schedule_task(
'eventizer', task_args,
datasource_type=datasource_type,
datasource_category=datasource_category,
job_interval=job_interval,
job_max_retries=job_max_retries
)
repository.task = task
repository.save()
response = {
'status': 'ok',
'task_id': repository.task.uuid,
'message': f"Repository {uri} added correctly"
}
return Response(response)