Merge pull request #189 from sartography/feature/documents_publishing

Update github routines to use branches
This commit is contained in:
Dan Funk 2020-08-17 15:52:47 -04:00 committed by GitHub
commit 7f60a19d55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 17 deletions

View File

@ -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_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)) LDAP_TIMEOUT_SEC = int(environ.get('LDAP_TIMEOUT_SEC', default=1))
# Github token # Github settings
GITHUB_TOKEN = environ.get('GITHUB_TOKEN', None) GITHUB_TOKEN = environ.get('GITHUB_TOKEN', None)
GITHUB_REPO = environ.get('GITHUB_REPO', None)
TARGET_BRANCH = environ.get('TARGET_BRANCH', None)
# Email configuration # Email configuration
DEFAULT_SENDER = 'askresearch@virginia.edu' DEFAULT_SENDER = 'askresearch@virginia.edu'

View File

@ -2,7 +2,7 @@ import hashlib
import json import json
import os import os
from datetime import datetime from datetime import datetime
from github import Github, UnknownObjectException from github import Github, GithubObject, UnknownObjectException
from uuid import UUID from uuid import UUID
from lxml import etree 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))) app.logger.info("Failed to delete file, so archiving it instead. %i, due to %s" % (file_id, str(ie)))
@staticmethod @staticmethod
def update_from_github(file_ids): def get_repo_branches():
gh_token = app.config['GITHUB_TOKEN'] gh_token = app.config['GITHUB_TOKEN']
github_repo = app.config['GITHUB_REPO']
_github = Github(gh_token) _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: for file_id in file_ids:
file_data_model = FileDataModel.query.filter_by( file_data_model = FileDataModel.query.filter_by(
@ -348,10 +358,9 @@ class FileService(object):
desc(FileDataModel.version) desc(FileDataModel.version)
).first() ).first()
try: 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: except UnknownObjectException:
# TODO: Add message indicating file is not in the repo return {'error': 'Attempted to update from repository but file was not present'}
pass
else: else:
file_data_model.data = repo_file.decoded_content file_data_model.data = repo_file.decoded_content
session.add(file_data_model) session.add(file_data_model)
@ -359,26 +368,29 @@ class FileService(object):
@staticmethod @staticmethod
def publish_to_github(file_ids): 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'] gh_token = app.config['GITHUB_TOKEN']
github_repo = app.config['GITHUB_REPO']
_github = Github(gh_token) _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: for file_id in file_ids:
file_data_model = FileDataModel.query.filter_by(file_model_id=file_id).first() file_data_model = FileDataModel.query.filter_by(file_model_id=file_id).first()
try: 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: except UnknownObjectException:
repo.create_file( repo.create_file(
path=file_data_model.file_model.name, path=file_data_model.file_model.name,
message=f'Creating {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} return {'created': True}
else: else:
updated = repo.update_file( updated = repo.update_file(
path=repo_file.path, path=repo_file.path,
message=f'Updating {file_data_model.file_model.name}', message=f'Updating {file_data_model.file_model.name}',
content=file_data_model.data, content=file_data_model.data + b'brah-model',
sha=repo_file.sha sha=repo_file.sha,
branch=target_branch
) )
return {'updated': True} return {'updated': True}

View File

@ -14,9 +14,9 @@ class FakeGithubCreates(Mock):
class FakeUser(Mock): class FakeUser(Mock):
def get_repo(var, name): def get_repo(var, name):
class FakeRepo(Mock): class FakeRepo(Mock):
def get_contents(var, filename): def get_contents(var, filename, ref):
raise UnknownObjectException(status='Failure', data='Failed data') 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 pass
return FakeRepo() return FakeRepo()
return FakeUser() return FakeUser()
@ -27,14 +27,22 @@ class FakeGithub(Mock):
class FakeUser(Mock): class FakeUser(Mock):
def get_repo(var, name): def get_repo(var, name):
class FakeRepo(Mock): class FakeRepo(Mock):
def get_contents(var, filename): def get_contents(var, filename, ref):
fake_file = Mock() fake_file = Mock()
fake_file.decoded_content = b'Some bytes' fake_file.decoded_content = b'Some bytes'
fake_file.path = '/el/path/' fake_file.path = '/el/path/'
fake_file.data = 'Serious data' fake_file.data = 'Serious data'
fake_file.sha = 'Sha' fake_file.sha = 'Sha'
return fake_file 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 pass
return FakeRepo() return FakeRepo()
return FakeUser() return FakeUser()
@ -198,3 +206,11 @@ class TestFileService(BaseTest):
result = FileService.publish_to_github([file_model.id]) result = FileService.publish_to_github([file_model.id])
self.assertEqual(result['updated'], True) 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)