-
Notifications
You must be signed in to change notification settings - Fork 35
/
pipeline.go
66 lines (58 loc) · 1.59 KB
/
pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package handlers
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
jenkinsv1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
jxclientv1 "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/sirupsen/logrus"
"github.com/unrolled/render"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PipelineHandler struct {
PAInterface jxclientv1.PipelineActivityInterface
Render *render.Render
Logger *logrus.Logger
}
func (h *PipelineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
owner := vars["owner"]
repo := vars["repo"]
branch := vars["branch"]
if strings.HasPrefix(branch, "pr-") {
branch = strings.ToUpper(branch)
}
build := vars["build"]
name := strings.ToLower(fmt.Sprintf("%s-%s-%s-%s", owner, repo, branch, build))
ctx := context.Background()
pa, err := h.PAInterface.Get(ctx, name, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if errors.IsNotFound(err) {
err := h.Render.HTML(w, http.StatusOK, "archived_logs", map[string]string{
"Owner": owner,
"Repository": repo,
"Branch": branch,
"Build": build,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
err = h.Render.HTML(w, http.StatusOK, "pipeline", struct {
Pipeline *jenkinsv1.PipelineActivity
}{
pa,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}