Skip to content

Commit

Permalink
Add an attribute to indicate task state
Browse files Browse the repository at this point in the history
  • Loading branch information
KVGarg committed Feb 9, 2019
1 parent 07d6d51 commit 7f50856
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
30 changes: 29 additions & 1 deletion data/management/commands/fetch_deployed_data.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
7 changes: 6 additions & 1 deletion gci/management/commands/cleanse_gci_task_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions gci/students.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
)
Expand Down
34 changes: 32 additions & 2 deletions gci/task.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import requests

from .config import load_cache

_tasks = None
Expand Down Expand Up @@ -81,12 +83,40 @@ 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')
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)

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

0 comments on commit 7f50856

Please sign in to comment.