from celery import signals
from goodtablesio import models
from goodtablesio.tasks import validate
from goodtablesio.integrations.github.utils.status import set_commit_status


# Module API

@signals.task_postrun.connect(sender=validate)
def post_task_handler(**kwargs):

    job = kwargs['retval']
    if isinstance(kwargs['retval'], Exception):
        job_id = kwargs['kwargs']['job_id']
        job = models.job.get(job_id)

    if job.get('integration_name') != 'github':
        return

    task_state = kwargs['state']

    status = job['status']
    conf = job['conf']

    if conf:

        if task_state == 'SUCCESS' and status != 'running':
            github_status = status
        else:
            github_status = 'error'

        set_commit_status(
           github_status,
           owner=conf['owner'],
           repo=conf['name'],
           sha=conf['sha'],
           job_id=job['id'])

@@ -20,16 +20,17 @@
 signals%0A
+%0A
 from goo
@@ -76,16 +76,25 @@
 io.tasks
+.validate
  import 
@@ -184,22 +184,8 @@
 s%0A%0A%0A
-# Module API%0A%0A
 @sig
@@ -834,20 +834,20 @@
 o=conf%5B'
-name
+repo
 '%5D,%0A    

Fix Github status signal