Commit 49e78bec authored by Jérome Perrin's avatar Jérome Perrin

test_result: Support repositories or user ids with .

Because gitlab way of passing project ID as user/project URL-encoded
does not support dots (it would needs to be encoded as %2E) and that
requests rewrites %2F from URL to . (which is probably correct, that's
the RFC), we cannot use encoded paths for when they contain a dot.

To workaround this, when user or repo as a dot, make another API call to
get the numerical project ID and use that instead.

We only do that for projects with a dot because that's an extra request
we'd like to avoid.

/reviewed-on nexedi/erp5!962
parent 0024e8ac
......@@ -64,10 +64,23 @@ class GitlabRESTConnector(XMLObject):
https://docs.gitlab.com/ce/api/commits.html#post-the-build-status-to-a-commit
"""
project_id = self._getRepositoryIdFromRepositoryUrl(repository_url)
if '.' in project_id:
url = urljoin(self.getUrlString(), "projects/{id}".format(id=project_id))
self.logger.debug(
"Project id encoded as %s has a dot in the name,"
" we need to lookup id at %s",
project_id,
url)
project_id = requests.get(
url,
headers={"PRIVATE-TOKEN": self.getToken()},
timeout=5).json()['id']
url = urljoin(
self.getUrlString(),
"projects/{id}/statuses/{sha}".format(
id=self._getRepositoryIdFromRepositoryUrl(repository_url),
id=project_id,
sha=commit_sha
)
)
......
......@@ -1590,3 +1590,32 @@ class TestGitlabRESTConnectorInterface(ERP5TypeTestCase):
"revision": "dc7b6e2e85e9434a97694a698884b057b7d30286",
},
test_suite_data['repository_dict']['foo-bar'])
def test_project_with_dot_in_name(self):
# gitlab API doc[1] explain that repository name needs to be URL-encoded
# so that user/repo becomes user%2Frepo, but what they don't say is that
# if repo name contain dot, this dot also needs to be encoded, so user/re.po
# must become user%2Fre%2Epo
# This is not compliant with how RFC 3986 says . is "unreserved", but it's
# the way gitlab (seen on 8.17.8) works.
# Anyway requests does not let us GET with %2E in URL, it turns them back
# into .
# So when we have a project name or user name with a dot, we make a first
# API call to get the numerical project ID
# 1: https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
self.test_suite.test_repo.setGitUrl('https://lab.example.com/nexedi/test.repo.git',)
self.post_commit_status_url = \
'https://lab.example.com/api/v4/projects/1234/statuses/cc4c79c003f7cfe0bfcbc7b302eac988110c96ae'
with responses.RequestsMock() as rsps:
rsps.add(
responses.GET,
'https://lab.example.com/api/v4/projects/nexedi%2Ftest.repo',
json={'id': 1234})
rsps.add_callback(
responses.POST,
self.post_commit_status_url,
self._response_callback('running'))
self.test_result.start()
self.tic()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment