-
Notifications
You must be signed in to change notification settings - Fork 28
/
sync.go
174 lines (140 loc) · 4.62 KB
/
sync.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
// SPDX-License-Identifier: Apache-2.0
package scm
import (
"fmt"
"net/http"
"strings"
"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/scm/repos/{org}/{repo}/sync scm SyncRepo
//
// Sync up scm service and database in the context of a specific 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 synchronized repo
// schema:
// "$ref": "#/definitions/Repo"
// '204':
// description: Successful request resulting in no change
// '301':
// description: Repo has moved permanently
// schema:
// "$ref": "#/definitions/Error"
// '403':
// description: User has been forbidden access to repository
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to synchronize repo
// schema:
// "$ref": "#/definitions/Error"
// SyncRepo represents the API handler to
// synchronize a single repository between
// SCM service and the database should a discrepancy
// exist. Primarily used for deleted repos or to align
// subscribed events with allowed events.
func SyncRepo(c *gin.Context) {
// capture middleware values
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logger := logrus.WithFields(logrus.Fields{
"org": o,
"repo": r.GetName(),
"user": u.GetName(),
})
logger.Infof("syncing repo %s", r.GetFullName())
// retrieve repo from source code manager service
_, respCode, err := scm.FromContext(c).GetRepo(ctx, u, r)
// if there is an error retrieving repo, we know it is deleted: set to inactive
if err != nil {
if respCode == http.StatusNotFound {
// set repo to inactive - do not delete
r.SetActive(false)
// update repo in database
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// exit with success as hook sync will be unnecessary
c.JSON(http.StatusOK, r)
return
}
retErr := fmt.Errorf("error while retrieving repo %s from %s: %w", r.GetFullName(), scm.FromContext(c).Driver(), err)
util.HandleError(c, respCode, retErr)
return
}
// verify the user is an admin of the repo
// we cannot use our normal permissions check due to the possibility the repo was deleted
perm, err := scm.FromContext(c).RepoAccess(ctx, u.GetName(), u.GetToken(), o, r.GetName())
if err != nil {
logger.Errorf("unable to get user %s access level for org %s", u.GetName(), o)
}
if !strings.EqualFold(perm, "admin") {
retErr := fmt.Errorf("user %s does not have 'admin' permissions for the repo %s", u.GetName(), r.GetFullName())
util.HandleError(c, http.StatusUnauthorized, retErr)
return
}
// if we have webhook validation and the repo is active in the database,
// update the repo hook in the SCM
if c.Value("webhookvalidation").(bool) && r.GetActive() {
// grab last hook from repo to fetch the webhook ID
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to retrieve last hook for repo %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// update webhook
webhookExists, err := scm.FromContext(c).Update(ctx, u, r, lastHook.GetWebhookID())
if err != nil {
// if webhook has been manually deleted from GitHub,
// set to inactive in database
if !webhookExists {
r.SetActive(false)
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, r)
return
}
retErr := fmt.Errorf("unable to update repo webhook for %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
}
c.Status(http.StatusNoContent)
}