|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +from flask import Flask, request, make_response |
| 4 | +import subprocess |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | + |
| 8 | +@app.route('/', methods=['POST']) |
| 9 | +def index(): |
| 10 | + message = request.form.get('message', default='Anubis Cloud IDE Autosave') |
| 11 | + if message.strip() == '': |
| 12 | + message = 'Anubis Cloud IDE Autosave' |
| 13 | + |
| 14 | + try: |
| 15 | + # Add |
| 16 | + add = subprocess.run( |
| 17 | + ['git', 'add', '.'], |
| 18 | + cwd='/home/project', |
| 19 | + timeout=3, |
| 20 | + stdout=subprocess.PIPE, |
| 21 | + stderr=subprocess.STDOUT, |
| 22 | + ) |
| 23 | + if add.returncode != 0: |
| 24 | + response = make_response( |
| 25 | + 'Failed to git add\n', |
| 26 | + ) |
| 27 | + response.headers = {'Content-Type': 'text/plain'} |
| 28 | + return response |
| 29 | + |
| 30 | + # Commit |
| 31 | + commit = subprocess.run( |
| 32 | + ['git', 'commit', '-m', message], |
| 33 | + cwd='/home/project', |
| 34 | + timeout=3, |
| 35 | + stdout=subprocess.PIPE, |
| 36 | + stderr=subprocess.STDOUT, |
| 37 | + ) |
| 38 | + if commit.returncode != 0: |
| 39 | + response = make_response( |
| 40 | + 'Failed to git commit\n', |
| 41 | + ) |
| 42 | + response.headers = {'Content-Type': 'text/plain'} |
| 43 | + return response |
| 44 | + |
| 45 | + # Push |
| 46 | + push = subprocess.run( |
| 47 | + ['git', 'push'], |
| 48 | + cwd='/home/project', |
| 49 | + timeout=3, |
| 50 | + stdout=subprocess.PIPE, |
| 51 | + stderr=subprocess.STDOUT, |
| 52 | + ) |
| 53 | + if push.returncode != 0: |
| 54 | + response = make_response( |
| 55 | + 'Failed to git push\n', |
| 56 | + ) |
| 57 | + response.headers = {'Content-Type': 'text/plain'} |
| 58 | + return response |
| 59 | + |
| 60 | + except subprocess.TimeoutExpired: |
| 61 | + response = make_response( |
| 62 | + 'Autosave timeout\n', |
| 63 | + ) |
| 64 | + response.headers = {'Content-Type': 'text/plain'} |
| 65 | + return response |
| 66 | + |
| 67 | + output = (add.stdout + commit.stdout + push.stdout + b'\n').decode() |
| 68 | + response = make_response(output) |
| 69 | + response.headers = {'Content-Type': 'text/plain'} |
| 70 | + return response |
0 commit comments