def create_webhooks()

in webhook-app/webhook_creator.py [0:0]


def create_webhooks():
    """Auto-creates webhooks

    * Gets all open issues assigned to the bot.
    * Checks to see if the issue is title 'add webhook'.
    * Checks the creator and the bot are both admins.
    * Creates the hook and leaves a comment.
    """

    gh = github_helper.get_client()
    issues = gh.issues(filter='assigned', state='open')

    for issue in issues:
        # Does someone want us to add the webhook?
        if issue.title.lower() not in ('add webhook', 'create webhook'):
            return

        logging.info('Processing issue {}'.format(issue.url))

        # Make sure the user who filed the issue is an admin.
        permission = github_helper.get_permission(
            gh, issue.repository[0],
            issue.repository[1],
            issue.user.login)

        if permission != 'admin':
            logging.info(
                'Not installing webhook because {} is not an '
                'admin.'.format(issue.user.login))
            return

        # Make sure we're an admin.
        repo = gh.repository(*issue.repository)

        if not repo.permissions['admin']:
            logging.info(
                'Not installing hook because depbot is not an admin')
            # TODO: leave a comment?
            return

        # Create the webhook.
        try:
            webhook_helper.create_webhook(repo.owner, repo.name)
            issue.create_comment('Webhook added!')
        except github3.exceptions.UnprocessableEntity:
            # Webhook already exists
            logging.info('Hook already existed.')
            issue.create_comment('Webhook is already here!')

        issue.close()