def create_hook(self, repo_id, repo_name):
"""Create repository hook."""
config = dict(
url=self.webhook_url,
content_type='json',
secret=current_app.config['GITHUB_SHARED_SECRET'],
insecure_ssl='1' if current_app.config['GITHUB_INSECURE_SSL']
else '0',
)
ghrepo = self.api.repository_with_id(repo_id)
if ghrepo:
try:
hook = ghrepo.create_hook(
'web', # GitHub identifier for webhook service
config,
events=['release'],
)
except github3.GitHubError as e:
# Check if hook is already installed
hook_errors = (m for m in e.errors
if m['code'] == 'custom' and
m['resource'] == 'Hook')
if next(hook_errors, None):
hooks = (h for h in ghrepo.hooks()
if h.config.get('url', '') == config['url'])
hook = next(hooks, None)
if hook:
hook.edit(config=config, events=['release'])
finally:
if hook:
Repository.enable(user_id=self.user_id,
github_id=repo_id,
name=repo_name,
hook=hook.id)
return True
return False
评论列表
文章目录