-
Notifications
You must be signed in to change notification settings - Fork 414
/
ui.go
120 lines (104 loc) · 3.64 KB
/
ui.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package api
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/metrics"
"github.com/ovh/cds/engine/api/navbar"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/services"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)
func (api *API) getNavbarHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
consumer := getUserConsumer(ctx)
data, err := navbar.LoadNavbarData(api.mustDB(), api.Cache, *consumer.AuthConsumerUser.AuthentifiedUser)
if err != nil {
return err
}
return service.WriteJSON(w, data, http.StatusOK)
}
}
func (api *API) getApplicationOverviewHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
srvs, err := services.LoadAllByType(ctx, api.mustDB(), sdk.TypeElasticsearch)
if err != nil {
return err
}
vars := mux.Vars(r)
projectKey := vars[permProjectKey]
appName := vars["applicationName"]
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
app, err := application.LoadByName(ctx, tx, projectKey, appName)
if err != nil {
return sdk.WrapError(err, "unable to load application")
}
usage, err := loadApplicationUsage(ctx, tx, projectKey, appName)
if err != nil {
return sdk.WrapError(err, "cannot load application usage")
}
app.Usage = &usage
appOverview := sdk.ApplicationOverview{
Graphs: make([]sdk.ApplicationOverviewGraph, 0, 3),
History: make(map[string][]sdk.WorkflowRunSummary, len(app.Usage.Workflows)),
}
if len(srvs) > 0 {
// Get metrics
mVulnerability, err := metrics.GetMetrics(ctx, tx, projectKey, app.ID, sdk.MetricKeyVulnerability)
if err != nil {
return sdk.WrapError(err, "cannot list vulnerability metrics")
}
appOverview.Graphs = append(appOverview.Graphs, sdk.ApplicationOverviewGraph{
Type: sdk.MetricKeyVulnerability,
Datas: mVulnerability,
})
mTest, err := metrics.GetMetrics(ctx, tx, projectKey, app.ID, sdk.MetricKeyUnitTest)
if err != nil {
return sdk.WrapError(err, "cannot list Unit test metrics")
}
appOverview.Graphs = append(appOverview.Graphs, sdk.ApplicationOverviewGraph{
Type: sdk.MetricKeyUnitTest,
Datas: mTest,
})
}
if app.VCSServer != "" {
// GET VCS URL
// Get vcs info to known if we are on the default branch or not
client, err := repositoriesmanager.AuthorizedClient(ctx, tx, api.Cache, projectKey, app.VCSServer)
if err != nil {
return sdk.NewErrorWithStack(err, sdk.NewErrorFrom(sdk.ErrNoReposManagerClientAuth,
"cannot get repo client %s", app.VCSServer))
}
vcsRepo, err := client.RepoByFullname(ctx, app.RepositoryFullname)
if err != nil {
return sdk.WrapError(err, "unable to get repo")
}
appOverview.GitURL = vcsRepo.URL
defaultBranch, err := repositoriesmanager.DefaultBranch(ctx, client, app.RepositoryFullname)
if err != nil {
return sdk.WrapError(err, "unable to get default branch")
}
// GET LAST BUILD
tagFilter := make(map[string]string, 1)
tagFilter["git.branch"] = defaultBranch.DisplayID
for _, w := range app.Usage.Workflows {
runs, _, _, _, err := workflow.LoadRunsSummaries(ctx, tx, projectKey, w.Name, 0, 5, tagFilter)
if err != nil {
return sdk.WrapError(err, "unable to load runs")
}
appOverview.History[w.Name] = runs
}
}
if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}
return service.WriteJSON(w, appOverview, http.StatusOK)
}
}