Skip to content

Commit ac64f09

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

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import requests
2+
import os
3+
14
from .config import load_cache
25

36
_tasks = None
@@ -81,12 +84,49 @@ def beginner_tasks(tasks):
8184

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

8690

8791
def cleanse_tasks(tasks):
92+
tokens = {
93+
'GH_TOKEN': os.environ.get('GH_TOKEN'),
94+
}
95+
96+
for task in tasks.values():
97+
if task['max_instances'] == 1 \
98+
and str(task['external_url']).__contains__('issues/'):
99+
task['state'] = get_task_state(task['external_url'], tokens)
100+
88101
cleansed_tasks = published_tasks(tasks)
89102

90103
strip_mentors(tasks)
91104

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

0 commit comments

Comments
 (0)