[IMP] repo creation error handling + warnings

* The repo would only be registered at the very end of the creation,
  meaning an error *during* the repo creation (e.g. while uploading the
  first blob or setting up webhooks) would leave the repository
  undeleted. Register the repository as soon as we know it was
  created, in order to correctly dispose of it afterwards.
* Migrate logging.warning call to warnings.warn on repository deletion
  failure: pytest will print warnings during its reporting, not so for
  log warnings (?)
This commit is contained in:
Xavier Morel 2021-04-06 10:52:51 +02:00 committed by xmo-odoo
parent b55e38ae2d
commit e542dfc852

View File

@ -54,6 +54,7 @@ import subprocess
import sys import sys
import time import time
import uuid import uuid
import warnings
import xmlrpc.client import xmlrpc.client
from contextlib import closing from contextlib import closing
@ -383,6 +384,7 @@ def make_repo(capsys, request, config, tunnel, users):
'allow_rebase_merge': False, 'allow_rebase_merge': False,
}) })
r.raise_for_status() r.raise_for_status()
repo = Repo(github, fullname, repos)
# create webhook # create webhook
github.post('{}/hooks'.format(repo_url), json={ github.post('{}/hooks'.format(repo_url), json={
@ -401,8 +403,9 @@ def make_repo(capsys, request, config, tunnel, users):
'content': base64.b64encode(b'whee').decode('ascii'), 'content': base64.b64encode(b'whee').decode('ascii'),
'branch': 'garbage_%s' % uuid.uuid4() 'branch': 'garbage_%s' % uuid.uuid4()
}).raise_for_status() }).raise_for_status()
# try to unwatch repo, doesn't actually work
return Repo(github, fullname, repos) repo.unsubscribe()
return repo
yield repomaker yield repomaker
@ -433,9 +436,6 @@ class Repo:
self.hook = False self.hook = False
repos.append(self) repos.append(self)
# unwatch repo
self.unsubscribe()
@property @property
def owner(self): def owner(self):
return self.name.split('/')[0] return self.name.split('/')[0]
@ -470,7 +470,7 @@ class Repo:
def delete(self): def delete(self):
r = self._session.delete('https://api.github.com/repos/{}'.format(self.name)) r = self._session.delete('https://api.github.com/repos/{}'.format(self.name))
if r.status_code != 204: if r.status_code != 204:
logging.getLogger(__name__).warning("Unable to delete repository %s", self.name) warnings.warn("Unable to delete repository %s (HTTP %s)" % (self.name, r.status_code))
def set_secret(self, secret): def set_secret(self, secret):
assert self.hook assert self.hook