From 5ac9d198ef9380e6292f44d375d46567079bb316 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 --- .../commands/fetch_deployed_data.py | 30 +++++++++++++++- .../commands/cleanse_gci_task_data.py | 7 +++- gci/students.py | 5 +-- gci/task.py | 36 +++++++++++++++++-- 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/data/management/commands/fetch_deployed_data.py b/data/management/commands/fetch_deployed_data.py index 2ffde7fd..588fbe58 100644 --- a/data/management/commands/fetch_deployed_data.py +++ b/data/management/commands/fetch_deployed_data.py @@ -1,15 +1,20 @@ import logging import os.path - +from ruamel.yaml import YAML import requests from django.core.management.base import BaseCommand from community.git import get_deploy_url, get_upstream_deploy_url +from gci.students import cleanse_instances +from gci.task import cleanse_tasks class Command(BaseCommand): help = 'Fetch old data' + yaml = YAML() + tasks = {} + instances = {} def add_arguments(self, parser): parser.add_argument('output_dir', nargs='?', type=str) @@ -55,3 +60,26 @@ def handle(self, *args, **options): filename = os.path.basename(filename) with open(os.path.join(output_dir, filename), 'wb') as f: f.write(r.content) + + tokens = { + 'GH_TOKEN': os.environ.get('GH_TOKEN'), + 'GL_TOKEN': os.environ.get('GL_TOKEN') + } + + if filename == 'tasks.yaml': + tasks = self.get_data(output_dir, filename) + self.tasks = cleanse_tasks(tasks, tokens) + self.put_data(output_dir, filename, self.tasks) + + if filename == 'instances.yaml': + instances = self.get_data(output_dir, filename) + self.instances = cleanse_instances(instances, self.tasks) + self.put_data(output_dir, filename, self.instances) + + def get_data(self, output_dir, filename): + with open(os.path.join(output_dir, filename), 'r') as f: + return self.yaml.load(f) + + def put_data(self, output_dir, filename, data): + with open(os.path.join(output_dir, filename), 'w') as f: + self.yaml.dump(data, f) diff --git a/gci/management/commands/cleanse_gci_task_data.py b/gci/management/commands/cleanse_gci_task_data.py index 7da5a168..4775f6f8 100644 --- a/gci/management/commands/cleanse_gci_task_data.py +++ b/gci/management/commands/cleanse_gci_task_data.py @@ -22,13 +22,18 @@ def handle(self, *args, **options): yaml = YAML() + tokens = { + 'GH_TOKEN': os.environ.get('GH_TOKEN'), + 'GL_TOKEN': os.environ.get('GL_TOKEN') + } + with open(os.path.join(input_dir, 'tasks.yaml'), 'r') as f: tasks = yaml.load(f) with open(os.path.join(input_dir, 'instances.yaml'), 'r') as f: instances = yaml.load(f) - tasks = cleanse_tasks(tasks) + tasks = cleanse_tasks(tasks, tokens) instances = cleanse_instances(instances, tasks) if not os.path.exists(output_dir): diff --git a/gci/students.py b/gci/students.py index 077a960c..a9af727c 100644 --- a/gci/students.py +++ b/gci/students.py @@ -1,8 +1,7 @@ -import re import logging +import re from .client import GCIAPIClient - from .config import get_api_key, load_cache from .gitorg import get_issue from .task import beginner_tasks, get_task @@ -83,6 +82,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..89a6d0dc 100644 --- a/gci/task.py +++ b/gci/task.py @@ -1,3 +1,5 @@ +import requests + from .config import load_cache _tasks = None @@ -81,12 +83,42 @@ 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): + 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) -def cleanse_tasks(tasks): 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 = {} + print(task_url) + task_data = requests.get(task_url, headers=headers).json() + print(task_data) + 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