Skip to content

Instantly share code, notes, and snippets.

@ugexe
Last active August 31, 2017 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/a7724886674ffef74f656f93f0b8a8cf to your computer and use it in GitHub Desktop.
Save ugexe/a7724886674ffef74f656f93f0b8a8cf to your computer and use it in GitHub Desktop.
Appveyor build status updates received from a web hook and delivered to an irc channel
#!/usr/bin/env perl6
use v6;
use IRC::Client;
use Cro::HTTP::Server;
use Cro::HTTP::Router;
use Cro::HTTP::BodyParser;
# Add the following section to an appveyor.yml
#
# notifications:
# - provider: Webhook
# url: http://p6.nu:3000/appveyor-webhook
# headers:
# User-Agent: appveyor-webhook
# on_build_success: false
# on_build_failure: true
# on_build_status_changed: true
my $webhook-server = Cro::HTTP::Server.new(
host => '23.239.16.90', # hostname binds to ipv6 and doesn't work, so use explicit ipv4 address
port => 3000,
body-parsers => [Cro::HTTP::BodyParser::JSON],
application => route {
state %previous-status-update;
# GET /status - view the last webhook payload
get -> 'status' {
content 'application/json', %previous-status-update;
}
# POST /appveyor-webhook - allow appveyor to send updates via a webhook
post -> 'appveyor-webhook' {
request-body -> %json (
:$eventName!,
:%eventData! [
:$branch!,
:$commitId!,
:$commitAuthor!,
:$commitMessage!,
:$buildUrl!,
*%_
],
) {
start {
my $text = "[Appveyor] $eventName for $branch@$commitId by $commitAuthor: `$commitMessage` ($buildUrl)";
IRC::Client.new(
nick => 'appveyorable',
host => (%*ENV<IRC_CLIENT_HOST> // 'irc.freenode.net'),
channels => <#perl6-dev>,
plugins => (
class :: does IRC::Client::Plugin {
method irc-connected ($) {
$.irc.send( :where<#perl6-dev> :$text );
$.irc.quit;
}
}.new,
),
).run;
}
state $lock = Lock.new;
$lock.protect: { %previous-status-update = %json }
content 'application/json', %json;
}
}
},
);
$webhook-server.start;
react whenever signal(SIGINT) {
$webhook-server.stop;
exit;
}
BEGIN die "Run `zef install Cro --/test` first\n" if (try require ::("Cro::HTTP::Server")) ~~ Nil;
BEGIN die "Run `zef install IRC::Client` first\n" if (try require ::("IRC::Client")) ~~ Nil;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment