From 27b342623a750725ef567fd804f8e4e3e2537b7a Mon Sep 17 00:00:00 2001 From: viacheslav anzhiganov Date: Sat, 25 Jan 2025 05:56:19 +0300 Subject: [PATCH 1/3] add koji call --- koji-forgejo-webook.py | 61 +++++++++++++++++++++++++++++++++++++++--- reequirements.txt | 1 - requirements.txt | 2 ++ 3 files changed, 59 insertions(+), 5 deletions(-) delete mode 100644 reequirements.txt create mode 100644 requirements.txt diff --git a/koji-forgejo-webook.py b/koji-forgejo-webook.py index 584bc50..a567464 100644 --- a/koji-forgejo-webook.py +++ b/koji-forgejo-webook.py @@ -3,8 +3,53 @@ import hashlib import hmac import logging from flask import Flask, request, abort +import koji + app = Flask(__name__) +app.config['SECRET_KEY'] = '123' +# koji +app.config['KOJI_SERVER'] = 'https://kojidev.stackwebservices.org/kojihub' +app.config['KOJI_TARGET'] = 'rl9-candidate' + +app.config['WEBHOOK_SECRET_KEY'] = 'your_secret_key' + +class KojiProcessor: + def __init__(self): + pass + + def koji_build(self, server_url, build_target, source_url): + """ + Perform a Koji build + + :param server_url: Koji hub server URL + :param build_target: Target for the build (e.g., 'f40-candidate') + :param source_url: Git repository URL to build from + """ + # Create a Koji client session + session = koji.ClientSession(server_url) + + try: + # Authenticate (if required) + # Uncomment if authentication is needed + session.login() + + # Submit the build task + build_id = session.build( + source_url, # Source URL + build_target, # Build target + opts={ + 'scratch': False, # Set to True for scratch build + 'nowait': False # Wait for build completion + } + ) + + return build_id + + except Exception as e: + print(f"Build failed: {e}") + return None + class ForgejoWebhookProcessor: def __init__(self, secret_key): @@ -28,6 +73,7 @@ class ForgejoWebhookProcessor: # Extract key webhook information repository = data.get('repository', {}) commits = data.get('commits', []) + head_commit = data.get('head_commit') ref = data.get('ref', '') # Log basic webhook details @@ -36,9 +82,16 @@ class ForgejoWebhookProcessor: logging.info(f"Number of Commits: {len(commits)}") # Custom processing logic here - for commit in commits: - logging.info(f"Commit: {commit.get('id')}") - logging.info(f"Message: {commit.get('message')}") + # for commit in commits: + # logging.info(f"Commit: {commit.get('id')}") + # logging.info(f"Message: {commit.get('message')}") + + commit_id = head_commit.get("id") + clone_url = repository.get("clone_url") + + git_url = f"git+{clone_url}#{commit_id}" + + KojiProcessor().koji_build(app.config['KOJI_SERVER'], app.config['KOJI_TARGET'], git_url) except json.JSONDecodeError: logging.error("Invalid JSON payload") @@ -47,7 +100,7 @@ class ForgejoWebhookProcessor: @app.route('/forgejo-webhook', methods=['POST']) def webhook(): - processor = ForgejoWebhookProcessor('your_secret_key') + processor = ForgejoWebhookProcessor(app.config['WEBHOOK_SECRET_KEY']) # Get payload and signature payload = request.get_data(as_text=True) diff --git a/reequirements.txt b/reequirements.txt deleted file mode 100644 index 8ab6294..0000000 --- a/reequirements.txt +++ /dev/null @@ -1 +0,0 @@ -flask \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8f91ebc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +koji \ No newline at end of file -- 2.45.3 From e6ab985c12c649f97e42a4d8d3c40db29b61943e Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Sat, 25 Jan 2025 22:07:39 +0300 Subject: [PATCH 2/3] WIP add apache wsgi configuration --- README.md | 4 ++-- config.ini | 5 +++++ koji_forgejo_webhook.conf | 5 +++++ koji-forgejo-webook.py => koji_forgejo_webhook.py | 12 +++++++----- koji_forgejo_webhook.wsgi | 7 +++++++ 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 config.ini create mode 100644 koji_forgejo_webhook.conf rename koji-forgejo-webook.py => koji_forgejo_webhook.py (94%) create mode 100644 koji_forgejo_webhook.wsgi diff --git a/README.md b/README.md index e08e63b..51f5383 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ pip install -r requirements.txt ### rpm ```shell -dnf install python3-flask +dnf install python3-flask python3-mod_wsgi ``` ## run ```shell python3 koji-forgejo-webook.py -``` \ No newline at end of file +``` diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..fc76933 --- /dev/null +++ b/config.ini @@ -0,0 +1,5 @@ +[DEFAULT] +SECRET_KEY = $3cr3tk3y +KOJI_SERVER = https://kojidev.stackwebservices.org/kojihub +KOJI_TARGET = rl9-candidate +WEBHOOK_SECRET_KEY = your_secret_key diff --git a/koji_forgejo_webhook.conf b/koji_forgejo_webhook.conf new file mode 100644 index 0000000..801f543 --- /dev/null +++ b/koji_forgejo_webhook.conf @@ -0,0 +1,5 @@ +WSGIDaemonProcess forgejo processes=2 threads=4 +WSGIScriptAlias /forgejo-webhook /opt/koji-forgejo-webhook/koji_forgejo_webhook.wsgi + + Require all granted + diff --git a/koji-forgejo-webook.py b/koji_forgejo_webhook.py similarity index 94% rename from koji-forgejo-webook.py rename to koji_forgejo_webhook.py index a567464..c314909 100644 --- a/koji-forgejo-webook.py +++ b/koji_forgejo_webhook.py @@ -2,17 +2,17 @@ import json import hashlib import hmac import logging +import configparser from flask import Flask, request, abort import koji +# Load configuration from .ini file +config = configparser.ConfigParser() +config.read('config.ini') app = Flask(__name__) -app.config['SECRET_KEY'] = '123' -# koji -app.config['KOJI_SERVER'] = 'https://kojidev.stackwebservices.org/kojihub' -app.config['KOJI_TARGET'] = 'rl9-candidate' +app.config.from_mapping(config['DEFAULT']) -app.config['WEBHOOK_SECRET_KEY'] = 'your_secret_key' class KojiProcessor: def __init__(self): @@ -98,6 +98,7 @@ class ForgejoWebhookProcessor: abort(400) +@app.route('/', methods=['POST']) @app.route('/forgejo-webhook', methods=['POST']) def webhook(): processor = ForgejoWebhookProcessor(app.config['WEBHOOK_SECRET_KEY']) @@ -115,6 +116,7 @@ def webhook(): return 'Webhook processed', 200 + if __name__ == '__main__': logging.basicConfig(level=logging.INFO) app.run(host="0.0.0.0", port=5001) \ No newline at end of file diff --git a/koji_forgejo_webhook.wsgi b/koji_forgejo_webhook.wsgi new file mode 100644 index 0000000..74515f5 --- /dev/null +++ b/koji_forgejo_webhook.wsgi @@ -0,0 +1,7 @@ +import sys +import logging + +logging.basicConfig(stream=sys.stderr) +sys.path.insert(0, "/opt/koji-forgejo-webhook") + +from koji_forgejo_webhook import app as application -- 2.45.3 From 2df80f6d41530c38e29ecac5a8b82c88d3deb771 Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Sat, 25 Jan 2025 22:41:37 +0300 Subject: [PATCH 3/3] WIP add trailing EOL --- koji_forgejo_webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/koji_forgejo_webhook.py b/koji_forgejo_webhook.py index c314909..c3ca073 100644 --- a/koji_forgejo_webhook.py +++ b/koji_forgejo_webhook.py @@ -119,4 +119,4 @@ def webhook(): if __name__ == '__main__': logging.basicConfig(level=logging.INFO) - app.run(host="0.0.0.0", port=5001) \ No newline at end of file + app.run(host="0.0.0.0", port=5001) -- 2.45.3