Skip to content

Commit 5ac9d19

Browse files
committed
Add an attribute to indicate task state
1 parent 07d6d51 commit 5ac9d19

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

data/management/commands/fetch_deployed_data.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import logging
22
import os.path
3-
3+
from ruamel.yaml import YAML
44
import requests
55

66
from django.core.management.base import BaseCommand
77

88
from community.git import get_deploy_url, get_upstream_deploy_url
9+
from gci.students import cleanse_instances
10+
from gci.task import cleanse_tasks
911

1012

1113
class Command(BaseCommand):
1214
help = 'Fetch old data'
15+
yaml = YAML()
16+
tasks = {}
17+
instances = {}
1318

1419
def add_arguments(self, parser):
1520
parser.add_argument('output_dir', nargs='?', type=str)
@@ -55,3 +60,26 @@ def handle(self, *args, **options):
5560
filename = os.path.basename(filename)
5661
with open(os.path.join(output_dir, filename), 'wb') as f:
5762
f.write(r.content)
63+
64+
tokens = {
65+
'GH_TOKEN': os.environ.get('GH_TOKEN'),
66+
'GL_TOKEN': os.environ.get('GL_TOKEN')
67+
}
68+
69+
if filename == 'tasks.yaml':
70+
tasks = self.get_data(output_dir, filename)
71+
self.tasks = cleanse_tasks(tasks, tokens)
72+
self.put_data(output_dir, filename, self.tasks)
73+
74+
if filename == 'instances.yaml':
75+
instances = self.get_data(output_dir, filename)
76+
self.instances = cleanse_instances(instances, self.tasks)
77+
self.put_data(output_dir, filename, self.instances)
78+
79+
def get_data(self, output_dir, filename):
80+
with open(os.path.join(output_dir, filename), 'r') as f:
81+
return self.yaml.load(f)
82+
83+
def put_data(self, output_dir, filename, data):
84+
with open(os.path.join(output_dir, filename), 'w') as f:
85+
self.yaml.dump(data, f)

gci/management/commands/cleanse_gci_task_data.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ def handle(self, *args, **options):
2222

2323
yaml = YAML()
2424

25+
tokens = {
26+
'GH_TOKEN': os.environ.get('GH_TOKEN'),
27+
'GL_TOKEN': os.environ.get('GL_TOKEN')
28+
}
29+
2530
with open(os.path.join(input_dir, 'tasks.yaml'), 'r') as f:
2631
tasks = yaml.load(f)
2732

2833
with open(os.path.join(input_dir, 'instances.yaml'), 'r') as f:
2934
instances = yaml.load(f)
3035

31-
tasks = cleanse_tasks(tasks)
36+
tasks = cleanse_tasks(tasks, tokens)
3237
instances = cleanse_instances(instances, tasks)
3338

3439
if not os.path.exists(output_dir):

gci/students.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import re
21
import logging
2+
import re
33

44
from .client import GCIAPIClient
5-
65
from .config import get_api_key, load_cache
76
from .gitorg import get_issue
87
from .task import beginner_tasks, get_task
@@ -83,6 +82,8 @@ def cleanse_instances(instances, tasks):
8382
for instance_id, instance
8483
in instances.items()
8584
if instance['status'] not in PRIVATE_INSTANCE_STATUSES
85+
and tasks[instance['task_definition_id']]
86+
.__contains__('state').__eq__('COMPLETED')
8687
and instance['task_definition_id'] in tasks
8788
and instance['task_definition_id'] not in beginner_tasks(tasks)
8889
)

gci/task.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import requests
2+
13
from .config import load_cache
24

35
_tasks = None
@@ -81,12 +83,42 @@ def beginner_tasks(tasks):
8183

8284
def strip_mentors(tasks):
8385
for task in tasks.values():
84-
del task['mentors']
86+
if task.__contains__('mentors'):
87+
del task['mentors']
88+
8589

90+
def cleanse_tasks(tasks, tokens):
91+
for task in tasks.values():
92+
if task['max_instances'] == 1 \
93+
and str(task['external_url']).__contains__('issues/'):
94+
task['state'] = get_task_state(task['external_url'], tokens)
8695

87-
def cleanse_tasks(tasks):
8896
cleansed_tasks = published_tasks(tasks)
8997

9098
strip_mentors(tasks)
9199

92100
return cleansed_tasks
101+
102+
103+
def get_task_state(task_url, tokens):
104+
if task_url.__contains__('github'):
105+
task_url = task_url.replace('github.com', 'api.github.com/repos')
106+
headers = {'Authorization': 'token {}'.format(tokens['GH_TOKEN'])}
107+
else:
108+
issue_id = task_url.split('issues/')[1]
109+
project_id = ((task_url.split('https://gitlab.com/')
110+
[1]).split('/issues')[0]).replace('/', '%2F')
111+
task_url = 'https://gitlab.com/api/v4/projects/{}/issues/{}'.format(
112+
project_id, issue_id)
113+
headers = {}
114+
print(task_url)
115+
task_data = requests.get(task_url, headers=headers).json()
116+
print(task_data)
117+
if task_data['state'] == 'closed':
118+
task_state = 'COMPLETED'
119+
elif task_data['state'] == 'open' and len(task_data['assignees']) > 0:
120+
task_state = 'CLAIMED'
121+
else:
122+
task_state = 'AVAILABLE'
123+
124+
return task_state

0 commit comments

Comments
 (0)