-
Notifications
You must be signed in to change notification settings - Fork 124
/
patch_resolver.go
219 lines (192 loc) · 8.09 KB
/
patch_resolver.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
package graphql
import (
"context"
"fmt"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/rest/data"
restModel "github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/utility"
)
// AuthorDisplayName is the resolver for the authorDisplayName field.
func (r *patchResolver) AuthorDisplayName(ctx context.Context, obj *restModel.APIPatch) (string, error) {
usr, err := user.FindOneById(*obj.Author)
if err != nil {
return "", ResourceNotFound.Send(ctx, fmt.Sprintf("Error getting user from user ID: %s", err.Error()))
}
if usr == nil {
return "", ResourceNotFound.Send(ctx, "Could not find user from user ID")
}
return usr.DisplayName(), nil
}
// BaseTaskStatuses is the resolver for the baseTaskStatuses field.
func (r *patchResolver) BaseTaskStatuses(ctx context.Context, obj *restModel.APIPatch) ([]string, error) {
baseVersion, err := model.FindBaseVersionForVersion(*obj.Id)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error finding base version for version '%s': %s", *obj.Id, err.Error()))
}
if baseVersion == nil {
return nil, nil
}
statuses, err := task.GetBaseStatusesForActivatedTasks(ctx, *obj.Id, baseVersion.Id)
if err != nil {
return nil, nil
}
return statuses, nil
}
// Builds is the resolver for the builds field.
func (r *patchResolver) Builds(ctx context.Context, obj *restModel.APIPatch) ([]*restModel.APIBuild, error) {
builds, err := build.FindBuildsByVersions([]string{*obj.Version})
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error finding build by version %s: %s", *obj.Version, err.Error()))
}
var apiBuilds []*restModel.APIBuild
for _, build := range builds {
apiBuild := restModel.APIBuild{}
apiBuild.BuildFromService(build, nil)
apiBuilds = append(apiBuilds, &apiBuild)
}
return apiBuilds, nil
}
// CommitQueuePosition is the resolver for the commitQueuePosition field.
func (r *patchResolver) CommitQueuePosition(ctx context.Context, obj *restModel.APIPatch) (*int, error) {
if err := obj.GetCommitQueuePosition(); err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
return obj.CommitQueuePosition, nil
}
// Duration is the resolver for the duration field.
func (r *patchResolver) Duration(ctx context.Context, obj *restModel.APIPatch) (*PatchDuration, error) {
query := db.Query(task.ByVersion(*obj.Id)).WithFields(task.TimeTakenKey, task.StartTimeKey, task.FinishTimeKey, task.DisplayOnlyKey, task.ExecutionKey)
tasks, err := task.FindAllFirstExecution(query)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
if tasks == nil {
return nil, ResourceNotFound.Send(ctx, "Could not find any tasks for patch")
}
timeTaken, makespan := task.GetFormattedTimeSpent(tasks)
return makePatchDuration(timeTaken, makespan), nil
}
// PatchTriggerAliases is the resolver for the patchTriggerAliases field.
func (r *patchResolver) PatchTriggerAliases(ctx context.Context, obj *restModel.APIPatch) ([]*restModel.APIPatchTriggerDefinition, error) {
projectRef, err := data.FindProjectById(*obj.ProjectId, true, true)
if err != nil || projectRef == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Could not find project: %s : %s", *obj.ProjectId, err))
}
if len(projectRef.PatchTriggerAliases) == 0 {
return nil, nil
}
projectCache := map[string]*model.Project{}
aliases := []*restModel.APIPatchTriggerDefinition{}
for _, alias := range projectRef.PatchTriggerAliases {
project, projectCached := projectCache[alias.ChildProject]
if !projectCached {
_, project, _, err = model.FindLatestVersionWithValidProject(alias.ChildProject, false)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("getting last known project for '%s': %s", alias.ChildProject, err.Error()))
}
projectCache[alias.ChildProject] = project
}
matchingTasks, err := project.VariantTasksForSelectors([]patch.PatchTriggerDefinition{alias}, *obj.Requester)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Problem matching tasks to alias definitions: %v", err.Error()))
}
variantsTasks := []restModel.VariantTask{}
for _, vt := range matchingTasks {
variantsTasks = append(variantsTasks, restModel.VariantTask{
Name: utility.ToStringPtr(vt.Variant),
Tasks: utility.ToStringPtrSlice(vt.Tasks),
})
}
identifier, err := model.GetIdentifierForProject(alias.ChildProject)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Problem getting child project identifier: %v", err.Error()))
}
aliases = append(aliases, &restModel.APIPatchTriggerDefinition{
Alias: utility.ToStringPtr(alias.Alias),
ChildProjectId: utility.ToStringPtr(alias.ChildProject),
ChildProjectIdentifier: utility.ToStringPtr(identifier),
VariantsTasks: variantsTasks,
})
}
return aliases, nil
}
// Project is the resolver for the project field.
func (r *patchResolver) Project(ctx context.Context, obj *restModel.APIPatch) (*PatchProject, error) {
patchProject, err := getPatchProjectVariantsAndTasksForUI(ctx, obj)
if err != nil {
return nil, err
}
return patchProject, nil
}
// ProjectIdentifier is the resolver for the projectIdentifier field.
func (r *patchResolver) ProjectIdentifier(ctx context.Context, obj *restModel.APIPatch) (string, error) {
obj.GetIdentifier()
return utility.FromStringPtr(obj.ProjectIdentifier), nil
}
// ProjectMetadata is the resolver for the projectMetadata field.
func (r *patchResolver) ProjectMetadata(ctx context.Context, obj *restModel.APIPatch) (*restModel.APIProjectRef, error) {
apiProjectRef, err := getProjectMetadata(ctx, obj.ProjectId, obj.Id)
return apiProjectRef, err
}
// TaskCount is the resolver for the taskCount field.
func (r *patchResolver) TaskCount(ctx context.Context, obj *restModel.APIPatch) (*int, error) {
taskCount, err := task.Count(db.Query(task.DisplayTasksByVersion(*obj.Id, false)))
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error getting task count for patch %s: %s", *obj.Id, err.Error()))
}
return &taskCount, nil
}
// TaskStatuses is the resolver for the taskStatuses field.
func (r *patchResolver) TaskStatuses(ctx context.Context, obj *restModel.APIPatch) ([]string, error) {
statuses, err := task.GetTaskStatusesByVersion(ctx, utility.FromStringPtr(obj.Id))
if err != nil {
return nil, nil
}
return statuses, nil
}
// Time is the resolver for the time field.
func (r *patchResolver) Time(ctx context.Context, obj *restModel.APIPatch) (*PatchTime, error) {
usr := mustHaveUser(ctx)
started, err := getFormattedDate(obj.StartTime, usr.Settings.Timezone)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
finished, err := getFormattedDate(obj.FinishTime, usr.Settings.Timezone)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
submittedAt, err := getFormattedDate(obj.CreateTime, usr.Settings.Timezone)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
return &PatchTime{
Started: started,
Finished: finished,
SubmittedAt: *submittedAt,
}, nil
}
// VersionFull is the resolver for the versionFull field.
func (r *patchResolver) VersionFull(ctx context.Context, obj *restModel.APIPatch) (*restModel.APIVersion, error) {
if utility.FromStringPtr(obj.Version) == "" {
return nil, nil
}
v, err := model.VersionFindOneId(*obj.Version)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error while finding version with id: `%s`: %s", *obj.Version, err.Error()))
}
if v == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Unable to find version with id: `%s`", *obj.Version))
}
apiVersion := restModel.APIVersion{}
apiVersion.BuildFromService(*v)
return &apiVersion, nil
}
// Patch returns PatchResolver implementation.
func (r *Resolver) Patch() PatchResolver { return &patchResolver{r} }
type patchResolver struct{ *Resolver }