From 21df8507f45d9cb03b6499f22d0c32ee4f160867 Mon Sep 17 00:00:00 2001 From: KVGarg Date: Thu, 7 Feb 2019 20:56:07 +0530 Subject: [PATCH] Add an attribute to indicate task state --- gci/students.py | 2 ++ gci/task.py | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gci/students.py b/gci/students.py index 077a960c..19ed74db 100644 --- a/gci/students.py +++ b/gci/students.py @@ -83,6 +83,8 @@ def cleanse_instances(instances, tasks): for instance_id, instance in instances.items() if instance['status'] not in PRIVATE_INSTANCE_STATUSES + and tasks[instance['task_definition_id']] + .__contains__('state').__eq__('COMPLETED') and instance['task_definition_id'] in tasks and instance['task_definition_id'] not in beginner_tasks(tasks) ) diff --git a/gci/task.py b/gci/task.py index cdd627e0..00642c6f 100644 --- a/gci/task.py +++ b/gci/task.py @@ -1,3 +1,6 @@ +import requests +import os + from .config import load_cache _tasks = None @@ -81,12 +84,49 @@ def beginner_tasks(tasks): def strip_mentors(tasks): for task in tasks.values(): - del task['mentors'] + if task.__contains__('mentors'): + del task['mentors'] def cleanse_tasks(tasks): + tokens = { + 'GH_TOKEN': os.environ.get('GH_TOKEN'), + } + + for task in tasks.values(): + if (task['max_instances'] == 1 and + str(task['external_url']).__contains__('issues/')): + task['state'] = get_task_state(task['external_url'], tokens) + cleansed_tasks = published_tasks(tasks) strip_mentors(tasks) return cleansed_tasks + + +def get_task_state(task_url, tokens): + if task_url.__contains__('github'): + task_url = task_url.replace('github.com', 'api.github.com/repos') + headers = {'Authorization': 'token {}'.format(tokens['GH_TOKEN'])} + else: + issue_id = task_url.split('issues/')[1] + project_id = ((task_url.split('https://gitlab.com/') + [1]).split('/issues')[0]).replace('/', '%2F') + task_url = 'https://gitlab.com/api/v4/projects/{}/issues/{}'.format( + project_id, issue_id) + headers = {} + + if tokens['GH_TOKEN']: + task_data = requests.get(task_url, headers=headers).json() + else: + task_data = requests.get(task_url).json() + + if task_data['state'] == 'closed': + task_state = 'COMPLETED' + elif task_data['state'] == 'open' and len(task_data['assignees']) > 0: + task_state = 'CLAIMED' + else: + task_state = 'AVAILABLE' + + return task_state