Skip to content

Commit

Permalink
GCI: Add issue task creator
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Oct 20, 2018
1 parent 382e727 commit 2625404
Show file tree
Hide file tree
Showing 7 changed files with 783 additions and 2 deletions.
1 change: 1 addition & 0 deletions .moban.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ packages:
- unassigned_issues

dependencies:
- git+https://gitlab.com/coala/coala-utils.git
- git-url-parse
- django
- django-distill
Expand Down
138 changes: 138 additions & 0 deletions gci/api_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import logging
import pprint

# Start ignoring KeywordBear
from coala_utils.Question import ask_yes_no
# Stop ignoring

from gci.client import GCIAPIClient
from gci.config import get_api_key
from gci.config import load_yaml
from gci.gitorg import get_issue
from gci.issues import check_task
from gci.issues import get_projects
from gci.issues import lookup_url

_mentor_config = None


def get_mentor_config(filename):
global _mentor_config

if not _mentor_config:
_mentor_config = load_yaml(filename)

return _mentor_config


def allocate_mentors(task, mentor_config_filename):
mentor_config = get_mentor_config(mentor_config_filename)
for tag in task['tags']:
mentors = mentor_config.get(tag)
if mentors:
task['mentors'].extend(mentors)

url = task['external_url']
for key, mentors in mentor_config.items():
if key in url.lower():
task['mentors'].extend(mentors)


def upload_tasks(tasks, mentor_config_filename):
logger = logging.getLogger(__name__ + '.upload_tasks')
client = GCIAPIClient(get_api_key('GCI'))

for task in tasks:
url = task['external_url']

if not check_task(task):
logger.warning(f'task check failed: {url}')
continue

existing_task = lookup_url(url, private=True)
if existing_task:
task_id = existing_task['id']
logger.warning(f'{url} is task {task_id}')
continue

if not task['mentors'] and mentor_config_filename:
allocate_mentors(task, mentor_config_filename)

if task['status'] == 2 and not task['mentors']:
logger.warning(f'{url}: Can not publish without mentors')
continue

pprint.pprint(task)
if task['mentors']:
if ask_yes_no(f'Publish task', 'no'):
task['status'] = 2

if task['status'] != 2:
if not ask_yes_no(f'Create task', 'no'):
continue

client.NewTask(task)

if task['status'] == 2:
print('Task published.')
else:
print('Task created.')


def publish_tasks(tasks, issue_config, mentor_config_filename):
client = GCIAPIClient(get_api_key('GCI'))
projects = dict(get_projects(issue_config, yield_levels=False))

for task_id, task in tasks.items():
name = task['name']

if 'status' in task:
publish_flag = task['status']
if publish_flag == 2:
# TODO: unpublishing tasks if project disabled/blocked
continue
assert publish_flag == 1

if not task['tags']:
continue

url = task['external_url']
if not url:
print(f'{task_id} {name} has no url')
continue
issue = get_issue(url)
if not issue:
print(f'{task_id} {name} {url} not recognised')
continue

project = projects.get(issue.repository.full_name.lower())

if not project:
print(f'{task_id} {url} project not recognised')
continue

disabled = project.get('disabled')
if disabled:
print(f'{task_id} {url} project disabled')
continue

repo_block = project.get('blocked')
if repo_block and issue.number != repo_block:
print(f'{task_id} {url} project blocked by {repo_block}')
continue

if not task['mentors'] and mentor_config_filename:
allocate_mentors(task, mentor_config_filename)

if not task['mentors']:
print(f'{task_id} {url} no mentors available')
continue

task['status'] = 2

pprint.pprint(task)
if not ask_yes_no(f'Publish task {task_id}', 'no'):
continue

print(f'Publishing {task_id} {name} ..')
client.UpdateTask(task_id, task)
9 changes: 7 additions & 2 deletions gci/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'GCI_PRIVATE_DATA_ROOT',
'get_api_key',
'load_cache',
'load_yaml',
)

GCI_PRIVATE_DATA_ROOT = 'private'
Expand All @@ -24,12 +25,16 @@
)


def load_yaml(filename):
with open(filename, 'r') as f:
return ruamel.yaml.load(f, Loader=ruamel.yaml.Loader)


def load_cache(filename, private=False):
if private:
data_dir = GCI_PRIVATE_DATA_ROOT
else:
data_dir = GCI_DATA_DIR

path = os.path.join(data_dir, filename)
with open(path, 'r') as f:
return ruamel.yaml.load(f, Loader=ruamel.yaml.Loader)
return load_yaml(path)
Loading

0 comments on commit 2625404

Please sign in to comment.