Example #1
0
// UpsertOne upserts a host.
func UpsertOne(query interface{}, update interface{}) (*mgo.ChangeInfo, error) {
	return db.Upsert(
		Collection,
		query,
		update,
	)
}
Example #2
0
// insertTask creates a TaskJSON document with the data sent in the request body.
func insertTask(w http.ResponseWriter, r *http.Request) {
	t := plugin.GetTask(r)
	if t == nil {
		http.Error(w, "task not found", http.StatusNotFound)
		return
	}
	name := mux.Vars(r)["name"]
	rawData := map[string]interface{}{}
	err := util.ReadJSONInto(r.Body, &rawData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	jsonBlob := TaskJSON{
		TaskId:              t.Id,
		TaskName:            t.DisplayName,
		Name:                name,
		BuildId:             t.BuildId,
		Variant:             t.BuildVariant,
		ProjectId:           t.Project,
		VersionId:           t.Version,
		CreateTime:          t.CreateTime,
		Revision:            t.Revision,
		RevisionOrderNumber: t.RevisionOrderNumber,
		Data:                rawData,
		IsPatch:             t.Requester == evergreen.PatchVersionRequester,
	}
	_, err = db.Upsert(collection, bson.M{TaskIdKey: t.Id, NameKey: name}, jsonBlob)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	plugin.WriteJSON(w, http.StatusOK, "ok")
	return
}
Example #3
0
// Upsert updates the project ref in the db if an entry already exists,
// overwriting the existing ref. If no project ref exists, one is created
func (projectRef *ProjectRef) Upsert() error {
	_, err := db.Upsert(
		ProjectRefCollection,
		bson.M{
			ProjectRefIdentifierKey: projectRef.Identifier,
		},
		bson.M{
			"$set": bson.M{
				ProjectRefRepoKindKey:           projectRef.RepoKind,
				ProjectRefEnabledKey:            projectRef.Enabled,
				ProjectRefPrivateKey:            projectRef.Private,
				ProjectRefBatchTimeKey:          projectRef.BatchTime,
				ProjectRefOwnerKey:              projectRef.Owner,
				ProjectRefRepoKey:               projectRef.Repo,
				ProjectRefBranchKey:             projectRef.Branch,
				ProjectRefDisplayNameKey:        projectRef.DisplayName,
				ProjectRefDeactivatePreviousKey: projectRef.DeactivatePrevious,
				ProjectRefTrackedKey:            projectRef.Tracked,
				ProjectRefRemotePathKey:         projectRef.RemotePath,
				ProjectRefTrackedKey:            projectRef.Tracked,
				ProjectRefLocalConfig:           projectRef.LocalConfig,
				ProjectRefAlertsKey:             projectRef.Alerts,
				ProjectRefRepotrackerError:      projectRef.RepotrackerError,
				ProjectRefAdminsKey:             projectRef.Admins,
			},
		},
	)
	return err
}
// Upsert overwrites an existing note.
func (n *Note) Upsert() error {
	_, err := db.Upsert(
		NotesCollection,
		bson.D{{TaskIdKey, n.TaskId}},
		n,
	)
	return err
}
Example #5
0
func (projectVars *ProjectVars) Upsert() (*mgo.ChangeInfo, error) {
	return db.Upsert(
		ProjectVarsCollection,
		bson.M{
			ProjectVarIdKey: projectVars.Id,
		},
		bson.M{
			"$set": bson.M{
				ProjectVarsMapKey: projectVars.Vars,
			},
		},
	)
}
Example #6
0
func UpdateTaskQueue(distro string, taskQueue []TaskQueueItem) error {
	_, err := db.Upsert(
		TaskQueuesCollection,
		bson.M{
			TaskQueueDistroKey: distro,
		},
		bson.M{
			"$set": bson.M{
				TaskQueueQueueKey: taskQueue,
			},
		},
	)
	return err
}
Example #7
0
func UpsertOneProcessRuntime(query interface{}, update interface{}) error {
	info, err := db.Upsert(
		RuntimesCollection,
		query,
		update,
	)
	if err != nil {
		return err
	}
	if info.UpsertedId != nil {
		evergreen.Logger.Logf(slogger.INFO, "Added \"%s\" process to ProcessRuntime"+
			" db", info.UpsertedId)
	}
	return nil
}
Example #8
0
// Record the last-notification time for a given project.
func SetLastNotificationsEventTime(projectName string,
	timeOfEvent time.Time) error {
	_, err := db.Upsert(
		NotifyTimesCollection,
		bson.M{
			PntProjectNameKey: projectName,
		},
		bson.M{
			"$set": bson.M{
				PntLastEventTime: timeOfEvent,
			},
		},
	)
	return err
}
Example #9
0
// Upsert updates the files entry in the db if an entry already exists,
// overwriting the existing file data. If no entry exists, one is created
func (e Entry) Upsert() error {
	for _, file := range e.Files {
		_, err := db.Upsert(
			Collection,
			bson.M{
				TaskIdKey:   e.TaskId,
				TaskNameKey: e.TaskDisplayName,
				BuildIdKey:  e.BuildId,
			},
			bson.M{
				"$addToSet": bson.M{
					FilesKey: file,
				},
			},
		)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #10
0
File: json.go Project: sr527/json
// GetRoutes returns an API route for serving patch data.
func (jsp *JSONPlugin) GetAPIHandler() http.Handler {
	r := mux.NewRouter()
	r.HandleFunc("/tags/{task_name}/{name}", func(w http.ResponseWriter, r *http.Request) {
		t := plugin.GetTask(r)
		if t == nil {
			http.Error(w, "task not found", http.StatusNotFound)
			return
		}
		tagged := []TaskJSON{}
		jsonQuery := db.Query(bson.M{
			"project_id": t.Project,
			"variant":    t.BuildVariant,
			"task_name":  mux.Vars(r)["task_name"],
			"tag":        bson.M{"$exists": true, "$ne": ""},
			"name":       mux.Vars(r)["name"]})
		err := db.FindAllQ(collection, jsonQuery, &tagged)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		plugin.WriteJSON(w, http.StatusOK, tagged)
	})
	r.HandleFunc("/history/{task_name}/{name}", func(w http.ResponseWriter, r *http.Request) {
		t := plugin.GetTask(r)
		if t == nil {
			http.Error(w, "task not found", http.StatusNotFound)
			return
		}
		before := []TaskJSON{}
		jsonQuery := db.Query(bson.M{
			"project_id": t.Project,
			"variant":    t.BuildVariant,
			"order":      bson.M{"$lte": t.RevisionOrderNumber},
			"task_name":  mux.Vars(r)["task_name"],
			"is_patch":   false,
			"name":       mux.Vars(r)["name"]}).Sort([]string{"-order"}).Limit(100)
		err := db.FindAllQ(collection, jsonQuery, &before)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		//reverse order of "before" because we had to sort it backwards to apply the limit correctly:
		for i, j := 0, len(before)-1; i < j; i, j = i+1, j-1 {
			before[i], before[j] = before[j], before[i]
		}

		after := []TaskJSON{}
		jsonAfterQuery := db.Query(bson.M{
			"project_id": t.Project,
			"variant":    t.BuildVariant,
			"order":      bson.M{"$gt": t.RevisionOrderNumber},
			"task_name":  mux.Vars(r)["task_name"],
			"is_patch":   false,
			"name":       mux.Vars(r)["name"]}).Sort([]string{"order"}).Limit(100)
		err = db.FindAllQ(collection, jsonAfterQuery, &after)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		//concatenate before + after
		before = append(before, after...)
		plugin.WriteJSON(w, http.StatusOK, before)
	})

	r.HandleFunc("/data/{name}", func(w http.ResponseWriter, r *http.Request) {
		task := plugin.GetTask(r)
		if task == nil {
			http.Error(w, "task not found", http.StatusNotFound)
			return
		}
		name := mux.Vars(r)["name"]
		rawData := map[string]interface{}{}
		err := util.ReadJSONInto(r.Body, &rawData)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		jsonBlob := TaskJSON{
			TaskId:              task.Id,
			TaskName:            task.DisplayName,
			Name:                name,
			BuildId:             task.BuildId,
			Variant:             task.BuildVariant,
			ProjectId:           task.Project,
			VersionId:           task.Version,
			Revision:            task.Revision,
			RevisionOrderNumber: task.RevisionOrderNumber,
			Data:                rawData,
			IsPatch:             task.Requester == evergreen.PatchVersionRequester,
		}
		_, err = db.Upsert(collection, bson.M{"task_id": task.Id, "name": name}, jsonBlob)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		plugin.WriteJSON(w, http.StatusOK, "ok")
		return
	})

	r.HandleFunc("/data/{task_name}/{name}", func(w http.ResponseWriter, r *http.Request) {
		task := plugin.GetTask(r)
		if task == nil {
			http.Error(w, "task not found", http.StatusNotFound)
			return
		}
		name := mux.Vars(r)["name"]
		taskName := mux.Vars(r)["task_name"]
		var jsonForTask TaskJSON
		err := db.FindOneQ(collection, db.Query(bson.M{"version_id": task.Version, "build_id": task.BuildId, "name": name, "task_name": taskName}), &jsonForTask)
		if err != nil {
			if err == mgo.ErrNotFound {
				plugin.WriteJSON(w, http.StatusNotFound, nil)
				return
			}
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		plugin.WriteJSON(w, http.StatusOK, jsonForTask.Data)
	})
	r.HandleFunc("/data/{task_name}/{name}/{variant}", func(w http.ResponseWriter, r *http.Request) {
		task := plugin.GetTask(r)
		if task == nil {
			http.Error(w, "task not found", http.StatusNotFound)
			return
		}
		name := mux.Vars(r)["name"]
		taskName := mux.Vars(r)["task_name"]
		variantId := mux.Vars(r)["variant"]
		// Find the task for the other variant, if it exists
		ts, err := model.FindTasks(db.Query(bson.M{model.TaskVersionKey: task.Version, model.TaskBuildVariantKey: variantId, model.TaskDisplayNameKey: taskName}).Limit(1))
		if err != nil {
			if err == mgo.ErrNotFound {
				plugin.WriteJSON(w, http.StatusNotFound, nil)
				return
			}
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if len(ts) == 0 {
			plugin.WriteJSON(w, http.StatusNotFound, nil)
			return
		}
		otherVariantTask := ts[0]

		var jsonForTask TaskJSON
		err = db.FindOneQ(collection, db.Query(bson.M{"task_id": otherVariantTask.Id, "name": name}), &jsonForTask)
		if err != nil {
			if err == mgo.ErrNotFound {
				plugin.WriteJSON(w, http.StatusNotFound, nil)
				return
			}
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		plugin.WriteJSON(w, http.StatusOK, jsonForTask.Data)
	})
	return r
}