/
repo.go
175 lines (166 loc) · 4.23 KB
/
repo.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Created by zc on 2020/9/7.
*/
package api
import (
"encoding/json"
"errors"
"github.com/go-chi/chi"
"github.com/pkgms/go/ctr"
"github.com/zc2638/drone-control/global"
"github.com/zc2638/drone-control/store"
"gorm.io/gorm"
"net/http"
"strconv"
)
type RepoParams struct {
UserID int64 `json:"user_id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Timeout int64 `json:"timeout"`
Data string `json:"data"`
}
// RepoList returns an http.HandlerFunc that processes an
// http.Request to get the repo list.
func RepoList() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pageStr := r.URL.Query().Get("page")
sizeStr := r.URL.Query().Get("size")
size, _ := strconv.Atoi(sizeStr)
page, _ := strconv.Atoi(pageStr)
if page < 1 {
page = 1
}
if size < 1 {
size = 10
}
var repos []store.ReposData
db := global.GormDB().Limit(size).Offset((page - 1) * size).Find(&repos)
if db.Error != nil {
ctr.BadRequest(w, db.Error)
return
}
ctr.OK(w, repos)
}
}
// RepoInfo returns an http.HandlerFunc that processes an
// http.Request to get the repo details.
func RepoInfo() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(
chi.URLParam(r, "repo"), 10, 64)
if id < 1 {
ctr.BadRequest(w, errors.New("cannot find repo, repo_id is invalid"))
return
}
var repo store.ReposData
db := global.GormDB().Where(&store.ReposData{
ID: id,
}).First(&repo)
if db.Error != nil {
ctr.BadRequest(w, db.Error)
return
}
ctr.OK(w, repo)
}
}
// RepoInfoBySlug returns an http.HandlerFunc that processes an
// http.Request to get the repo details by namespace and name.
func RepoInfoBySlug() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := chi.URLParam(r, "namespace")
name := chi.URLParam(r, "name")
if namespace == "" || name == "" {
ctr.BadRequest(w, errors.New("space or name is empty"))
return
}
var repo store.ReposData
db := global.GormDB().Where(&store.ReposData{
Namespace: namespace,
Name: name,
}).First(&repo)
if db.Error != nil {
ctr.BadRequest(w, db.Error)
return
}
if db.RowsAffected == 0 {
ctr.OK(w, nil)
return
}
ctr.OK(w, repo)
}
}
// RepoApply returns an http.HandlerFunc that processes an
// http.Request to create a repo.
func RepoApply() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var data RepoParams
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
ctr.BadRequest(w, err)
return
}
defer r.Body.Close()
fullData, err := CheckClone(data.Data)
if err != nil {
ctr.BadRequest(w, errors.New("exec yaml failed"))
return
}
if _, err := parsePipeline(fullData, nil); err != nil {
ctr.BadRequest(w, err)
return
}
var timeout int64 = global.DefaultRepoTimeout // Minute
if data.Timeout > 0 {
timeout = data.Timeout
}
repo := &store.ReposData{
UserID: data.UserID,
Namespace: data.Namespace,
Name: data.Name,
Timeout: timeout,
Data: data.Data,
}
var current store.ReposData
db := global.GormDB().Where(&store.ReposData{
Namespace: data.Namespace,
Name: data.Name,
}).First(¤t)
if db.Error != nil && !errors.Is(gorm.ErrRecordNotFound, db.Error) {
ctr.BadRequest(w, db.Error)
return
}
if db.RowsAffected == 0 {
err = global.GormDB().Create(repo).Error
} else {
err = global.GormDB().Model(&store.ReposData{}).Where(&store.ReposData{
Namespace: data.Namespace,
Name: data.Name,
}).Updates(repo).Error
}
if err != nil {
ctr.BadRequest(w, err)
return
}
ctr.OK(w, repo)
}
}
// RepoDelete returns an http.HandlerFunc that processes an
// http.Request to delete the repo.
func RepoDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := chi.URLParam(r, "namespace")
name := chi.URLParam(r, "name")
if namespace == "" || name == "" {
ctr.BadRequest(w, errors.New("namespace or name is empty"))
return
}
if err := global.GormDB().Where(&store.ReposData{
Namespace: namespace,
Name: name,
}).Delete(&store.ReposData{}).Error; err != nil {
ctr.BadRequest(w, err)
return
}
ctr.Success(w)
}
}