-
Notifications
You must be signed in to change notification settings - Fork 124
/
task.go
118 lines (106 loc) · 3.44 KB
/
task.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
package data
import (
"fmt"
"net/http"
"github.com/evergreen-ci/evergreen/model"
serviceModel "github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/gimlet"
"github.com/pkg/errors"
)
// FindTasksByBuildId uses the service layer's task type to query the backing database for a
// list of task that matches buildId. It accepts the startTaskId and a limit
// to allow for pagination of the queries. It returns results sorted by taskId.
func FindTasksByBuildId(buildId, taskId, status string, limit int, sortDir int) ([]task.Task, error) {
pipeline := task.TasksByBuildIdPipeline(buildId, taskId, status, limit, sortDir)
res := []task.Task{}
err := task.Aggregate(pipeline, &res)
if err != nil {
return []task.Task{}, err
}
if taskId != "" {
found := false
for _, t := range res {
if t.Id == taskId {
found = true
break
}
}
if !found {
return []task.Task{}, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("task '%s' not found", taskId),
}
}
}
return res, nil
}
// FindTasksByProjectAndCommit is a method to find a set of tasks which ran as part of
// certain version in a project. It takes the projectId, commit hash, and a taskId
// for paginating through the results.
func FindTasksByProjectAndCommit(opts task.GetTasksByProjectAndCommitOptions) ([]task.Task, error) {
projectId, err := model.GetIdForProject(opts.Project)
if err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: errors.Wrapf(err, "project '%s' not found", projectId).Error(),
}
}
opts.Project = projectId
pipeline := task.TasksByProjectAndCommitPipeline(opts)
res := []task.Task{}
err = task.Aggregate(pipeline, &res)
if err != nil {
return []task.Task{}, err
}
if len(res) == 0 {
var message string
if opts.Status != "" {
message = fmt.Sprintf("task from project '%s' and commit '%s' with status '%s' "+
"not found", projectId, opts.CommitHash, opts.Status)
} else {
message = fmt.Sprintf("task from project '%s' and commit '%s' not found",
projectId, opts.CommitHash)
}
return []task.Task{}, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: message,
}
}
return res, nil
}
func CheckTaskSecret(taskID string, r *http.Request) (int, error) {
_, code, err := serviceModel.ValidateTask(taskID, true, r)
return code, errors.Wrapf(err, "invalid task '%s'", taskID)
}
func FindTask(taskID string) (*task.Task, error) {
foundTask, err := task.FindOneId(taskID)
if err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrap(err, "finding task").Error(),
}
}
if foundTask == nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("task '%s' not found", taskID),
}
}
return foundTask, nil
}
// FindGeneratedTasksFromID finds info about all tasks that were generated by
// the given generator task.
func FindGeneratedTasksFromID(generatorID string) ([]task.GeneratedTaskInfo, error) {
taskInfos, err := task.FindGeneratedTasksFromID(generatorID)
if err != nil {
return nil, errors.Wrapf(err, "finding tasks generated by task '%s'", generatorID)
}
if len(taskInfos) == 0 {
return nil, gimlet.ErrorResponse{
Message: fmt.Sprintf("no tasks generated by task '%s' could be found", generatorID),
StatusCode: http.StatusNotFound,
}
}
return taskInfos, nil
}