diff --git a/config/default.py b/config/default.py index adb9b845..36d86b6e 100644 --- a/config/default.py +++ b/config/default.py @@ -46,8 +46,10 @@ PB_STUDY_DETAILS_URL = environ.get('PB_STUDY_DETAILS_URL', default=PB_BASE_URL + LDAP_URL = environ.get('LDAP_URL', default="ldap.virginia.edu").strip('/') # No trailing slash or http:// LDAP_TIMEOUT_SEC = int(environ.get('LDAP_TIMEOUT_SEC', default=1)) -# Github token +# Github settings GITHUB_TOKEN = environ.get('GITHUB_TOKEN', None) +GITHUB_REPO = environ.get('GITHUB_REPO', None) +TARGET_BRANCH = environ.get('TARGET_BRANCH', None) # Email configuration DEFAULT_SENDER = 'askresearch@virginia.edu' diff --git a/crc/services/file_service.py b/crc/services/file_service.py index ea2ef563..8810f6ad 100644 --- a/crc/services/file_service.py +++ b/crc/services/file_service.py @@ -2,7 +2,7 @@ import hashlib import json import os from datetime import datetime -from github import Github, UnknownObjectException +from github import Github, GithubObject, UnknownObjectException from uuid import UUID from lxml import etree @@ -336,10 +336,20 @@ class FileService(object): app.logger.info("Failed to delete file, so archiving it instead. %i, due to %s" % (file_id, str(ie))) @staticmethod - def update_from_github(file_ids): + def get_repo_branches(): gh_token = app.config['GITHUB_TOKEN'] + github_repo = app.config['GITHUB_REPO'] _github = Github(gh_token) - repo = _github.get_user().get_repo('crispy-fiesta') + repo = _github.get_user().get_repo(github_repo) + branches = [branch.name for branch in repo.get_branches()] + return branches + + @staticmethod + def update_from_github(file_ids, source_target=GithubObject.NotSet): + gh_token = app.config['GITHUB_TOKEN'] + github_repo = app.config['GITHUB_REPO'] + _github = Github(gh_token) + repo = _github.get_user().get_repo(github_repo) for file_id in file_ids: file_data_model = FileDataModel.query.filter_by( @@ -348,10 +358,9 @@ class FileService(object): desc(FileDataModel.version) ).first() try: - repo_file = repo.get_contents(file_data_model.file_model.name) + repo_file = repo.get_contents(file_data_model.file_model.name, ref=source_target) except UnknownObjectException: - # TODO: Add message indicating file is not in the repo - pass + return {'error': 'Attempted to update from repository but file was not present'} else: file_data_model.data = repo_file.decoded_content session.add(file_data_model) @@ -359,26 +368,29 @@ class FileService(object): @staticmethod def publish_to_github(file_ids): + target_branch = app.config['TARGET_BRANCH'] if app.config['TARGET_BRANCH'] else GithubObject.NotSet gh_token = app.config['GITHUB_TOKEN'] + github_repo = app.config['GITHUB_REPO'] _github = Github(gh_token) - repo = _github.get_user().get_repo('crispy-fiesta') - + repo = _github.get_user().get_repo(github_repo) for file_id in file_ids: file_data_model = FileDataModel.query.filter_by(file_model_id=file_id).first() try: - repo_file = repo.get_contents(file_data_model.file_model.name) + repo_file = repo.get_contents(file_data_model.file_model.name, ref=target_branch) except UnknownObjectException: repo.create_file( path=file_data_model.file_model.name, message=f'Creating {file_data_model.file_model.name}', - content=file_data_model.data + content=file_data_model.data, + branch=target_branch ) return {'created': True} else: updated = repo.update_file( path=repo_file.path, message=f'Updating {file_data_model.file_model.name}', - content=file_data_model.data, - sha=repo_file.sha + content=file_data_model.data + b'brah-model', + sha=repo_file.sha, + branch=target_branch ) return {'updated': True} diff --git a/tests/files/test_file_service.py b/tests/files/test_file_service.py index e32f6bfb..339943d4 100644 --- a/tests/files/test_file_service.py +++ b/tests/files/test_file_service.py @@ -14,9 +14,9 @@ class FakeGithubCreates(Mock): class FakeUser(Mock): def get_repo(var, name): class FakeRepo(Mock): - def get_contents(var, filename): + def get_contents(var, filename, ref): raise UnknownObjectException(status='Failure', data='Failed data') - def update_file(var, path, message, content, sha): + def update_file(var, path, message, content, sha, branch): pass return FakeRepo() return FakeUser() @@ -27,14 +27,22 @@ class FakeGithub(Mock): class FakeUser(Mock): def get_repo(var, name): class FakeRepo(Mock): - def get_contents(var, filename): + def get_contents(var, filename, ref): fake_file = Mock() fake_file.decoded_content = b'Some bytes' fake_file.path = '/el/path/' fake_file.data = 'Serious data' fake_file.sha = 'Sha' return fake_file - def update_file(var, path, message, content, sha): + def get_branches(var): + branch1 = Mock() + branch1.name = 'branch1' + branch2 = Mock() + branch2.name = 'branch2' + master = Mock() + master.name = 'master' + return [branch1, branch2, master] + def update_file(var, path, message, content, sha, branch): pass return FakeRepo() return FakeUser() @@ -198,3 +206,11 @@ class TestFileService(BaseTest): result = FileService.publish_to_github([file_model.id]) self.assertEqual(result['updated'], True) + + @patch('crc.services.file_service.Github') + def test_get_repo_branches(self, mock_github): + mock_github.return_value = FakeGithub() + + branches = FileService.get_repo_branches() + + self.assertIsInstance(branches, list)