ADD: initial commit
Some checks failed
continuous-integration/drone Build is failing

This commit is contained in:
Dominik Meyer 2024-03-05 17:47:07 +01:00
commit 6ee392e55e
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
12 changed files with 311 additions and 0 deletions

106
.drone.yml Normal file
View File

@ -0,0 +1,106 @@
kind: pipeline
type: kubernetes
name: build-amd64
platform:
arch: amd64
node_selector:
kubernetes.io/arch: amd64
volumes:
- name: fedhq-ca-crt
config_map:
name: fedhq-ca-crt
default_mode: 420 # same as 644 in octal, or u+w,a+r
optional: false
steps:
- name: build amd64
image: quay.io/buildah/stable
privileged: true
volumes:
- name: fedhq-ca-crt
path: /etc/ssl/certs2/
commands:
- scripts/setupEnvironment.sh
- buildah bud -t "registry.cloud.federationhq.de/webhook2ntfy:latest-amd64" --arch amd64 .
- buildah push --all registry.cloud.federationhq.de/webhook2ntfy:latest-amd64
---
kind: pipeline
type: kubernetes
name: build-arm64
platform:
arch: arm64
node_selector:
kubernetes.io/arch: arm64
volumes:
- name: fedhq-ca-crt
config_map:
name: fedhq-ca-crt
default_mode: 420 # same as 644 in octal, or u+w,a+r
optional: false
steps:
- name: build arm64
image: quay.io/buildah/stable
privileged: true
volumes:
- name: fedhq-ca-crt
path: /etc/ssl/certs2/
commands:
- scripts/setupEnvironment.sh
- buildah bud -t "registry.cloud.federationhq.de/webhook2ntfy:latest-arm64" --arch arm64 .
- buildah push --all registry.cloud.federationhq.de/webhook2ntfy:latest-arm64
---
kind: pipeline
type: kubernetes
name: push
node_selector:
kubernetes.io/arch: amd64
volumes:
- name: fedhq-ca-crt
config_map:
name: fedhq-ca-crt
default_mode: 420 # same as 644 in octal, or u+w,a+r
optional: false
steps:
- name: push
image: quay.io/buildah/stable
privileged: true
environment:
USERNAME:
from_secret: username
PASSWORD:
from_secret: password
volumes:
- name: fedhq-ca-crt
path: /etc/ssl/certs2/
commands:
- scripts/setupEnvironment.sh
- buildah manifest create webhook2ntfy:latest registry.cloud.federationhq.de/webhook2ntfy:latest-arm64 registry.cloud.federationhq.de/webhook2ntfy:latest-amd64
- buildah manifest push --all webhook2ntfy:latest docker://registry.cloud.federationhq.de/webhook2ntfy:latest
#- buildah login -u $${USERNAME} -p $${PASSWORD} registry.hub.docker.com
#- buildah manifest push --all imapfilter:latest docker://registry.hub.docker.com/byterazor/imapfilter:latest
- buildah manifest rm imapfilter:latest
depends_on:
- build-amd64
- build-arm64
---
kind: secret
name: username
get:
path: docker
name: username
---
kind: secret
name: password
get:
path: docker
name: secret

18
Containerfile Normal file
View File

@ -0,0 +1,18 @@
FROM alpine
RUN apk update && apk add --no-cache tini bash ca-certificates openssl perl perl-mojolicious
RUN mkdir -p /opt/webhook2_ntfy/
ADD . /opt/webhook2_ntfy/
RUN chmod a+x /opt/webhook2_ntfy/script/webhook2_ntfy
WORKDIR /opt/webhook2_ntfy
RUN addgroup webhook2ntfy && adduser -D -G webhook2ntfy webhook2ntfy
ADD entrypoint.sh /entrypoint.sh
RUN chmod a+x /entrypoint.sh
USER webhook2ntfy
ENTRYPOINT ["/sbin/tini", "--", "/entrypoint.sh"]

5
entrypoint.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
cd /opt/webhook2_ntfy
script/webhook2_ntfy daemon -l http://*:3000

32
lib/Webhook2Ntfy.pm Normal file
View File

@ -0,0 +1,32 @@
package Webhook2Ntfy;
use Mojo::Base 'Mojolicious', -signatures;
# This method will run once at server start
sub startup ($self) {
# Configure the application
$self->secrets($ENV{SECRETS});
my $webhook = $ENV{WEBHOOK_PATH};
if (!$webhook || length($webhook) == 0 )
{
$self->log->fatal("no webhook provided in WEBHOOK_PATH");
exit(-1);
}
elsif (length($webhook) < 20)
{
$self->log->fatal("please provide a more complex webhook path in WEBHOOK_PATH");
exit(-1);
}
$self->log->info("using webhook path: " . $webhook);
# Router
my $r = $self->routes;
# Normal route to controller
$r->post($webhook)->to('Webhook#call');
}
1;

View File

@ -0,0 +1,105 @@
package Webhook2Ntfy::Controller::Webhook;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use JSON;
use Data::Dumper;
use URI::Escape;
use Encode qw/encode decode/;
use MIME::Base64;
use LWP::UserAgent;
use HTTP::Request::Common qw{ POST };
sub publish
{
my $self = shift;
my $msg = shift;
my $ntfyServer = $ENV{NTFY_SERVER};
my $ntfyToken = $self->prepareAuthToken($ENV{NTFY_TOKEN});
my $ua = LWP::UserAgent->new();
my $url = $ntfyServer . "/?auth=$ntfyToken";
my $response = $ua->post( $url, Markdown=>'yes', Content => to_json($msg));
}
sub prepareAuthToken
{
my $self = shift;
my $token = shift;
my $auth=encode_base64("".":".$token, "");
my $authString = encode_base64("Basic " . $auth, "");
$authString =~ s/=+$//;
return $authString;
}
sub redmine2ntfy
{
my $self = shift;
my $data = shift;
$data->{text} =~ /^<(.*)\|(.+)>\+\-\+(.+)\+<(.*)\|(.*)>\+.*\+by\+(.*)$/;
my $projectUrl=$1;
my $projectName=$2;
my $entity = $3;
my $entityUrl=$4;
my $entityName=$5;
my $author=$6;
$author =~ s/\+/ /g;
my $status;
if ($entity eq "Issue")
{
$entityName=~s/\+/ /g;
}
my $title = "$entity update for $projectName";
my $text = "There has been an $entity update for project [$projectName]($projectUrl) by $author on [$entityName]($entityUrl)";
my $message = {
topic => $data->{channel},
title => $title,
message => $text
};
}
sub toNtfyMessage
{
my $self = shift;
my $data = shift;
if ($ENV{IS_REDMINE})
{
return $self->redmine2ntfy($data);
}
my $message = {
topic => $data->{channel},
message => $data->{text}
};
return $message;
}
# This action will render a template
sub call {
my $self = shift;
my $body = $self->req->body;
$body=uri_unescape($body);
$body=substr($body,8,length($body));
my $data=from_json($body);
$self->publish($self->toNtfyMessage($data));
# Render template "example/welcome.html.ep" with message
$self->render(msg => 'accepted');
}
1;

11
public/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the Mojolicious real-time web framework!</title>
</head>
<body>
<h2>Welcome to the Mojolicious real-time web framework!</h2>
This is the static document "public/index.html",
<a href="/">click here</a> to get back to the start.
</body>
</html>

11
script/webhook2_ntfy Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::File qw(curfile);
use lib curfile->dirname->sibling('lib')->to_string;
use Mojolicious::Commands;
# Start command line interface for application
Mojolicious::Commands->start_app('Webhook2Ntfy');

3
scripts/setupEnvironment.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
cp /etc/ssl/certs2/federationHQ-CA.pem /etc/pki/ca-trust/source/anchors/
update-ca-trust

9
t/basic.t Normal file
View File

@ -0,0 +1,9 @@
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('Webhook2Ntfy');
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
done_testing();

View File

@ -0,0 +1,5 @@
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>

View File

@ -0,0 +1,3 @@
% layout 'default';
% title 'Message Accepted';
<h2><%= $msg %></h2>

3
webhook2_ntfy.yml Normal file
View File

@ -0,0 +1,3 @@
---
secrets:
- 6d0aaa833652976c0d076e7567195a8d258f57e3