-
Notifications
You must be signed in to change notification settings - Fork 124
/
patch.go
213 lines (196 loc) · 6.77 KB
/
patch.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
package service
import (
"fmt"
"net/http"
"strconv"
"github.com/evergreen-ci/evergreen/units"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/commitqueue"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
func (uis *UIServer) patchPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Patch == nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
currentUser := MustHaveUser(r)
spruceLink := fmt.Sprintf("%s/patch/%s/configure", uis.Settings.Ui.UIv2Url, projCtx.Patch.Id.Hex())
if RedirectSpruceUsers(w, r, spruceLink) {
return
}
var versionAsUI *uiVersion
if projCtx.Version != nil { // Patch is already finalized
versionAsUI = &uiVersion{
Version: *projCtx.Version,
RepoOwner: projCtx.ProjectRef.Owner,
Repo: projCtx.ProjectRef.Repo,
}
}
// get the new patch document with the patched configuration
var err error
projCtx.Patch, err = patch.FindOne(patch.ById(projCtx.Patch.Id))
if err != nil {
http.Error(w, fmt.Sprintf("error loading patch: %v", err), http.StatusInternalServerError)
return
}
if projCtx.Patch == nil {
http.Error(w, fmt.Sprintf("could not find patch '%s' for projCtx", projCtx.Patch.Id),
http.StatusInternalServerError)
return
}
// Unmarshall project and get project variants and tasks
variantsAndTasksFromProject, err := model.GetVariantsAndTasksFromPatchProject(r.Context(), uis.env.Settings(), projCtx.Patch)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
commitQueuePosition := 0
if projCtx.Patch.IsCommitQueuePatch() {
cq, err := commitqueue.FindOneId(projCtx.ProjectRef.Id)
// still display patch page if problem finding commit queue
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "error finding commit queue"))
}
if cq != nil {
commitQueuePosition = cq.FindItem(projCtx.Patch.Id.Hex())
}
}
newUILink := ""
if len(uis.Settings.Ui.UIv2Url) > 0 {
newUILink = spruceLink
}
uis.render.WriteResponse(w, http.StatusOK, struct {
Version *uiVersion
Variants map[string]model.BuildVariant
Tasks []struct{ Name string }
CanEdit bool
CommitQueuePosition int
NewUILink string
ViewData
}{versionAsUI, variantsAndTasksFromProject.Variants, variantsAndTasksFromProject.Tasks, currentUser != nil,
commitQueuePosition, newUILink, uis.GetCommonViewData(w, r, true, true)},
"base", "patch_version.html", "base_angular.html", "menu.html")
}
func (uis *UIServer) schedulePatchUI(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Patch == nil {
uis.LoggedError(w, r, http.StatusNotFound, errors.New("patch not found"))
}
curUser := gimlet.GetUser(r.Context())
if curUser == nil {
uis.LoggedError(w, r, http.StatusUnauthorized, errors.New("Not authorized to schedule patch"))
}
patchUpdateReq := model.PatchUpdate{}
if err := utility.ReadJSON(utility.NewRequestReader(r), &patchUpdateReq); err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
}
patchUpdateReq.Caller = curUser.Username()
status, err := units.SchedulePatch(r.Context(), uis.env, projCtx.Patch.Id.Hex(), projCtx.Version, patchUpdateReq)
if err != nil {
uis.LoggedError(w, r, status, err)
return
}
PushFlash(uis.CookieStore, r, w, NewSuccessFlash("Patch successfully configured."))
gimlet.WriteJSON(w, struct {
VersionId string `json:"version"`
}{projCtx.Patch.Id.Hex()})
}
func (uis *UIServer) diffPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Patch == nil {
http.Error(w, "patch not found", http.StatusNotFound)
return
}
// We have to reload the patch outside of the project context,
// since the raw diff is excluded by default. This redundancy is
// worth the time savings this behavior offers other pages.
fullPatch, err := patch.FindOne(patch.ById(projCtx.Patch.Id))
if err != nil {
http.Error(w, fmt.Sprintf("error loading patch: %s", err.Error()),
http.StatusInternalServerError)
return
}
if fullPatch == nil {
http.Error(w, fmt.Sprintf("could not find patch '%s'", projCtx.Patch.Id),
http.StatusInternalServerError)
return
}
if err = fullPatch.FetchPatchFiles(false); err != nil {
http.Error(w, fmt.Sprintf("finding patch files: %s", err.Error()),
http.StatusInternalServerError)
return
}
uis.render.WriteResponse(w, http.StatusOK, fullPatch, "base", "diff.html")
}
func (uis *UIServer) fileDiffPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Patch == nil {
http.Error(w, "patch not found", http.StatusNotFound)
return
}
fullPatch, err := patch.FindOne(patch.ById(projCtx.Patch.Id))
if err != nil {
http.Error(w, fmt.Sprintf("error loading patch: %s", err.Error()),
http.StatusInternalServerError)
return
}
if fullPatch == nil {
http.Error(w, fmt.Sprintf("could not find patch '%s'", projCtx.Patch.Id),
http.StatusInternalServerError)
return
}
if err = fullPatch.FetchPatchFiles(false); err != nil {
http.Error(w, fmt.Sprintf("error finding patch: %s", err.Error()),
http.StatusInternalServerError)
}
uis.render.WriteResponse(w, http.StatusOK, struct {
Data patch.Patch
FileName string
PatchNumber string
CommitNumber string
}{*fullPatch, r.FormValue("file_name"), r.FormValue("patch_number"), r.FormValue("commit_number")},
"base", "file_diff.html")
}
func (uis *UIServer) rawDiffPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Patch == nil {
http.Error(w, "patch not found", http.StatusNotFound)
return
}
fullPatch, err := patch.FindOne(patch.ById(projCtx.Patch.Id))
if err != nil {
http.Error(w, fmt.Sprintf("error loading patch: %s", err.Error()),
http.StatusInternalServerError)
return
}
if fullPatch == nil {
http.Error(w, fmt.Sprintf("could not find patch '%s'", projCtx.Patch.Id),
http.StatusInternalServerError)
return
}
if err = fullPatch.FetchPatchFiles(true); err != nil {
http.Error(w, fmt.Sprintf("error fetching patch files: %s", err.Error()),
http.StatusInternalServerError)
return
}
patchNum, err := strconv.Atoi(r.FormValue("patch_number"))
if err != nil {
http.Error(w, fmt.Sprintf("error getting patch number: %s", err.Error()),
http.StatusInternalServerError)
return
}
if patchNum < 0 || patchNum >= len(fullPatch.Patches) {
http.Error(w, "patch number out of range", http.StatusInternalServerError)
return
}
diff := fullPatch.Patches[patchNum].PatchSet.Patch
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(diff))
grip.Warning(err)
}