def addhook(full_name): auth = github.get_session(token=g.user.token) old_hooks = auth.get('/repos/%s/hooks' % full_name) if old_hooks.status_code != 200: logging.error('Repos API reading error for user %s' % g.user.login) flash(_('GitHub API access error, please try again later')) return redirect(url_for('settings.repos') + "#tab_github") exist_id = False if old_hooks.json(): for i in old_hooks.json(): if 'name' in i and i['name'] == 'web': if 'config' in i and 'url' in i['config'] \ and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name): exist_id = i['id'] if exist_id: logging.warn('Delete old webhook for user %s, repo %s and id %s' % (g.user.login, full_name, exist_id)) resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' % {'full_name': full_name, 'id': exist_id}) if resp.status_code != 204: flash(_('Error deleting old webhook, delete if manually or retry')) return redirect(url_for('settings.repos') + "#tab_github") resp = auth.post('/repos/%(full_name)s/hooks' % {'full_name': full_name}, data=json.dumps({ 'name': 'web', 'active': True, 'events': ['push'], 'config': { 'url': current_app.config.get('HOOK_URL').format(id=full_name), 'content_type': 'json', 'secret': signify(full_name) # TODO: sign from name and SECRET. } }) ) if resp.status_code < 300: # no errors, in 2xx range project = Project.query.filter_by( login=g.user.login, full_name=full_name).first() if not project: project = Project( login=g.user.login, full_name=full_name, # TODO: Use actual github clone string used by Github # clone='[email protected]:%s/%s.git' % (g.user.login, full_name), clone='git://github.com/%s.git' % full_name, is_github=True ) project_data = auth.get('/repos/%s' % full_name) if project_data.status_code == 200: project.cache_update(data=project_data.json()) else: flash(_('Repository information update error')) return redirect(url_for('settings.repos') + "#tab_github") project.is_github = True db.session.add(project) db.session.commit() else: logging.error('Web hook registration error for %s' % full_name) flash(_('Repository webhook update error')) return redirect(url_for('settings.repos') + "#tab_github") flash(_('Added webhook for %(name)s.', name=full_name)) project.sync() return redirect(url_for('project.queue', username=project.login, project_id=project.id))