/
version.go
127 lines (112 loc) · 3.48 KB
/
version.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
package evgjson
import (
"net/http"
"time"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model/version"
"github.com/evergreen-ci/evergreen/plugin"
"github.com/evergreen-ci/evergreen/util"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// CommitInfo represents the information about the commit
// associated with the version of a given project. This is displayed
// at the top of each header for each project.
type CommitInfo struct {
Author string `json:"author"`
Message string `json:"message"`
CreateTime time.Time `json:"create_time"`
Revision string `json:"revision"`
VersionId string `json:"version_id"`
}
type VersionData struct {
JSONTasks []TaskJSON `json:"json_tasks"`
Commit CommitInfo `json:"commit_info"`
}
// getVersion returns a StatusOK if the route is hit
func getVersion(w http.ResponseWriter, r *http.Request) {
plugin.WriteJSON(w, http.StatusOK, "1")
}
// findTasksForVersion sends back the list of TaskJSON documents associated with a version id.
func findTasksForVersion(versionId, name string) ([]TaskJSON, error) {
var jsonForTasks []TaskJSON
err := db.FindAllQ(collection, db.Query(bson.M{VersionIdKey: versionId,
NameKey: name}), &jsonForTasks)
if err != nil {
if err != mgo.ErrNotFound {
return nil, err
}
return nil, err
}
return jsonForTasks, err
}
// getTasksForVersion sends back the list of TaskJSON documents associated with a version id.
func getTasksForVersion(w http.ResponseWriter, r *http.Request) {
jsonForTasks, err := findTasksForVersion(mux.Vars(r)["version_id"], mux.Vars(r)["name"])
if jsonForTasks == nil {
http.Error(w, "{}", http.StatusNotFound)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
plugin.WriteJSON(w, http.StatusOK, jsonForTasks)
return
}
// getTasksForLatestVersion sends back the TaskJSON data associated with the latest version.
func getTasksForLatestVersion(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
var jsonTask TaskJSON
projects := []string{}
err := util.ReadJSONInto(r.Body, &projects)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
versionData := []VersionData{}
for _, project := range projects {
err := db.FindOneQ(collection, db.Query(bson.M{NameKey: name,
ProjectIdKey: project}).Sort([]string{"-" + RevisionOrderNumberKey}).WithFields(VersionIdKey), &jsonTask)
if err != nil {
if err != mgo.ErrNotFound {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(w, "{}", http.StatusNotFound)
return
}
if jsonTask.VersionId == "" {
http.Error(w, "{}", http.StatusNotFound)
}
jsonTasks, err := findTasksForVersion(jsonTask.VersionId, name)
if jsonTasks == nil {
http.Error(w, "{}", http.StatusNotFound)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get the version commit info
v, err := version.FindOne(version.ById(jsonTask.VersionId))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if v == nil {
http.Error(w, "{}", http.StatusNotFound)
return
}
commitInfo := CommitInfo{
Author: v.Author,
Message: v.Message,
CreateTime: v.CreateTime,
Revision: v.Revision,
VersionId: jsonTask.VersionId,
}
versionData = append(versionData, VersionData{jsonTasks, commitInfo})
}
plugin.WriteJSON(w, http.StatusOK, versionData)
}