github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/taskdata/task.go (about) 1 package taskdata 2 3 import ( 4 "net/http" 5 6 "github.com/evergreen-ci/evergreen" 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/model/task" 9 "github.com/evergreen-ci/evergreen/plugin" 10 "github.com/evergreen-ci/evergreen/util" 11 "github.com/gorilla/mux" 12 "github.com/pkg/errors" 13 "gopkg.in/mgo.v2" 14 "gopkg.in/mgo.v2/bson" 15 ) 16 17 // uiGetTaskById sends back a JSONTask with the corresponding task id. 18 func uiGetTaskById(w http.ResponseWriter, r *http.Request) { 19 taskId := mux.Vars(r)["task_id"] 20 name := mux.Vars(r)["name"] 21 jsonForTask, err := GetTaskById(taskId, name) 22 if err != nil { 23 if err != mgo.ErrNotFound { 24 http.Error(w, err.Error(), http.StatusInternalServerError) 25 return 26 } 27 http.Error(w, "{}", http.StatusNotFound) 28 return 29 } 30 plugin.WriteJSON(w, http.StatusOK, jsonForTask) 31 } 32 33 // GetTaskById fetches a JSONTask with the corresponding task id. 34 func GetTaskById(taskId, name string) (TaskJSON, error) { 35 var jsonForTask TaskJSON 36 err := db.FindOneQ(collection, db.Query(bson.M{TaskIdKey: taskId, NameKey: name}), &jsonForTask) 37 if err != nil { 38 return TaskJSON{}, err 39 } 40 return jsonForTask, nil 41 } 42 43 func apiGetTaskByName(w http.ResponseWriter, r *http.Request) { 44 t := plugin.GetTask(r) 45 if t == nil { 46 http.Error(w, "task not found", http.StatusNotFound) 47 return 48 } 49 name := mux.Vars(r)["name"] 50 taskName := mux.Vars(r)["task_name"] 51 52 jsonForTask, err := GetTaskByName(t.Version, t.BuildId, taskName, name) 53 if err != nil { 54 if err == mgo.ErrNotFound { 55 plugin.WriteJSON(w, http.StatusNotFound, nil) 56 return 57 } 58 http.Error(w, err.Error(), http.StatusInternalServerError) 59 return 60 } 61 if len(r.FormValue("full")) != 0 { // if specified, include the json data's container as well 62 plugin.WriteJSON(w, http.StatusOK, jsonForTask) 63 return 64 } 65 plugin.WriteJSON(w, http.StatusOK, jsonForTask.Data) 66 } 67 68 // GetTaskByName finds a taskdata document by using the name of the task 69 // and other identifying information. 70 func GetTaskByName(version, buildId, taskName, name string) (TaskJSON, error) { 71 var jsonForTask TaskJSON 72 err := db.FindOneQ(collection, db.Query(bson.M{VersionIdKey: version, BuildIdKey: buildId, NameKey: name, 73 TaskNameKey: taskName}), &jsonForTask) 74 if err != nil { 75 return TaskJSON{}, err 76 } 77 return jsonForTask, nil 78 } 79 80 // apiGetTaskForVariant finds a task by name and variant and finds 81 // the document in the json collection associated with that task's id. 82 func apiGetTaskForVariant(w http.ResponseWriter, r *http.Request) { 83 t := plugin.GetTask(r) 84 if t == nil { 85 http.Error(w, "task not found", http.StatusNotFound) 86 return 87 } 88 name := mux.Vars(r)["name"] 89 taskName := mux.Vars(r)["task_name"] 90 variantId := mux.Vars(r)["variant"] 91 // Find the task for the other variant, if it exists 92 93 jsonForTask, err := GetTaskForVariant(t.Version, variantId, taskName, name) 94 95 if err != nil { 96 if err == mgo.ErrNotFound { 97 plugin.WriteJSON(w, http.StatusNotFound, nil) 98 return 99 } 100 http.Error(w, err.Error(), http.StatusInternalServerError) 101 return 102 } 103 if len(r.FormValue("full")) != 0 { // if specified, include the json data's container as well 104 plugin.WriteJSON(w, http.StatusOK, jsonForTask) 105 return 106 } 107 plugin.WriteJSON(w, http.StatusOK, jsonForTask.Data) 108 } 109 110 // GetTaskForVariant finds a task by name and variant and finds 111 // the document in the json collection associated with that task's id. 112 func GetTaskForVariant(version, variantId, taskName, name string) (TaskJSON, error) { 113 foundTask, err := task.FindOne(db.Query(bson.M{task.VersionKey: version, task.BuildVariantKey: variantId, 114 task.DisplayNameKey: taskName})) 115 if err != nil { 116 return TaskJSON{}, err 117 } 118 119 var jsonForTask TaskJSON 120 err = db.FindOneQ(collection, db.Query(bson.M{TaskIdKey: foundTask.Id, NameKey: name}), &jsonForTask) 121 if err != nil { 122 return TaskJSON{}, err 123 } 124 return jsonForTask, nil 125 } 126 127 func apiInsertTask(w http.ResponseWriter, r *http.Request) { 128 t := plugin.GetTask(r) 129 if t == nil { 130 http.Error(w, "task not found", http.StatusNotFound) 131 return 132 } 133 name := mux.Vars(r)["name"] 134 rawData := map[string]interface{}{} 135 err := util.ReadJSONInto(util.NewRequestReader(r), &rawData) 136 if err != nil { 137 http.Error(w, err.Error(), http.StatusInternalServerError) 138 return 139 } 140 err = InsertTask(t, name, rawData) 141 if err != nil { 142 http.Error(w, err.Error(), http.StatusInternalServerError) 143 return 144 } 145 plugin.WriteJSON(w, http.StatusOK, "ok") 146 return 147 } 148 149 // InsertTask creates a TaskJSON document in the plugin's collection. 150 func InsertTask(t *task.Task, name string, data map[string]interface{}) error { 151 jsonBlob := TaskJSON{ 152 TaskId: t.Id, 153 TaskName: t.DisplayName, 154 Name: name, 155 BuildId: t.BuildId, 156 Variant: t.BuildVariant, 157 ProjectId: t.Project, 158 VersionId: t.Version, 159 CreateTime: t.CreateTime, 160 Revision: t.Revision, 161 RevisionOrderNumber: t.RevisionOrderNumber, 162 Data: data, 163 IsPatch: t.Requester == evergreen.PatchVersionRequester, 164 } 165 _, err := db.Upsert(collection, bson.M{TaskIdKey: t.Id, NameKey: name}, jsonBlob) 166 167 return errors.WithStack(err) 168 }