Skip to content

Commit

Permalink
[dy] Fix git integration bugs (mage-ai#2199)
Browse files Browse the repository at this point in the history
* [dy] Fix bugs

* [dy] Fix lint
  • Loading branch information
dy46 authored Mar 14, 2023
1 parent cc71cb5 commit 2d4e93f
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 269 deletions.
6 changes: 2 additions & 4 deletions docs/developing-in-the-cloud/setting-up-git.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ sidebarTitle: "Git"

## 1. Open terminal

1. Open a pipeline
2. In the right panel of the screen (aka Sidekick), click on the tab named
**Terminal**.
1. In the left navigation menu, click the Terminal icon.
3. Click the terminal (large dark background area), and you should see a
blinking cursor. You can start typing once that cursor appears.
yellow cursor. You can start typing once that cursor appears.

---

Expand Down
7 changes: 7 additions & 0 deletions mage_ai/api/resources/GitBranchResource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from mage_ai.api.errors import ApiError
from mage_ai.api.resources.GenericResource import GenericResource
from mage_ai.data_preparation.git import Git
from mage_ai.data_preparation.preferences import get_preferences
Expand Down Expand Up @@ -37,6 +38,12 @@ async def update(self, payload, **kwargs):
self.model = dict(name=git_manager.current_branch, status=status)
elif action_type == 'commit':
message = payload.get('message')
if not message:
error = ApiError.RESOURCE_ERROR
error.update({
'message': 'Message is empty, please add a message for your commit.',
})
raise ApiError(error)
git_manager.commit(message)
elif action_type == 'push':
await git_manager.check_connection()
Expand Down
11 changes: 5 additions & 6 deletions mage_ai/api/resources/SyncResource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from mage_ai.api.resources.GenericResource import GenericResource
from mage_ai.data_preparation.git import Git
from mage_ai.data_preparation.preferences import get_preferences
from mage_ai.data_preparation.sync import GitConfig
from mage_ai.data_preparation.sync.git_sync import GitSync


class SyncResource(GenericResource):
Expand All @@ -22,7 +22,7 @@ def create(self, payload, user, **kwargs):
dict(sync_config=sync_config.to_dict())
)

Git(sync_config)
GitSync(sync_config)

return self(get_preferences().sync_config, user, **kwargs)

Expand All @@ -31,11 +31,10 @@ def member(self, pk, user, **kwargs):
return self(get_preferences().sync_config, user, **kwargs)

def update(self, payload, **kwargs):
git_manager = Git.get_manager()
config = git_manager.git_config
config = GitConfig.load(config=self.model)
sync = GitSync(config)
action_type = payload.get('action_type')
if action_type == 'sync_data':
git_manager.check_connection()
git_manager.reset(config.branch)
sync.sync_data()

return self
22 changes: 16 additions & 6 deletions mage_ai/data_preparation/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import git
import subprocess

REMOTE_NAME = 'mage-repo'


class Git:
def __init__(self, git_config: GitConfig):
Expand All @@ -18,12 +20,14 @@ def __init__(self, git_config: GitConfig):
self.commit('Initial commit')

try:
self.repo.create_remote('origin', self.remote_repo_link)
self.repo.create_remote(REMOTE_NAME, self.remote_repo_link)
except git.exc.GitCommandError:
# if the remote already exists
self.repo.remotes.origin.set_url(self.remote_repo_link)
pass

self.origin = self.repo.remotes.origin
self.origin = self.repo.remotes[REMOTE_NAME]
if self.remote_repo_link not in self.origin.urls:
self.origin.set_url(self.remote_repo_link)

@classmethod
def get_manager(self):
Expand Down Expand Up @@ -56,7 +60,9 @@ async def check_connection(self):
if return_code is None:
proc.kill()
raise Exception(
"Connecting to remote timed out, make sure your SSH key is set up properly.")
"Connecting to remote timed out, make sure your SSH key is set up properly"
" and your repository host is added as a known host. More information here:"
" https://docs.mage.ai/developing-in-the-cloud/setting-up-git#5-add-github-com-to-known-hosts") # noqa: E501

def all_branches(self):
return [head.name for head in self.repo.heads]
Expand All @@ -65,10 +71,14 @@ def reset(self, branch: str = None):
self.origin.fetch()
if branch is None:
branch = self.current_branch
self.repo.git.reset('--hard', f'origin/{branch}')
self.repo.git.reset('--hard', f'{self.origin.name}/{branch}')

def push(self):
self.repo.git.push('--set-upstream', self.origin.name, self.current_branch)
self.repo.git.push(
'--set-upstream',
self.origin.name,
self.current_branch
)

def pull(self):
self.origin.pull(self.current_branch)
Expand Down
29 changes: 6 additions & 23 deletions mage_ai/data_preparation/sync/git_sync.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
from mage_ai.data_preparation.git import Git
from mage_ai.data_preparation.sync import GitConfig
from mage_ai.data_preparation.sync.base_sync import BaseSync
from mage_ai.shared.logger import VerboseFunctionExec
import git
import subprocess
import asyncio


class GitSync(BaseSync):
def __init__(self, sync_config: GitConfig):
self.remote_repo_link = sync_config.remote_repo_link
self.repo_path = sync_config.repo_path
self.branch = sync_config.branch
try:
self.repo = git.Repo(self.repo_path)
except git.exc.InvalidGitRepositoryError:
self.repo = git.Repo.init(self.repo_path)

try:
self.repo.create_remote('origin', self.remote_repo_link)
except git.exc.GitCommandError:
# if the remote already exists
self.repo.remotes.origin.set_url(self.remote_repo_link)

self.origin = self.repo.remotes.origin
self.remote_repo_link = sync_config.remote_repo_link
self.git_manager = Git(sync_config)

def sync_data(self):
with VerboseFunctionExec(
f'Syncing data with remote repo {self.remote_repo_link}',
verbose=True,
):
# use subprocess because the gitpython command wasn't timing out correctly
subprocess.run(
['git', 'fetch', self.origin.name],
cwd=self.repo_path,
timeout=30,
)
self.repo.git.reset('--hard', f'{self.origin.name}/{self.branch}')
asyncio.run(self.git_manager.check_connection())
self.git_manager.reset(self.branch)
Loading

0 comments on commit 2d4e93f

Please sign in to comment.