-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathrepair.go
More file actions
127 lines (105 loc) · 3.16 KB
/
repair.go
File metadata and controls
127 lines (105 loc) · 3.16 KB
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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package repo
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/sirupsen/logrus"
)
// swagger:operation PATCH /api/v1/repos/{org}/{repo}/repair repos RepairRepo
//
// Remove and recreate the webhook for a repo
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the org
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repo
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully repaired the repo
// schema:
// type: string
// '500':
// description: Unable to repair the repo
// schema:
// "$ref": "#/definitions/Error"
// RepairRepo represents the API handler to remove
// and then create a webhook for a repo.
func RepairRepo(c *gin.Context) {
// capture middleware values
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"org": o,
"repo": r.GetName(),
"user": u.GetName(),
}).Infof("repairing repo %s", r.GetFullName())
// check if we should create the webhook
if c.Value("webhookvalidation").(bool) {
// send API call to remove the webhook
err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// send API call to create the webhook
hook, _, err := scm.FromContext(c).Enable(u, r)
if err != nil {
retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err)
switch err.Error() {
case "repo already enabled":
util.HandleError(c, http.StatusConflict, retErr)
return
case "repo not found":
util.HandleError(c, http.StatusNotFound, retErr)
return
}
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
hook.SetRepoID(r.GetID())
err = database.FromContext(c).CreateHook(hook)
if err != nil {
retErr := fmt.Errorf("unable to create initialization webhook for %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
}
// if the repo was previously inactive, mark it as active
if !r.GetActive() {
r.SetActive(true)
// send API call to update the repo
err := database.FromContext(c).UpdateRepo(r)
if err != nil {
retErr := fmt.Errorf("unable to set repo %s to active: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
}
c.JSON(http.StatusOK, fmt.Sprintf("repo %s repaired", r.GetFullName()))
}