41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import os
|
|
|
|
from google.cloud import storage
|
|
|
|
|
|
def handle_webhook(request):
|
|
request_json = request.get_json()
|
|
# Check if the required fields are in the request
|
|
print(request_json)
|
|
if not request_json or "repository" not in request_json:
|
|
return "Invalid request: 'repository' field missing", 400
|
|
|
|
# Extract the 'clone_url' field from the 'repository' object
|
|
repo_url = request_json.get("repository", {}).get("clone_url")
|
|
if not repo_url:
|
|
return "Invalid request: 'clone_url' field missing", 400
|
|
|
|
|
|
if not repo_url:
|
|
return "Repository URL not provided", 400
|
|
|
|
file_path = "/tmp/repo"
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
# Clone the repository
|
|
os.system(f"git clone {repo_url} {file_path}")
|
|
|
|
# Upload to Google Cloud Storage
|
|
bucket_name = os.environ.get('BUCKET_NAME')
|
|
client = storage.Client()
|
|
bucket = client.bucket(bucket_name)
|
|
|
|
for root, dirs, files in os.walk('/tmp/repo'):
|
|
for file_name in files:
|
|
local_path = os.path.join(root, file_name)
|
|
remote_path = os.path.relpath(local_path, '/tmp/repo')
|
|
blob = bucket.blob(remote_path)
|
|
blob.upload_from_filename(local_path)
|
|
|
|
return "Repository uploaded", 200
|