From f083bd1451176b7b17ea0ecb065263ad77f48dd4 Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 1 Dec 2026 17:45:25 +0100 Subject: [PATCH] First version --- .../notifications-webhook-release.yaml | 117 ++++++++++++++++++ .gitignore | 2 + Makefile | 39 ++++++ pkg/DEBIAN/conffiles | 1 + pkg/DEBIAN/control | 11 ++ pkg/DEBIAN/postinst | 18 +++ pkg/DEBIAN/prerm | 8 ++ pkg/etc/notifications-webhook/config | 8 ++ pkg/usr/bin/send-notification | 7 ++ 9 files changed, 211 insertions(+) create mode 100644 .gitea/workflows/notifications-webhook-release.yaml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 pkg/DEBIAN/conffiles create mode 100644 pkg/DEBIAN/control create mode 100644 pkg/DEBIAN/postinst create mode 100644 pkg/DEBIAN/prerm create mode 100644 pkg/etc/notifications-webhook/config create mode 100644 pkg/usr/bin/send-notification diff --git a/.gitea/workflows/notifications-webhook-release.yaml b/.gitea/workflows/notifications-webhook-release.yaml new file mode 100644 index 0000000..ac57b6b --- /dev/null +++ b/.gitea/workflows/notifications-webhook-release.yaml @@ -0,0 +1,117 @@ +name: Build and publish notifications-webhook + +on: + workflow_dispatch: + inputs: + version: + description: "Library version, without the v (e.g. 1.0)" + required: true + type: string + revision: + description: "Packaging revision" + required: false + default: "1" + type: string + +env: + DISTRIBUTION: any + COMPONENT: main + PACKAGE: notifications-webhook + SOURCE_OWNER: notifier + SOURCE_REPO: lib-send-notifications-webhook + +jobs: + select-runner: + runs-on: ubuntu-latest + outputs: + runner: ${{ steps.pick.outputs.runner }} + steps: + - name: Check if local runner is online + id: pick + env: + API_TOKEN: ${{ secrets.GET_AVAILABLE_RUNNERS_API_TOKEN }} + API_URL: ${{ github.server_url }}/api/v1/admin/actions/runners + run: | + RUNNER="ubuntu-latest" + + RESPONSE=$(curl -fsS --max-time 15 \ + -H "Authorization: token ${API_TOKEN}" \ + "${API_URL}" || echo "") + + if [ -n "$RESPONSE" ]; then + ONLINE=$(echo "$RESPONSE" | jq '[.runners[]? + | select(.status != "offline") + | select(any(.labels[]?; .name == "local"))] | length') + if [ "${ONLINE:-0}" -gt 0 ]; then + RUNNER="local" + fi + else + echo "Runner API not reachable, falling back to ubuntu-latest" + fi + + echo "Selected runner: $RUNNER" + echo "runner=$RUNNER" >> "$GITHUB_OUTPUT" + + build-and-publish: + needs: select-runner + runs-on: ${{ needs.select-runner.outputs.runner }} + steps: + - name: Validate inputs + env: + VERSION: ${{ inputs.version }} + REVISION: ${{ inputs.revision }} + run: | + echo "$VERSION" | grep -Eq '^[0-9]+(\.[0-9]+)*$' || { echo "Bad version"; exit 1; } + echo "$REVISION" | grep -Eq '^[0-9]+$' || { echo "Bad revision"; exit 1; } + + - uses: actions/checkout@v4 + + - name: Install build tools + run: | + if ! command -v make >/dev/null || ! command -v curl >/dev/null || ! command -v dpkg-deb >/dev/null; then + apt-get update && apt-get install -y make curl dpkg + fi + + - name: Fetch library source at the tag + env: + VERSION: ${{ inputs.version }} + SERVER: ${{ gitea.server_url }} + run: | + curl -sSL --fail \ + -o /tmp/source.tar.gz \ + "${SERVER}/${SOURCE_OWNER}/${SOURCE_REPO}/archive/v${VERSION}.tar.gz" + mkdir -p /tmp/source + tar -xzf /tmp/source.tar.gz -C /tmp/source --strip-components=1 + + - name: Build package + env: + VERSION: ${{ inputs.version }} + REVISION: ${{ inputs.revision }} + run: make VERSION="$VERSION" REVISION="$REVISION" SRC_DIR=/tmp/source/src + + - name: Publish to Gitea package registry + env: + VERSION: ${{ inputs.version }} + REVISION: ${{ inputs.revision }} + UPLOADER: ${{ secrets.PACKAGE_UPLOAD_USERNAME }} + TOKEN: ${{ secrets.PACKAGE_UPLOAD_TOKEN }} + OWNER: ${{ gitea.repository_owner }} + SERVER: ${{ gitea.server_url }} + run: | + DEB="build/${PACKAGE}_${VERSION}-${REVISION}_all.deb" + CODE=$(curl -sS -o /tmp/resp -w '%{http_code}' \ + --user "${UPLOADER}:${TOKEN}" \ + --upload-file "$DEB" \ + "${SERVER}/api/packages/${OWNER}/debian/pool/${DISTRIBUTION}/${COMPONENT}/upload") + cat /tmp/resp; echo + case "$CODE" in + 201) echo "Published $DEB" ;; + 409) echo "This version/revision already exists; bump REVISION"; exit 1 ;; + *) echo "Upload failed with HTTP $CODE"; exit 1 ;; + esac + + - name: Keep the .deb as a workflow artifact + uses: actions/upload-artifact@v3 + with: + name: notifications-webhook-deb + path: build/*.deb \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9fd4f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +src/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..23bf127 --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +PACKAGE := notifications-webhook +MODULE := send_notifications_webhook +SITE := usr/lib/python3/dist-packages + +VERSION ?= +REVISION ?= 1 +SRC_DIR ?= ../lib-send-notifications-webhook/src + +BUILD_DIR := build +STAGE := $(BUILD_DIR)/$(PACKAGE)_$(VERSION)-$(REVISION) +DEB := $(BUILD_DIR)/$(PACKAGE)_$(VERSION)-$(REVISION)_all.deb + +.PHONY: deb check clean +.DEFAULT_GOAL := deb + +check: + @test -n "$(VERSION)" || { echo "VERSION is required"; exit 1; } + @test -d "$(SRC_DIR)/$(MODULE)" || { echo "$(SRC_DIR)/$(MODULE) not found"; exit 1; } + @test -f "$(SRC_DIR)/$(MODULE)/cli.py" || { echo "$(SRC_DIR)/$(MODULE)/cli.py not found (is it in the tag?)"; exit 1; } + +deb: check + rm -rf $(STAGE) + mkdir -p $(STAGE)/DEBIAN $(STAGE)/$(SITE) + cp -r pkg/etc pkg/usr $(STAGE)/ + cp -r $(SRC_DIR)/$(MODULE) $(STAGE)/$(SITE)/ + find $(STAGE)/$(SITE) -name '__pycache__' -type d -prune -exec rm -rf {} + + find $(STAGE) -type d -exec chmod 0755 {} + + find $(STAGE)/$(SITE) -type f -exec chmod 0644 {} + + chmod 0755 $(STAGE)/usr/bin/send-notification + chmod 0640 $(STAGE)/etc/notifications-webhook/config + sed 's/@VERSION@/$(VERSION)-$(REVISION)/' pkg/DEBIAN/control > $(STAGE)/DEBIAN/control + echo "Installed-Size: $$(du -sk --exclude=DEBIAN $(STAGE) | cut -f1)" >> $(STAGE)/DEBIAN/control + install -m 0755 pkg/DEBIAN/postinst pkg/DEBIAN/prerm $(STAGE)/DEBIAN/ + install -m 0644 pkg/DEBIAN/conffiles $(STAGE)/DEBIAN/ + dpkg-deb --root-owner-group --build $(STAGE) $(DEB) + @echo "Built $(DEB)" + +clean: + rm -rf $(BUILD_DIR) \ No newline at end of file diff --git a/pkg/DEBIAN/conffiles b/pkg/DEBIAN/conffiles new file mode 100644 index 0000000..59f4207 --- /dev/null +++ b/pkg/DEBIAN/conffiles @@ -0,0 +1 @@ +/etc/notifications-webhook/config diff --git a/pkg/DEBIAN/control b/pkg/DEBIAN/control new file mode 100644 index 0000000..6b5cb5b --- /dev/null +++ b/pkg/DEBIAN/control @@ -0,0 +1,11 @@ +Package: notifications-webhook +Version: @VERSION@ +Architecture: all +Section: utils +Priority: optional +Depends: python3 (>= 3.10), python3-requests, python3-shared-models (>= 1.0) +Maintainer: Florian dev@gansejunge.com +Description: Command-line client for the notification webhook + Provides the send-notification command and the send_notifications_webhook + Python library. The webhook URL and API key are read from + /etc/notifications-webhook/config. \ No newline at end of file diff --git a/pkg/DEBIAN/postinst b/pkg/DEBIAN/postinst new file mode 100644 index 0000000..cfbf1ba --- /dev/null +++ b/pkg/DEBIAN/postinst @@ -0,0 +1,18 @@ +#!/bin/sh +set -e + +case "$1" in + configure) + if ! getent group notifications-webhook >/dev/null; then + groupadd --system notifications-webhook + fi + chown root:notifications-webhook /etc/notifications-webhook/config + chmod 0640 /etc/notifications-webhook/config + + if command -v py3compile >/dev/null 2>&1; then + py3compile -p notifications-webhook + fi + ;; +esac + +exit 0 \ No newline at end of file diff --git a/pkg/DEBIAN/prerm b/pkg/DEBIAN/prerm new file mode 100644 index 0000000..9792166 --- /dev/null +++ b/pkg/DEBIAN/prerm @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +if command -v py3clean >/dev/null 2>&1; then + py3clean -p notifications-webhook +fi + +exit 0 \ No newline at end of file diff --git a/pkg/etc/notifications-webhook/config b/pkg/etc/notifications-webhook/config new file mode 100644 index 0000000..9415fbc --- /dev/null +++ b/pkg/etc/notifications-webhook/config @@ -0,0 +1,8 @@ +# Configuration for send-notification. +# +# This file contains a secret. It is readable by root and members of the +# notifications-webhook group only; add users with: +# sudo usermod -aG notifications-webhook USERNAME +# +# url = https://notifier.example.com/webhook/notifications/myscript +# api_key = your-key-here \ No newline at end of file diff --git a/pkg/usr/bin/send-notification b/pkg/usr/bin/send-notification new file mode 100644 index 0000000..495812a --- /dev/null +++ b/pkg/usr/bin/send-notification @@ -0,0 +1,7 @@ +#!/usr/bin/python3 +import sys + +from send_notifications_webhook.cli import main + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file