Skip to content

Commit

Permalink
Move from config based to string based actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Udoff committed Jul 24, 2020
1 parent 2aef400 commit 4f1d2f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
28 changes: 10 additions & 18 deletions jupyterlab_git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,27 @@
"""
# need this in order to show version in `jupyter serverextension list`
from ._version import __version__
from traitlets import Type
from traitlets import List, Dict, Unicode
from traitlets.config import Configurable

from jupyterlab_git.handlers import setup_handlers
from jupyterlab_git.git import Git

class GitCustomActions(object):
"""
Class to handle custom actions. This is a base class that is expected to be replaced via the jupyter_notebook_config
"""

async def post_init(self, cwd):
"""
:param cwd: Directory where git init was run
"""
pass

class JupyterLabGitConfig(Configurable):
class JupyterLabGit(Configurable):
"""
Config options for jupyterlab_git
Modeled after: https://github.com/jupyter/jupyter_server/blob/9dd2a9a114c045cfd8fd8748400c6a697041f7fa/jupyter_server/serverapp.py#L1040
"""

# See: https://traitlets.readthedocs.io/en/stable/trait_types.html#classes-and-instances
custom_git_actions_class = Type(
default_value=GitCustomActions,
actions = Dict(
help='Actions to be taken after a git command. Each action takes a list of commands to execute (strings). Supported actions: post_init',
config=True,
help='Delegate custom git actions'
trait=List(
trait=Unicode,
help='List of commands to run. E.g. ["touch baz.py"]'
)
# TODO Validate
)

def _jupyter_server_extension_paths():
Expand All @@ -43,7 +35,7 @@ def load_jupyter_server_extension(nbapp):
"""Load the Jupyter server extension.
"""

user_custom_actions = JupyterLabGitConfig(config=nbapp.config).custom_git_actions_class()
user_custom_actions = JupyterLabGit(config=nbapp.config).actions
git = Git(nbapp.web_app.settings['contents_manager'], user_custom_actions)
nbapp.web_app.settings["git"] = git
setup_handlers(nbapp.web_app)
20 changes: 19 additions & 1 deletion jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
import re
import shlex
import subprocess
from urllib.parse import unquote

Expand Down Expand Up @@ -875,12 +876,29 @@ async def init(self, current_path):
code, _, error = await execute(
cmd, cwd=cwd
)
await self.user_custom_actions.post_init(cwd)
if code == 0:
code, _, error = await self._maybe_run_actions('post_init', cwd)

if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}

async def _maybe_run_actions(self, name, cwd):
code = 0
stdout = None
stderr = None
if name in self.user_custom_actions:
actions_list = self.user_custom_actions[name]
for action in actions_list:
# We trust the actions as they were passed via a config and not the UI
code, stdout, stderr = await execute(
shlex.split(action), cwd=cwd
)
# After any failure, stop
if code != 0:
break
return code, stdout, stderr

def _is_remote_branch(self, branch_reference):
"""Check if given branch is remote branch by comparing with 'remotes/',
TODO : Consider a better way to check remote branch
Expand Down

0 comments on commit 4f1d2f4

Please sign in to comment.