Merge pull request #454 from sartography/git-integration-fixes-596

Git integration fixes #596
This commit is contained in:
Dan Funk 2022-02-17 12:15:13 -05:00 committed by GitHub
commit b91372171c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 74 deletions

View File

@ -79,11 +79,11 @@ GITHUB_REPO = environ.get('GITHUB_REPO', None)
TARGET_BRANCH = environ.get('TARGET_BRANCH', None)
# Git settings, used by git_service
# The above Github settings--used in file_service, will likely be deprecated
# You can override these settings in instance/config
GIT_REMOTE_PATH = environ.get('GIT_REMOTE_PATH', None)
GIT_BRANCH = environ.get('GIT_BRANCH', None)
GIT_MERGE_BRANCH = environ.get('GIT_MERGE_BRANCH', None) # Developers can set this to 'all' in instance.config
# Among other things, we use these to build a remote URL like https://username:password@host/path.git
GIT_REMOTE_SERVER = environ.get('GIT_REMOTE_SERVER', None) # example: 'github.com'
GIT_REMOTE_PATH = environ.get('GIT_REMOTE_PATH', None) # example: 'sartography/crconnect-workflow-specs
GIT_BRANCH = environ.get('GIT_BRANCH', None) # example: 'main'
GIT_MERGE_BRANCH = environ.get('GIT_MERGE_BRANCH', None) # Example: 'staging'
# Email configuration
DEFAULT_SENDER = 'uvacrconnect@virginia.edu'

View File

@ -2381,10 +2381,9 @@ components:
merge_branch:
type: string
example: staging
# status:
# type: string
# example: staging
changes:
type: array
example: ['file_1.txt', 'file_2.txt']
untracked:
type: array
example: ['a_file.txt', 'b_file.txt']

View File

@ -29,11 +29,10 @@ class GitService(object):
@staticmethod
def get_remote_url(remote_path):
# we use github
# Note that the 'password' is a token generated by github, not the site password
host = app.config['GIT_REMOTE_SERVER']
username = app.config["GIT_USER_NAME"]
password = app.config["GIT_USER_PASS"]
remote_url = f"https://{username}:{password}@github.com/{remote_path}.git"
remote_url = f"https://{username}:{password}@{host}/{remote_path}.git"
return remote_url
@staticmethod

View File

@ -195,66 +195,6 @@ class UserFileService(object):
app.logger.info("Failed to delete file, so archiving it instead. %i, due to %s" % (file_id, str(ie)))
raise ApiError('Delete Failed', "Unable to delete file. ")
@staticmethod
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(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(
file_model_id=file_id
).order_by(
desc(FileDataModel.version)
).first()
try:
repo_file = repo.get_contents(file_data_model.file_model.name, ref=source_target)
except UnknownObjectException:
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)
session.commit()
@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(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, 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,
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 + b'brah-model',
sha=repo_file.sha,
branch=target_branch
)
return {'updated': True}
@staticmethod
def dmn_from_spreadsheet(ss_data):

View File

@ -1,5 +1,6 @@
from tests.base_test import BaseTest
from crc import app
from crc.services.git_service import GitService
from unittest.mock import patch, Mock, call
@ -57,6 +58,10 @@ class TestGitService(BaseTest):
self.assertIn(call.index.commit('This is my comment'), method_calls)
self.assertIn(call.remotes.origin.push(), method_calls)
# def test_pull_from_remote(self):
# result = GitService.pull_from_remote()
# print(result)
def test_get_remote_url(self):
app.config['GIT_REMOTE_SERVER'] = 'test_server.com'
app.config['GIT_USER_NAME'] = 'test_username'
app.config['GIT_USER_PASS'] = 'test_pass'
result = GitService.get_remote_url('my_test_path')
self.assertEqual('https://test_username:test_pass@test_server.com/my_test_path.git', result)