-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee18571
commit 180c162
Showing
12 changed files
with
173 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[report] | ||
exclude_lines = | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ logs | |
*.egg-info | ||
venv | ||
htmlcov | ||
.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,63 @@ | ||
import os | ||
import subprocess | ||
from .globals import Global | ||
from .utils import Utils | ||
from harvey.globals import Global | ||
from harvey.utils import Utils | ||
|
||
|
||
class Git(): | ||
@classmethod | ||
def pull(cls, webhook): | ||
"""Clone/pull project using Git | ||
def update_git_repo(cls, webhook): | ||
"""Clone or pull repo using Git depending on if it exists or not | ||
""" | ||
project_path = os.path.join( | ||
Global.PROJECTS_PATH, Global.repo_full_name(webhook) | ||
) | ||
if os.path.exists(project_path): | ||
try: | ||
final_output = subprocess.check_output( | ||
f'git - C {project_path} pull - -ff-only origin master', | ||
stdin=None, | ||
stderr=None, | ||
shell=True, | ||
timeout=Global.GIT_TIMEOUT | ||
) | ||
print(final_output) | ||
except subprocess.TimeoutExpired: | ||
final_output = 'Error: Harvey timed out during git pull operation.' # noqa | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
except subprocess.CalledProcessError: | ||
final_output = f'\nError: Harvey could not pull {Global.repo_full_name(webhook)}.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
output = cls.pull_repo(project_path, webhook) | ||
else: | ||
try: | ||
final_output = subprocess.check_output( | ||
f'git clone --depth=10 --branch=master {Global.repo_url(webhook)} \ | ||
{project_path}', | ||
stdin=None, | ||
stderr=None, | ||
shell=True, | ||
timeout=Global.GIT_TIMEOUT | ||
) | ||
print(final_output) | ||
except subprocess.TimeoutExpired: | ||
final_output = 'Error: Harvey timed out during git clone operation.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
except subprocess.CalledProcessError: | ||
final_output = f'\nError: Harvey could not clone {Global.repo_full_name(webhook)}.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
output = cls.clone_repo(project_path, webhook) | ||
return output.decode('UTF-8') | ||
|
||
return final_output.decode('UTF-8') | ||
@classmethod | ||
def clone_repo(cls, project_path, webhook): | ||
"""Clone a repo into the Harvey projects folder | ||
""" | ||
try: | ||
final_output = subprocess.check_output( | ||
f'git -C {project_path} pull --rebase origin master', | ||
stdin=None, | ||
stderr=None, | ||
shell=True, | ||
timeout=Global.GIT_TIMEOUT | ||
) | ||
print(final_output) | ||
except subprocess.TimeoutExpired: | ||
final_output = 'Error: Harvey timed out during git pull operation.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
except subprocess.CalledProcessError: | ||
final_output = f'\nError: Harvey could not pull {Global.repo_full_name(webhook)}.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
|
||
@classmethod | ||
def pull_repo(cls, project_path, webhook): | ||
"""Pull updates for a repo in the Harvey projects folder | ||
""" | ||
try: | ||
final_output = subprocess.check_output( | ||
f'git clone --depth=10 --branch=master {Global.repo_url(webhook)} {project_path}', | ||
stdin=None, | ||
stderr=None, | ||
shell=True, | ||
timeout=Global.GIT_TIMEOUT | ||
) | ||
print(final_output) | ||
except subprocess.TimeoutExpired: | ||
final_output = 'Error: Harvey timed out during git clone operation.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) | ||
except subprocess.CalledProcessError: | ||
final_output = f'\nError: Harvey could not clone {Global.repo_full_name(webhook)}.' | ||
print(final_output) | ||
Utils.kill(final_output, webhook) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import pytest | ||
import mock | ||
import subprocess | ||
from harvey.git import Git | ||
|
||
|
||
@pytest.fixture | ||
def _mock_webhook(): | ||
webhook = { | ||
"repository": { | ||
"name": "TEST-repo-name", | ||
"full_name": "TEST_user/TEST-repo-name", | ||
"ssh_url": "https://test-url.com", | ||
"owner": { | ||
"name": "TEST_owner" | ||
} | ||
}, | ||
"commits": [ | ||
{ | ||
"id": 123456, | ||
"author": { | ||
"name": "test_user" | ||
} | ||
} | ||
] | ||
} | ||
return webhook | ||
|
||
|
||
@pytest.fixture | ||
def _mock_project_path(): | ||
path = 'projects/test_user/test-repo-name' | ||
return path | ||
|
||
|
||
@mock.patch('os.path.exists', return_value=True) | ||
@mock.patch('harvey.git.Git.pull_repo') | ||
def test_update_git_repo_path_exists(mock_pull_repo, mock_path_exists, _mock_project_path, _mock_webhook): # noqa | ||
Git.update_git_repo(_mock_webhook) | ||
mock_pull_repo.assert_called_once_with(_mock_project_path, _mock_webhook) | ||
|
||
|
||
@mock.patch('os.path.exists', return_value=False) | ||
@mock.patch('harvey.git.Git.clone_repo') | ||
def test_update_git_repo_path_does_not_exist(mock_clone_repo, mock_path_exists, _mock_project_path, _mock_webhook): # noqa | ||
Git.update_git_repo(_mock_webhook) | ||
mock_clone_repo.assert_called_once_with(_mock_project_path, _mock_webhook) | ||
|
||
|
||
@mock.patch('subprocess.check_output') | ||
def test_clone_repo(mock_subprocess, _mock_project_path, _mock_webhook): | ||
# TODO: Mock the subprocess better to ensure it does what it's supposed to | ||
Git.clone_repo(_mock_project_path, _mock_webhook) | ||
mock_subprocess.assert_called_once() | ||
|
||
|
||
@mock.patch('harvey.utils.Utils.kill') | ||
@mock.patch('subprocess.check_output', side_effect=subprocess.TimeoutExpired(cmd=subprocess.check_output, timeout=0.1)) # noqa | ||
def test_clone_repo_subprocess_timeout(mock_subprocess, mock_utils_kill, _mock_project_path, _mock_webhook): # noqa | ||
Git.clone_repo(_mock_project_path, _mock_webhook) | ||
mock_utils_kill.assert_called_once() | ||
|
||
|
||
@mock.patch('harvey.utils.Utils.kill') | ||
@mock.patch('subprocess.check_output', side_effect=subprocess.CalledProcessError(returncode=1, cmd=subprocess.check_output)) # noqa | ||
def test_clone_repo_process_error(mock_subprocess, mock_utils_kill, _mock_project_path, _mock_webhook): # noqa | ||
Git.clone_repo(_mock_project_path, _mock_webhook) | ||
mock_utils_kill.assert_called_once() | ||
|
||
|
||
@mock.patch('subprocess.check_output') | ||
def test_pull_repo(mock_subprocess, _mock_project_path, _mock_webhook): | ||
# TODO: Mock the subprocess better to ensure it does what it's supposed to | ||
Git.pull_repo(_mock_project_path, _mock_webhook) | ||
mock_subprocess.assert_called_once() | ||
|
||
|
||
@mock.patch('harvey.utils.Utils.kill') | ||
@mock.patch('subprocess.check_output', side_effect=subprocess.TimeoutExpired(cmd=subprocess.check_output, timeout=0.1)) # noqa | ||
def test_pull_repo_subprocess_timeout(mock_subprocess, mock_utils_kill, _mock_project_path, _mock_webhook): # noqa | ||
Git.pull_repo(_mock_project_path, _mock_webhook) | ||
mock_utils_kill.assert_called_once() | ||
|
||
|
||
@mock.patch('harvey.utils.Utils.kill') | ||
@mock.patch('subprocess.check_output', side_effect=subprocess.CalledProcessError(returncode=1, cmd=subprocess.check_output)) # noqa | ||
def test_pull_repo_process_error(mock_subprocess, mock_utils_kill, _mock_project_path, _mock_webhook): # noqa | ||
Git.pull_repo(_mock_project_path, _mock_webhook) | ||
mock_utils_kill.assert_called_once() |