This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
/
artifacts.go
151 lines (134 loc) · 3.59 KB
/
artifacts.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package server
import (
"encoding/json"
"fmt"
"github.com/gimlet-io/gimletd/dx"
"github.com/gimlet-io/gimletd/model"
"github.com/gimlet-io/gimletd/store"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"net/http"
"strconv"
"time"
)
func saveArtifact(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
store := ctx.Value("store").(*store.Store)
var artifact dx.Artifact
json.NewDecoder(r.Body).Decode(&artifact)
artifact.ID = fmt.Sprintf("%s-%s", artifact.Version.RepositoryName, uuid.New().String())
artifact.Created = time.Now().Unix()
event, err := model.ToEvent(artifact)
if err != nil {
logrus.Errorf("cannot convert to artifact model: %s", err)
http.Error(w, http.StatusText(500), 500)
return
}
savedEvent, err := store.CreateEvent(event)
if err != nil {
logrus.Errorf("cannot save artifact: %s", err)
http.Error(w, http.StatusText(500), 500)
return
}
savedArtifact, err := model.ToArtifact(savedEvent)
artifactStr, err := json.Marshal(savedArtifact)
if err != nil {
logrus.Errorf("cannot serialize artifact: %s", err)
http.Error(w, http.StatusText(500), 500)
return
}
w.WriteHeader(http.StatusCreated)
w.Write(artifactStr)
}
func getArtifacts(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
store := ctx.Value("store").(*store.Store)
var limit, offset int
var since, until *time.Time
var repo, branch string
var event *dx.GitEvent
var sourceBranch string
var sha []string
params := r.URL.Query()
if val, ok := params["limit"]; ok {
l, err := strconv.Atoi(val[0])
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+" - "+err.Error(), http.StatusBadRequest)
return
}
limit = l
}
if val, ok := params["offset"]; ok {
o, err := strconv.Atoi(val[0])
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+" - "+err.Error(), http.StatusBadRequest)
return
}
offset = o
}
if val, ok := params["since"]; ok {
t, err := time.Parse(time.RFC3339, val[0])
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+" - "+err.Error(), http.StatusBadRequest)
return
}
since = &t
}
if val, ok := params["until"]; ok {
t, err := time.Parse(time.RFC3339, val[0])
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+" - "+err.Error(), http.StatusBadRequest)
return
}
until = &t
}
if val, ok := params["repository"]; ok {
repo = val[0]
}
if val, ok := params["branch"]; ok {
branch = val[0]
}
if val, ok := params["sourceBranch"]; ok {
sourceBranch = val[0]
}
if val, ok := params["sha"]; ok {
sha = val
}
if val, ok := params["event"]; ok {
event = dx.PushPtr()
err := event.UnmarshalJSON([]byte(`"` + val[0] + `"`))
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+" - "+err.Error(), http.StatusBadRequest)
return
}
}
events, err := store.Artifacts(
repo, branch,
event,
sourceBranch,
sha,
limit, offset, since, until)
if err != nil {
logrus.Errorf("cannot get artifacts: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
artifacts := []*dx.Artifact{}
for _, a := range events {
artifact, err := model.ToArtifact(a)
if err != nil {
logrus.Errorf("cannot deserialize artifact: %s", err)
http.Error(w, http.StatusText(500), 500)
return
}
artifacts = append(artifacts, artifact)
}
artifactsStr, err := json.Marshal(artifacts)
if err != nil {
logrus.Errorf("cannot serialize artifacts: %s", err)
http.Error(w, http.StatusText(500), 500)
return
}
w.WriteHeader(http.StatusOK)
w.Write(artifactsStr)
}