From 3532fccead6502d88a192f6972a911ee4d2f1a55 Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Sun, 26 Jan 2025 03:00:50 +0300 Subject: [PATCH] WIP add build execution with shell --- config.ini | 3 ++- koji_forgejo_webhook.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/config.ini b/config.ini index fc76933..49c63c7 100644 --- a/config.ini +++ b/config.ini @@ -1,5 +1,6 @@ [DEFAULT] SECRET_KEY = $3cr3tk3y -KOJI_SERVER = https://kojidev.stackwebservices.org/kojihub +KOJI_SERVER = https://kojidev.stackwebservices.com/kojihub KOJI_TARGET = rl9-candidate +KOJI_USE_SHELL = true WEBHOOK_SECRET_KEY = your_secret_key diff --git a/koji_forgejo_webhook.py b/koji_forgejo_webhook.py index c3ca073..b3a9c87 100644 --- a/koji_forgejo_webhook.py +++ b/koji_forgejo_webhook.py @@ -3,21 +3,42 @@ import hashlib import hmac import logging import configparser -from flask import Flask, request, abort +import subprocess +from flask import Flask, request, abort, current_app import koji # Load configuration from .ini file config = configparser.ConfigParser() config.read('config.ini') -app = Flask(__name__) -app.config.from_mapping(config['DEFAULT']) +app = Flask(__name__, instance_relative_config=True) +# app.config.from_mapping(config['DEFAULT']) +app.config['SECRET_KEY'] = config.get('DEFAULT', 'SECRET_KEY') +app.config['KOJI_SERVER'] = config.get('DEFAULT', 'KOJI_SERVER') +app.config['KOJI_TARGET'] = config.get('DEFAULT', 'KOJI_TARGET') +app.config['KOJI_USE_SHELL'] = config.getboolean('DEFAULT', 'KOJI_USE_SHELL') +app.config['WEBHOOK_SECRET_KEY'] = config.get('DEFAULT', 'WEBHOOK_SECRET_KEY') class KojiProcessor: def __init__(self): pass + def koji_build_shell(self, build_target, git_url): + try: + # Construct the koji build command + command = ['koji', 'build', build_target, git_url] + + # Start the build process + result = subprocess.run(command, check=True, capture_output=True, text=True) + + # Print output from the command + logging.info(f"Build started successfully!") + logging.info(result.stdout) + except subprocess.CalledProcessError as e: + logging.error("Error starting build:") + logging.error(e.stderr) + def koji_build(self, server_url, build_target, source_url): """ Perform a Koji build @@ -91,7 +112,10 @@ class ForgejoWebhookProcessor: git_url = f"git+{clone_url}#{commit_id}" - KojiProcessor().koji_build(app.config['KOJI_SERVER'], app.config['KOJI_TARGET'], git_url) + if int(app.config.get('KOJI_USE_SHELL')) == 1: + KojiProcessor().koji_build_shell(app.config['KOJI_TARGET'], git_url) + else: + KojiProcessor().koji_build(app.config['KOJI_SERVER'], app.config['KOJI_TARGET'], git_url) except json.JSONDecodeError: logging.error("Invalid JSON payload") @@ -100,6 +124,7 @@ class ForgejoWebhookProcessor: @app.route('/', methods=['POST']) @app.route('/forgejo-webhook', methods=['POST']) +@app.route('/forgejo-webhook/', methods=['POST']) def webhook(): processor = ForgejoWebhookProcessor(app.config['WEBHOOK_SECRET_KEY'])