-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.go
291 lines (269 loc) · 10.1 KB
/
branch.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"net/http"
"github.com/iver-wharf/wharf-api/v5/internal/ptrconv"
"github.com/iver-wharf/wharf-api/v5/pkg/model/database"
"github.com/iver-wharf/wharf-api/v5/pkg/model/request"
"github.com/iver-wharf/wharf-api/v5/pkg/modelconv"
"github.com/iver-wharf/wharf-core/pkg/ginutil"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type branchModule struct {
Database *gorm.DB
}
func (m branchModule) Register(g *gin.RouterGroup) {
projectBranch := g.Group("/project/:projectId/branch")
{
projectBranch.GET("", m.getProjectBranchListHandler)
projectBranch.PUT("", m.updateProjectBranchListHandler)
projectBranch.POST("", m.createProjectBranchHandler)
}
}
// getProjectBranchListHandler godoc
// @id getProjectBranchList
// @summary Get list of branches.
// @description Added in v5.0.0.
// @tags branch
// @produce json
// @param projectId path uint true "project ID" minimum(0)
// @param pretty query bool false "Pretty indented JSON output"
// @success 200 {object} response.PaginatedBranches "Branches"
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 404 {object} problem.Response "Project not found"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /project/{projectId}/branch [get]
func (m branchModule) getProjectBranchListHandler(c *gin.Context) {
projectID, ok := ginutil.ParseParamUint(c, "projectId")
if !ok {
return
}
if !validateProjectExistsByID(c, m.Database, projectID, "when fetching list of branches for project") {
return
}
var dbBranches []database.Branch
err := m.Database.
Where(&database.Branch{ProjectID: projectID}).
Find(&dbBranches).Error
if err != nil {
ginutil.WriteDBReadError(c, err, fmt.Sprintf(
"Failed fetching list of branches for project with ID %d.",
projectID))
return
}
dbDefaultBranch := findDefaultDBBranch(dbBranches)
renderJSON(c, http.StatusOK, modelconv.DBBranchListToPaginatedResponse(dbBranches, int64(len(dbBranches)), dbDefaultBranch))
}
// createProjectBranchHandler godoc
// @id createProjectBranch
// @summary Add branch to project.
// @description Adds a branch to the project, and allows you to set this new branch to be the default branch.
// @description Will ignore name collisions, and treat them as if the branch was just created anyway.
// @description Added in v5.0.0.
// @tags branch
// @accept json
// @produce json
// @param projectId path uint true "project ID" minimum(0)
// @param branch body request.Branch true "Branch object"
// @param pretty query bool false "Pretty indented JSON output"
// @success 201 {object} response.Branch "Created branch"
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 404 {object} problem.Response "Project not found"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /project/{projectId}/branch [post]
func (m branchModule) createProjectBranchHandler(c *gin.Context) {
projectID, ok := ginutil.ParseParamUint(c, "projectId")
if !ok {
return
}
var reqBranch request.Branch
if err := c.ShouldBindJSON(&reqBranch); err != nil {
ginutil.WriteInvalidBindError(c, err,
"One or more parameters failed to parse when reading the request body for branch object to create.")
return
}
dbProject, ok := fetchProjectByIDSlim(c, m.Database, projectID, "when creating branch for project")
if !ok {
return
}
tokenID := ptrconv.UintPtr(dbProject.TokenID)
var dbBranch database.Branch
err := m.Database.Transaction(func(tx *gorm.DB) error {
dbBranch = database.Branch{
ProjectID: projectID,
Default: reqBranch.Default,
Name: reqBranch.Name,
TokenID: tokenID,
}
if err := tx.Where(&database.Branch{
ProjectID: projectID,
Name: reqBranch.Name,
}).FirstOrCreate(&dbBranch).Error; err != nil {
return err
}
if reqBranch.Default {
if err := setDefaultBranchByName(tx, projectID, reqBranch.Name); err != nil {
return err
}
}
return tx.First(&dbBranch, dbBranch.BranchID).Error
})
if err != nil {
ginutil.WriteDBWriteError(c, err, fmt.Sprintf(
"Failed creating branch for project with ID %d.",
projectID))
return
}
renderJSON(c, http.StatusCreated, modelconv.DBBranchToResponse(dbBranch))
}
// updateProjectBranchListHandler godoc
// @id updateProjectBranchList
// @summary Resets branches for a project
// @description For a given project, it will remove all branches unlisted in the request,
// @description and add all branches from the request that are missing in the database.
// @description Effectively resetting the list of branches to the list from the HTTP request body.
// @description Useful when used via the provider APIs when importing a project.
// @description Added in v5.0.0.
// @tags branch
// @accept json
// @produce json
// @param projectId path uint true "project ID" minimum(0)
// @param branches body request.BranchListUpdate true "Branch update"
// @param pretty query bool false "Pretty indented JSON output"
// @success 200 {object} response.BranchList "Updated branches"
// @failure 400 {object} problem.Response "Bad request"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @failure 404 {object} problem.Response "Project not found"
// @failure 502 {object} problem.Response "Database is unreachable"
// @router /project/{projectId}/branch [put]
func (m branchModule) updateProjectBranchListHandler(c *gin.Context) {
projectID, ok := ginutil.ParseParamUint(c, "projectId")
if !ok {
return
}
var reqBranchListUpdate request.BranchListUpdate
if err := c.ShouldBindJSON(&reqBranchListUpdate); err != nil {
ginutil.WriteInvalidBindError(c, err,
"One or more parameters failed to parse when reading the request body for branch object array to update.")
return
}
dbProject, ok := fetchProjectByIDSlim(c, m.Database, projectID, "when updating branches")
if !ok {
return
}
dbBranchList, err := updateBranchList(m.Database, projectID, ptrconv.UintPtr(dbProject.TokenID), reqBranchListUpdate)
if err != nil {
ginutil.WriteDBWriteError(c, err, "Failed to update branches in database.")
return
}
resBranchList := modelconv.DBBranchListToResponse(dbBranchList.branches, dbBranchList.defaultBranch)
renderJSON(c, http.StatusOK, resBranchList)
}
type databaseBranchList struct {
defaultBranch *database.Branch
branches []database.Branch
}
func updateBranchList(db *gorm.DB, projectID uint, tokenID uint, reqUpdate request.BranchListUpdate) (databaseBranchList, error) {
if err := ensureOnlyRequestedBranchesExist(db, projectID, tokenID, reqUpdate); err != nil {
log.Error().
WithError(err).
Message("Failed to replace branch list. Transaction rolled back.")
return databaseBranchList{}, err
}
var dbNewBranches []database.Branch
if err := db.
Where(&database.Branch{ProjectID: projectID}, database.BranchFields.ProjectID).
Find(&dbNewBranches).Error; err != nil {
return databaseBranchList{}, err
}
return databaseBranchList{
defaultBranch: findDefaultDBBranch(dbNewBranches),
branches: dbNewBranches,
}, nil
}
func ensureOnlyRequestedBranchesExist(db *gorm.DB, projectID uint, tokenID uint, reqUpdate request.BranchListUpdate) error {
return db.Transaction(func(tx *gorm.DB) error {
var dbOldBranches []database.Branch
if err := tx.
Where(&database.Branch{ProjectID: projectID}, database.BranchFields.ProjectID).
Find(&dbOldBranches).Error; err != nil {
return err
}
wantBranchNamesSet := modelconv.ReqBranchUpdatesToSetOfNames(reqUpdate.Branches)
hasBranchNamesSet := modelconv.DBBranchesToSetOfNames(dbOldBranches)
branchNamesToDelete := hasBranchNamesSet.SetDiff(wantBranchNamesSet)
branchNamesToAdd := wantBranchNamesSet.SetDiff(hasBranchNamesSet)
if branchNamesToAdd.Len() > 0 {
if err := createBranchesWithNames(tx, projectID, tokenID, branchNamesToAdd.Slice()); err != nil {
return err
}
log.Info().
WithStringer("branchesAdded", branchNamesToAdd).
WithUint("project", projectID).
Message("Added branches to project when updating branches.")
}
if branchNamesToDelete.Len() > 0 {
if err := deleteBranchesByNames(tx, projectID, branchNamesToDelete.Slice()); err != nil {
return err
}
log.Info().
WithStringer("branchesDeleted", branchNamesToDelete).
WithUint("project", projectID).
Message("Deleted branches from project when updating branches.")
}
return setDefaultBranchByName(tx, projectID, reqUpdate.DefaultBranch)
})
}
func findDefaultDBBranch(dbBranches []database.Branch) *database.Branch {
for _, dbNewBranch := range dbBranches {
if dbNewBranch.Default {
return &dbNewBranch
}
}
return nil
}
func createBranchesWithNames(db *gorm.DB, projectID, tokenID uint, branchNames []string) error {
var dbBranches []database.Branch
for _, branchName := range branchNames {
dbBranches = append(dbBranches, database.Branch{
ProjectID: projectID,
TokenID: tokenID,
Name: branchName,
})
}
return db.Create(dbBranches).Error
}
func deleteBranchesByNames(db *gorm.DB, projectID uint, branchNames []string) error {
return db.
Where(&database.Branch{ProjectID: projectID}, database.BranchFields.ProjectID).
Where(database.BranchColumns.Name+" IN ?", asAnySlice(branchNames)).
Delete(&database.Branch{}).
Error
}
func setDefaultBranchByName(db *gorm.DB, projectID uint, defaultBranchName string) error {
return db.Transaction(func(tx *gorm.DB) error {
// ensure "default=false" on all other branches
if err := tx.
Model(&database.Branch{}).
Where(&database.Branch{ProjectID: projectID, Default: true},
database.BranchFields.ProjectID,
database.BranchFields.Default).
Where(tx.Not(&database.Branch{Name: defaultBranchName},
database.BranchFields.Name)).
Select(database.BranchFields.Default).
Updates(&database.Branch{Default: false}).Error; err != nil {
return err
}
// ensure "default=true" on default branch
return tx.
Model(&database.Branch{}).
Where(&database.Branch{ProjectID: projectID, Name: defaultBranchName},
database.BranchFields.ProjectID,
database.BranchFields.Name).
Select(database.BranchFields.Default).
Updates(&database.Branch{Default: true}).Error
})
}