-
Notifications
You must be signed in to change notification settings - Fork 469
/
PipelineStatusTimelineRestHandler.go
83 lines (76 loc) · 2.7 KB
/
PipelineStatusTimelineRestHandler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package restHandler
import (
"fmt"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/app/status"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
)
type PipelineStatusTimelineRestHandler interface {
FetchTimelines(w http.ResponseWriter, r *http.Request)
}
type PipelineStatusTimelineRestHandlerImpl struct {
logger *zap.SugaredLogger
pipelineStatusTimelineService status.PipelineStatusTimelineService
enforcerUtil rbac.EnforcerUtil
enforcer casbin.Enforcer
}
func NewPipelineStatusTimelineRestHandlerImpl(logger *zap.SugaredLogger,
pipelineStatusTimelineService status.PipelineStatusTimelineService, enforcerUtil rbac.EnforcerUtil,
enforcer casbin.Enforcer) *PipelineStatusTimelineRestHandlerImpl {
return &PipelineStatusTimelineRestHandlerImpl{
logger: logger,
pipelineStatusTimelineService: pipelineStatusTimelineService,
enforcerUtil: enforcerUtil,
enforcer: enforcer,
}
}
func (handler *PipelineStatusTimelineRestHandlerImpl) FetchTimelines(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
envId, err := strconv.Atoi(vars["envId"])
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
wfrId := 0
wfrIdParam := r.URL.Query().Get("wfrId")
if len(wfrIdParam) != 0 {
wfrId, err = strconv.Atoi(wfrIdParam)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
}
showTimeline := false
showTimelineParam := r.URL.Query().Get("showTimeline")
if len(showTimelineParam) > 0 {
showTimeline, err = strconv.ParseBool(showTimelineParam)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
}
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(appId)
token := r.Header.Get("token")
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
return
}
timelines, err := handler.pipelineStatusTimelineService.FetchTimelines(appId, envId, wfrId, showTimeline)
if err != nil {
handler.logger.Errorw("error in getting cd pipeline status timelines by wfrId", "err", err, "wfrId", wfrId)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, timelines, http.StatusOK)
return
}