From 3f26f8511dded1cfda18c47ed9bd07eacd18c234 Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Mon, 12 Sep 2022 16:59:02 -0400 Subject: [PATCH 1/2] ADD push_only option to sidecar internal API server. With this new option, we can push without generating a commit via the CLI. Signed-off-by: Zixuan James Li --- theia/sidecar/app.py | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/theia/sidecar/app.py b/theia/sidecar/app.py index 012e0721b..630fee390 100644 --- a/theia/sidecar/app.py +++ b/theia/sidecar/app.py @@ -32,6 +32,7 @@ def index(): # Get options from the form repo: str = request.form.get('repo', default=None) message: str = request.form.get('message', default='Anubis Cloud IDE Autosave').strip() + push_only: bool = request.form.get('push_only', default='false').lower() == 'true' # Default commit message if empty if message == '': @@ -45,28 +46,35 @@ def index(): if repo is None or not os.path.isdir(os.path.join(repo, '.git')): return text_response('Please navigate to the repository that you would like to autosave') - try: - # Add - add = subprocess.run( - ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.push=push', 'add', '.'], - cwd=repo, - timeout=3, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - if add.returncode != 0: - return text_response('Failed to git add') + output: list[bytes] = [] - # Commit - commit = subprocess.run( - ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.commit=commit', 'commit', '--no-verify', '-m', message], - cwd=repo, - timeout=3, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - if commit.returncode != 0: - return text_response('Failed to git commit') + try: + # We skip the add and commit phase if push_only is enabled + # In that case, we will only try to push + if not push_only: + # Add + add = subprocess.run( + ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.push=push', 'add', '.'], + cwd=repo, + timeout=3, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + if add.returncode != 0: + return text_response('Failed to git add') + output.append(add.stdout) + + # Commit + commit = subprocess.run( + ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.commit=commit', 'commit', '--no-verify', '-m', message], + cwd=repo, + timeout=3, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + if commit.returncode != 0: + return text_response('Failed to git commit') + output.append(commit.stdout) # Push push = subprocess.run( @@ -78,12 +86,13 @@ def index(): ) if push.returncode != 0: return text_response('Failed to git push') + output.append(push.stdout) except subprocess.TimeoutExpired: return text_response('Autosave timeout') - output = (add.stdout + commit.stdout + push.stdout + b'\n').decode() - return text_response(output) + output.append(b'\n') + return text_response(b"".join(output).decode()) if ADMIN: From a1c65bdd17dd5f6932aa29886789a887882bf272 Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Mon, 12 Sep 2022 17:04:19 -0400 Subject: [PATCH 2/2] ADD an option to force push via sidecar API. This allows the student to force push via the CLI. Making `git push` and `git push -f` available should be sufficient for students to maneuver with Git on our IDE instances for the purposes of learning. It's unlikely that we are going to support more arguments. Allowing the students to push via Anubis CLI is only for the convenience of not requiring them to set up their own authentication mechanism on GitHub or storing their SSH keys or GitHub token on the server. Signed-off-by: Zixuan James Li --- theia/sidecar/app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/theia/sidecar/app.py b/theia/sidecar/app.py index 630fee390..cdf838dcd 100644 --- a/theia/sidecar/app.py +++ b/theia/sidecar/app.py @@ -33,6 +33,7 @@ def index(): repo: str = request.form.get('repo', default=None) message: str = request.form.get('message', default='Anubis Cloud IDE Autosave').strip() push_only: bool = request.form.get('push_only', default='false').lower() == 'true' + force_push: bool = request.form.get('force_push', default='false').lower() == 'true' # Default commit message if empty if message == '': @@ -77,8 +78,11 @@ def index(): output.append(commit.stdout) # Push + push_args = ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.push=push', 'push', '--no-verify'] + if force_push: + push_args.append('--force') push = subprocess.run( - ['git', '-c', 'core.hooksPath=/dev/null', '-c', 'alias.push=push', 'push', '--no-verify'], + push_args, cwd=repo, timeout=3, stdout=subprocess.PIPE,