Skip to content

Commit

Permalink
code-runner gitea webhook added
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 5, 2024
1 parent 125f975 commit 2e14da2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions data/code-runner-gitea-webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
import shutil
import threading
import json
import os

builddir="/tmp/code-runner"
class WebhookHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()


def run_code_runner(self, target, **kwargs):
subprocess.run(["ymp", "code-runner", target+"/build.yaml"])

def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
payload = json.loads(post_data.decode('utf-8'))

repo=payload["repository"]["clone_url"]
target=builddir+"/"+payload["repository"]["full_name"]
if os.path.exists(target):
shutil.rmtree(target)
if not os.path.isdir(os.path.dirname(target)):
os.makedirs(os.path.dirname(target))
subprocess.run(["git", "clone", repo, target])
thread = threading.Thread(target=self.run_code_runner, args=(target,))
thread.start()


self._set_headers()
self.wfile.write(json.dumps({'status': 'success'}).encode('utf-8'))

def run(server_class=HTTPServer, handler_class=WebhookHandler, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting server on port {port}...')
httpd.serve_forever()

if __name__ == '__main__':
run()

0 comments on commit 2e14da2

Please sign in to comment.