github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/plugin/builtin/taskdata/history.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/gorilla/mux"
    11  	"github.com/pkg/errors"
    12  	"gopkg.in/mgo.v2/bson"
    13  )
    14  
    15  func GetTaskHistory(t *task.Task, name string) ([]TaskJSON, error) {
    16  	var t2 *task.Task = t
    17  	var err error
    18  	if t.Requester == evergreen.PatchVersionRequester {
    19  		t2, err = t.FindTaskOnBaseCommit()
    20  		if err != nil {
    21  			return nil, err
    22  		}
    23  		if t2 == nil {
    24  			return nil, errors.New("could not find task on base commit")
    25  		}
    26  		t.RevisionOrderNumber = t2.RevisionOrderNumber
    27  	}
    28  
    29  	before := []TaskJSON{}
    30  	jsonQuery := db.Query(bson.M{
    31  		ProjectIdKey:           t.Project,
    32  		VariantKey:             t.BuildVariant,
    33  		RevisionOrderNumberKey: bson.M{"$lte": t.RevisionOrderNumber},
    34  		TaskNameKey:            t.DisplayName,
    35  		IsPatchKey:             false,
    36  		NameKey:                name,
    37  	})
    38  	jsonQuery = jsonQuery.Sort([]string{"-order"}).Limit(100)
    39  	err = db.FindAllQ(collection, jsonQuery, &before)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	//reverse order of "before" because we had to sort it backwards to apply the limit correctly:
    44  	for i, j := 0, len(before)-1; i < j; i, j = i+1, j-1 {
    45  		before[i], before[j] = before[j], before[i]
    46  	}
    47  
    48  	after := []TaskJSON{}
    49  	jsonAfterQuery := db.Query(bson.M{
    50  		ProjectIdKey:           t.Project,
    51  		VariantKey:             t.BuildVariant,
    52  		RevisionOrderNumberKey: bson.M{"$gt": t.RevisionOrderNumber},
    53  		TaskNameKey:            t.DisplayName,
    54  		IsPatchKey:             false,
    55  		NameKey:                name}).Sort([]string{"order"}).Limit(100)
    56  	err = db.FindAllQ(collection, jsonAfterQuery, &after)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	//concatenate before + after
    62  	before = append(before, after...)
    63  
    64  	// if our task was a patch, replace the base commit's info in the history with the patch
    65  	if t.Requester == evergreen.PatchVersionRequester {
    66  		before, err = fixPatchInHistory(t.Id, t2, before)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  	return before, nil
    72  }
    73  
    74  // getTaskHistory finds previous tasks by task name.
    75  func apiGetTaskHistory(w http.ResponseWriter, r *http.Request) {
    76  	t := plugin.GetTask(r)
    77  	if t == nil {
    78  		http.Error(w, "task not found", http.StatusNotFound)
    79  		return
    80  	}
    81  	history, err := GetTaskHistory(t, mux.Vars(r)["name"])
    82  	if err != nil {
    83  		http.Error(w, err.Error(), http.StatusInternalServerError)
    84  		return
    85  	}
    86  	plugin.WriteJSON(w, http.StatusOK, history)
    87  }
    88  
    89  func uiGetTaskHistory(w http.ResponseWriter, r *http.Request) {
    90  	t, err := task.FindOne(task.ById(mux.Vars(r)["task_id"]))
    91  	if err != nil {
    92  		http.Error(w, err.Error(), http.StatusInternalServerError)
    93  		return
    94  	}
    95  	if t == nil {
    96  		http.Error(w, "{}", http.StatusNotFound)
    97  		return
    98  	}
    99  
   100  	history, err := GetTaskHistory(t, mux.Vars(r)["name"])
   101  	if err != nil {
   102  		http.Error(w, err.Error(), http.StatusInternalServerError)
   103  		return
   104  	}
   105  	plugin.WriteJSON(w, http.StatusOK, history)
   106  }