move git stuff out of main

This commit is contained in:
ston1th 2019-12-10 00:04:27 +01:00
commit a021fab543
5 changed files with 250 additions and 230 deletions

View file

@ -34,7 +34,8 @@ Commit: <Pull-Request Head Commit Hash>
## Issue Comment Event ## Issue Comment Event
``` ```
X-Interceptor-Comment-Action: "/test-deploy" X-Interceptor-Comment: "/test-deploy"
X-Interceptor-Comment-Action: []string{"created"}
X-Interceptor-Label: "test-deploy:#aabbcc" X-Interceptor-Label: "test-deploy:#aabbcc"
X-Interceptor-Remove-Labels: []string{"test", "deploy"} X-Interceptor-Remove-Labels: []string{"test", "deploy"}
``` ```

219
git.go Normal file
View file

@ -0,0 +1,219 @@
// Copyright (C) 2019 Marius Schellenberger
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
contentType = "Content-Type"
jsonType = "application/json"
secretName = "git-auth"
)
type Git struct {
sec typedcorev1.SecretInterface
cl *http.Client
}
func (g *Git) getSecret(host string) (username, password string, err error) {
sec, err := g.sec.Get(secretName, metav1.GetOptions{})
if err != nil {
return
}
s, ok := sec.Data[host]
if !ok {
err = fmt.Errorf("no key found for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return
}
a := strings.SplitN(string(s), ":", 2)
if len(a) < 2 {
err = fmt.Errorf("missing credentials for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return
}
username = a[0]
password = a[1]
return
}
func (g *Git) send(req *http.Request) (*http.Response, error) {
user, pass, err := g.getSecret(req.URL.Host)
if err != nil {
return nil, err
}
req.SetBasicAuth(user, pass)
req.Header.Set(contentType, jsonType)
return g.cl.Do(req)
}
func (g *Git) getPullRequest(repo Repository, api, id string) (pr PullRequest, err error) {
u, err := url.Parse(repo.CloneURL)
if err != nil {
return
}
u.Path = strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"pulls", id}, "/")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
resp, err := g.send(req)
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
err = errors.New("error getting pull request")
return
}
_, err = readBody(resp.Body, &pr)
return
}
func (g *Git) addLabel(repo Repository, api, label, color string) (labelID int64, err error) {
labelID = -1
u, err := url.Parse(repo.CloneURL)
if err != nil {
err = fmt.Errorf("error parsing repo URL: %s", err)
return
}
repoLabels := strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"labels"}, "/")
u.Path = repoLabels
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
resp, err := g.send(req)
if err != nil {
err = fmt.Errorf("error getting labels of %s: %s", u, err)
return
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("error getting labels of %s", u)
return
}
var labels []Label
_, err = readBody(resp.Body, &labels)
if err != nil {
return
}
for _, l := range labels {
if l.Name == label {
labelID = l.ID
break
}
}
if labelID >= 0 {
return
}
buf := new(bytes.Buffer)
l := &Label{Name: label, Color: color}
err = json.NewEncoder(buf).Encode(l)
if err != nil {
return
}
req, err = http.NewRequest("POST", u.String(), buf)
if err != nil {
return
}
resp, err = g.send(req)
if err != nil {
err = fmt.Errorf("error creating label %s for %s: %s", label, u, err)
return
}
if resp.StatusCode != http.StatusCreated {
err = fmt.Errorf("error creating label %s for %s", label, u)
}
var createdLabel Label
_, err = readBody(resp.Body, &createdLabel)
labelID = createdLabel.ID
return
}
func (g *Git) setLabel(repo Repository, api, id, label, color string, remove []string) (err error) {
labelID, err := g.addLabel(repo, api, label, color)
if err != nil {
return
}
u, err := url.Parse(repo.CloneURL)
if err != nil {
return
}
u.Path = strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"issues", id, "labels"}, "/")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
resp, err := g.send(req)
if err != nil {
return fmt.Errorf("error getting issue labels of %s: %s", u, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error getting issue labels of %s", u)
}
var labels []Label
_, err = readBody(resp.Body, &labels)
if err != nil {
return
}
plint := &PostLabelsInt{[]int64{labelID}}
plstring := &PostLabelsString{[]string{label}}
var rm, found bool
for _, l := range labels {
if contains(remove, l.Name) {
rm = true
} else {
if l.Name == label {
found = true
}
plint.Labels = append(plint.Labels, l.ID)
plstring.Labels = append(plstring.Labels, l.Name)
}
}
if !rm && found {
return
}
buf := new(bytes.Buffer)
switch api {
case githubAPI:
err = json.NewEncoder(buf).Encode(plstring)
case giteaAPI:
err = json.NewEncoder(buf).Encode(plint)
}
if err != nil {
return
}
req, err = http.NewRequest("PUT", u.String(), buf)
if err != nil {
return
}
resp, err = g.send(req)
if err != nil {
return fmt.Errorf("error setting issue label %s for %s: %s", label, u, err)
}
if resp.StatusCode != http.StatusOK {
b, _ := readBody(resp.Body, nil)
err = fmt.Errorf("error setting issue label %s for %s: %s", label, u, string(b))
}
return
}

View file

@ -80,6 +80,8 @@ func readBody(r io.ReadCloser, i interface{}) (body []byte, err error) {
if debug { if debug {
log.Printf("debug: %s", string(body)) log.Printf("debug: %s", string(body))
} }
if i != nil {
err = json.Unmarshal(body, i) err = json.Unmarshal(body, i)
}
return return
} }

235
main.go
View file

@ -3,24 +3,16 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"reflect" "reflect"
"regexp" "regexp"
"strings"
"time" "time"
//corev1 "k8s.io/api/core/v1" //corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
) )
@ -28,230 +20,25 @@ var debug = false
const ( const (
prAction = "X-Interceptor-Pr-Action" prAction = "X-Interceptor-Pr-Action"
pushAction = "X-Interceptor-Push-Action" pushRef = "X-Interceptor-Push-Ref"
commentHeader = "X-Interceptor-Comment"
commentAction = "X-Interceptor-Comment-Action" commentAction = "X-Interceptor-Comment-Action"
labelHeader = "X-Interceptor-Label" labelHeader = "X-Interceptor-Label"
removeLabelsHeader = "X-Interceptor-Remove-Labels" removeLabelsHeader = "X-Interceptor-Remove-Labels"
contentType = "Content-Type"
jsonType = "application/json"
githubEvent = "X-GitHub-Event" githubEvent = "X-GitHub-Event"
giteaEvent = "X-Gitea-Event" giteaEvent = "X-Gitea-Event"
issueHeader = "Issue" issueHeader = "Issue"
commitHeader = "Commit" commitHeader = "Commit"
secretName = "git-auth"
) )
func init() { func init() {
log.SetFlags(log.Flags() | log.Lshortfile) log.SetFlags(log.Flags() | log.Lshortfile)
} }
func (h *Handler) getSecret(host string) (username, password string, err error) {
sec, err := h.sec.Get(secretName, metav1.GetOptions{})
if err != nil {
return
}
s, ok := sec.Data[host]
if !ok {
err = fmt.Errorf("no key found for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return
}
a := strings.SplitN(string(s), ":", 2)
if len(a) < 2 {
err = fmt.Errorf("missing credentials for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return
}
username = a[0]
password = a[1]
return
}
func (h *Handler) getPullRequest(repo Repository, api, id string) (*PullRequest, error) {
u, err := url.Parse(repo.CloneURL)
if err != nil {
return nil, err
}
u.Path = strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"pulls", id}, "/")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set(contentType, jsonType)
user, pass, err := h.getSecret(u.Host)
if err != nil {
return nil, err
}
req.SetBasicAuth(user, pass)
resp, err := h.cl.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("error getting pull request")
}
pr := new(PullRequest)
_, err = readBody(resp.Body, pr)
if err != nil {
return nil, err
}
return pr, nil
}
func (h *Handler) addLabel(repo Repository, api, label, color string) (labelID int64, err error) {
labelID = -1
u, err := url.Parse(repo.CloneURL)
if err != nil {
err = fmt.Errorf("error parsing repo URL: %s", err)
return
}
repoLabels := strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"labels"}, "/")
u.Path = repoLabels
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
user, pass, err := h.getSecret(u.Host)
if err != nil {
return
}
req.SetBasicAuth(user, pass)
req.Header.Set(contentType, jsonType)
resp, err := h.cl.Do(req)
if err != nil {
err = fmt.Errorf("error getting labels of %s: %s", u, err)
return
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("error getting labels of %s", u)
return
}
var labels []Label
_, err = readBody(resp.Body, &labels)
if err != nil {
return
}
for _, l := range labels {
if l.Name == label {
labelID = l.ID
break
}
}
if labelID >= 0 {
return
}
buf := new(bytes.Buffer)
l := &Label{Name: label, Color: color}
err = json.NewEncoder(buf).Encode(l)
if err != nil {
return
}
req, err = http.NewRequest("POST", u.String(), buf)
if err != nil {
return
}
req.Header.Set(contentType, jsonType)
req.SetBasicAuth(user, pass)
resp, err = h.cl.Do(req)
if err != nil {
err = fmt.Errorf("error creating label %s for %s: %s", label, u, err)
return
}
if resp.StatusCode != http.StatusCreated {
err = fmt.Errorf("error creating label %s for %s", label, u)
}
var createdLabel Label
_, err = readBody(resp.Body, &createdLabel)
labelID = createdLabel.ID
return
}
func (h *Handler) setLabel(repo Repository, api, id, label, color string, remove []string) (err error) {
labelID, err := h.addLabel(repo, api, label, color)
if err != nil {
return
}
u, err := url.Parse(repo.CloneURL)
if err != nil {
return
}
u.Path = strings.Join([]string{api, "repos",
repo.Owner.Login, repo.Name,
"issues", id, "labels"}, "/")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
user, pass, err := h.getSecret(u.Host)
if err != nil {
return
}
req.SetBasicAuth(user, pass)
req.Header.Set(contentType, jsonType)
resp, err := h.cl.Do(req)
if err != nil {
return fmt.Errorf("error getting issue labels of %s: %s", u, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error getting issue labels of %s", u)
}
var labels []Label
_, err = readBody(resp.Body, &labels)
if err != nil {
return
}
pl := &PostLabels{[]int64{labelID}}
var rm, found bool
for _, l := range labels {
if contains(remove, l.Name) {
rm = true
} else {
if l.Name == label {
found = true
}
pl.Labels = append(pl.Labels, l.ID)
}
}
if !rm && found {
return
}
buf := new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(pl)
if err != nil {
return
}
req, err = http.NewRequest("PUT", u.String(), buf)
if err != nil {
return
}
req.SetBasicAuth(user, pass)
req.Header.Set(contentType, jsonType)
resp, err = h.cl.Do(req)
if err != nil {
return fmt.Errorf("error setting issue label %s for %s: %s", label, u, err)
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("error setting issue label %s for %s", label, u)
}
return
}
type Handler struct { type Handler struct {
sec typedcorev1.SecretInterface git *Git
cl *http.Client
} }
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -307,7 +94,7 @@ func (h *Handler) pushHandler(w http.ResponseWriter, r *http.Request) {
logError(w, err.Error()) logError(w, err.Error())
return return
} }
push := r.Header.Get(pushAction) push := r.Header.Get(pushRef)
//if contains(r.Header[pushAction], pb.Ref) { //if contains(r.Header[pushAction], pb.Ref) {
if push != "" && push == pb.Ref { if push != "" && push == pb.Ref {
h := w.Header() h := w.Header()
@ -326,8 +113,12 @@ func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if !contains(r.Header[commentAction], cb.Action) {
w.WriteHeader(http.StatusBadRequest)
return
}
//if contains(r.Header[commentAction], cb.Comment.Body) { //if contains(r.Header[commentAction], cb.Comment.Body) {
comment := r.Header.Get(commentAction) comment := r.Header.Get(commentHeader)
if comment == "" { if comment == "" {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
@ -351,7 +142,7 @@ func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) {
labels = append(labels, l.Name) labels = append(labels, l.Name)
} }
if !contains(labels, label) { if !contains(labels, label) {
err := h.setLabel(cb.Repository, api, issue, label, color, r.Header[removeLabelsHeader]) err := h.git.setLabel(cb.Repository, api, issue, label, color, r.Header[removeLabelsHeader])
if err != nil { if err != nil {
logError(w, err.Error()) logError(w, err.Error())
return return
@ -359,7 +150,7 @@ func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
pr, err := h.getPullRequest(cb.Repository, api, issue) pr, err := h.git.getPullRequest(cb.Repository, api, issue)
if err != nil { if err != nil {
logError(w, err.Error()) logError(w, err.Error())
return return
@ -402,12 +193,14 @@ func main() {
log.Fatalf("error getting current namespace %s\n", err) log.Fatalf("error getting current namespace %s\n", err)
} }
h := &Handler{ h := &Handler{
&Git{
sec: cl.CoreV1().Secrets(ns), sec: cl.CoreV1().Secrets(ns),
cl: &http.Client{Transport: &http.Transport{ cl: &http.Client{Transport: &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
}).DialContext, }).DialContext,
}}, }},
},
} }
http.Handle("/", h) http.Handle("/", h)

View file

@ -23,7 +23,11 @@ type Label struct {
Name string `json:"name"` Name string `json:"name"`
} }
type PostLabels struct { type PostLabelsString struct {
Labels []string `json:"labels"`
}
type PostLabelsInt struct {
Labels []int64 `json:"labels"` Labels []int64 `json:"labels"`
} }
@ -33,6 +37,7 @@ type PushBody struct {
} }
type CommentBody struct { type CommentBody struct {
Action string `json:"action"`
Issue Issue `json:"issue"` Issue Issue `json:"issue"`
Comment Comment `json:"comment"` Comment Comment `json:"comment"`
Repository Repository `json:"repository"` Repository Repository `json:"repository"`