initial import of an packager.io to mattermost webhook adapter

master
Felix Kronlage 6 years ago
commit 61fc5130db

@ -0,0 +1,26 @@
BSD 2-Clause License
Copyright (c) 2017, Felix Kronlage <fkr@hazardous.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -0,0 +1,26 @@
# Adapter for Packager.io to Mattermost
This is an Adapter that translates Packager.io webhook packets to the
Mattermost API.
# Data
Typical packager.io Datapacket:
Content={
"event":"package","repository_uuid":"74036391",
"repository_slug":"idb-project/the-idb","filename":"the-idb_1.7.0-1500714459.05db224.xenial_amd64.deb",
"commit":"05db224210523c8a7cb3221dbaf393dd8ba4cbea",
"branch":"1.7",
"tag":"1.7.0",
"tagged":false,
"real_tag":null,
"distribution":"ubuntu-16.04",
"package_url":"https://pkgr-production-deb.s3.amazonaws.com/gh/idb-project/the-idb/pool/t/th/the-idb_1.7.0-1500714459.05db224.xenial_amd64.deb",
"upstream_url":"https://github.com/idb-project/the-idb/commit/05db224210523c8a7cb3221dbaf393dd8ba4cbea",
"build_url":"https://packager.io/gh/idb-project/the-idb/build_runs/5423112"}
# Author
Felix Kronlage <fkr@hazardous.org>

@ -0,0 +1,6 @@
[general]
mattermost = "https://mattermost-server/hooks/hooktoken"
channel = "town-square"
username = "Happy Builder"
iconurl = "https://example.com/usericon.png"
listen = ":5050"

@ -0,0 +1,121 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
type AdapterConfig struct {
MattermostServer string
Channel string
Username string
IconUrl string
Listen string
}
type PackagerJson struct {
Event string `json:"event"`
RepositoryUUID string `json:"repository_uuid"`
RepositorySlug string `json:"repository_slug"`
Filename string `json:"filename"`
Commit string `json:"commit"`
Branch string `json:"branch"`
Tag string `json:"tag"`
Tagged bool `json:"tagged"`
RealTag string `json:"real_tag"`
Distribution string `json:"distribution"`
PackageURL string `json:"package_url"`
UpstreamURL string `json:"upstream_url"`
BuildURL string `json:"build_url"`
}
type MattermostJson struct {
Channel string `json:"channel"`
Text string `json:"text"`
Username string `json:"username"`
IconUrl string `json:"icon_url"`
}
func HandlePackagerPost(rw http.ResponseWriter, req *http.Request) {
switch req.Method {
case "POST":
dec := json.NewDecoder(req.Body)
packageJson := new(PackagerJson)
err := dec.Decode(&packageJson)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(packageJson)
retjs, err := json.Marshal(packageJson)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
text := fmt.Sprintf("build succeeded! Branch: %s - Tag: %s - %s - %s - %s", packageJson.Branch,
packageJson.Tag,
packageJson.Commit,
packageJson.Distribution,
packageJson.PackageURL)
packageMattermost := MattermostJson{Channel: config.Channel, Username: config.Username,
IconUrl: config.IconUrl,
Text: text}
payload := new(bytes.Buffer)
json.NewEncoder(payload).Encode(packageMattermost)
res, _ := http.Post(config.MattermostServer,
"application/json; charset=utf-8", payload)
io.Copy(os.Stdout, res.Body)
fmt.Fprintln(rw, string(retjs))
}
}
var config AdapterConfig
func main() {
viper.SetConfigName("app")
viper.AddConfigPath("config")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Config file not found or error parsing\n\n: %s", err)
} else {
config.Channel = viper.GetString("general.channel")
config.Username = viper.GetString("general.username")
config.IconUrl = viper.GetString("general.iconurl")
config.MattermostServer = viper.GetString("general.mattermost")
config.Listen = viper.GetString("general.listen")
fmt.Printf("\nUsing config:\n mattermost = %s\n channel = %s\n" +
" username = %s\n" +
" iconurl = %s\n\n" +
"Listening on port: %s\n",
config.MattermostServer,
config.Channel,
config.Username,
config.IconUrl,
config.Listen)
}
router := mux.NewRouter()
router.HandleFunc("/hook", HandlePackagerPost).Methods("POST")
log.Fatal(http.ListenAndServe(config.Listen, router))
}
Loading…
Cancel
Save