From 8ece79aca2e54a159814ae56d510a479f196e8df Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Tue, 10 Jul 2018 10:01:02 +0100 Subject: [PATCH] Basic fixtures for testing with GitHub repos Add simple fixtures for creating GitHub repos and forks. Change-Id: I4ad30c26db60680166b88b434a521c4e885381e8 --- fixtures_git/github.py | 125 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 fixtures_git/github.py diff --git a/fixtures_git/github.py b/fixtures_git/github.py new file mode 100644 index 0000000..f83b651 --- /dev/null +++ b/fixtures_git/github.py @@ -0,0 +1,125 @@ +# Copyright (c) 2018 Hewlett Packard Enterprise Development Company LP +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os +import tempfile +import time + +import fixtures +import github3 as github + +try: + import urlparse as parse +except ImportError: + from urllib import parse + +TEST_REPO_DESC = "########## Auto-generated test repository ##########" + + +class GithubRepoFixture(fixtures.Fixture): + """ + Fixture to create a new repo in GitHub and remove once finished. + """ + + default_repo_name_template = 'workflow-test-XXXXXX' + + def __init__(self, owner, token, url="https://github.com", + name_template=None): + + super(GithubRepoFixture, self).__init__() + + self.owner = owner + self.token = token + self.name_template = name_template or self.default_repo_name_template + + self.repo = None + self.repo_name = None + if parse.urlparse(url).netloc == "github.com": + github_login = github.login + else: + github_login = github.enterprise_login + self.github = github_login(token=token, url=url) + + # try an auth'ed request to make sure we have a valid token + # note this requires the token to have read on user + self.me = self.github.me() + + def _setUp(self): + template_parts = self.name_template.split('XXXXXX') + prefix = template_parts[0] + suffix = template_parts[-1] + tfile = tempfile.NamedTemporaryFile( + suffix=suffix, prefix=prefix, delete=False) + self.addCleanup(os.remove, tfile.name) + + self.repo_name = os.path.basename(tfile.name) + + self.addCleanup(self._delete_repo) + + org = self.github.organization(self.owner) + self.repo = org.create_repository( + name=self.repo_name, + description=TEST_REPO_DESC, + has_issues=False, + has_wiki=False, + auto_init=True, + ) + + def _delete_repo(self): + if self.repo is not None: + self.repo.delete() + elif self.repo_name is not None: + repo = self.github.repository(self.owner, self.repo_name) + if repo: + repo.delete() + + +class GithubForkedRepoFixture(fixtures.Fixture): + """ + Fixture to create and delete a fork of the given repo in the + default GitHub org of the token user + """ + def __init__(self, src_repo, token, url="https://github.com"): + + super(GithubForkedRepoFixture, self).__init__() + + self.src_repo = src_repo + self.token = token + + self.repo = None + if parse.urlparse(url).netloc == "github.com": + github_login = github.login + else: + github_login = github.enterprise_login + self.github = github_login(token=token, url=url) + + # try an auth'ed request to make sure we have a valid token + # note this requires the token to have read on user + self.me = self.github.me() + + def _setUp(self): + owner, repo_name = self.src_repo.split('/') + upstream_repo = self.github.repository(owner, repo_name) + + self.addCleanup(self._delete_repo) + self.repo = upstream_repo.create_fork() + # wait for the fork to be available + while self.github.repository(self.me, repo_name) is None: + time.sleep(2) + + def _delete_repo(self): + repo_name = self.src_repo.split('/')[-1] + repo = self.github.repository((self.github.me()).login, repo_name) + if repo and repo.description == TEST_REPO_DESC: + repo.delete()