added labels

This commit is contained in:
ston1th 2019-12-06 23:20:20 +01:00
commit bfb79c3f3c
5 changed files with 243 additions and 29 deletions

View file

@ -4,10 +4,12 @@ package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
@ -16,6 +18,10 @@ const (
giteaAPI = "api/v1"
)
func itoa(i int64) string {
return strconv.FormatInt(i, 10)
}
func getNS() (string, error) {
if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
return ns, nil
@ -36,13 +42,25 @@ func logError(w http.ResponseWriter, msg string) {
func contains(a []string, s string) bool {
for _, v := range a {
if strings.Contains(s, v) {
if v == s {
return true
}
}
return false
}
func getLabel(l string) (label, color string) {
a := strings.SplitN(l, ":", 2)
switch len(a) {
case 1:
label = a[0]
case 2:
label = a[0]
color = a[1]
}
return
}
func detectAPI(r *http.Request) string {
if r.Header.Get(giteaEvent) != "" {
return giteaAPI
@ -53,14 +71,13 @@ func detectAPI(r *http.Request) string {
return ""
}
func readBody(r *http.Request, i interface{}) (body []byte, err error) {
defer r.Body.Close()
body, err = ioutil.ReadAll(r.Body)
func readBody(r io.ReadCloser, i interface{}) (body []byte, err error) {
defer r.Close()
body, err = ioutil.ReadAll(r)
if err != nil {
return
}
if debug {
log.Printf("debug headers: %#v", r.Header)
log.Printf("debug: %s", string(body))
}
err = json.Unmarshal(body, i)