github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/service/rest_patch.go (about)

     1  package service
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/evergreen-ci/evergreen/model/patch"
     8  	"github.com/mongodb/grip"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  type RestPatch struct {
    13  	Id          string              `json:"_id"`
    14  	Description string              `json:"desc"`
    15  	Project     string              `json:"project"`
    16  	Revision    string              `json:"revision"`
    17  	PatchNumber int                 `json:"patch_number"`
    18  	Author      string              `json:"author"`
    19  	Version     string              `json:"version"`
    20  	CreateTime  time.Time           `json:"create_time"`
    21  	Patches     []patch.ModulePatch `json:"patches"`
    22  }
    23  
    24  // Returns a JSON response with the marshaled output of the task
    25  // specified in the request.
    26  func (restapi restAPI) getPatch(w http.ResponseWriter, r *http.Request) {
    27  	projCtx := MustHaveRESTContext(r)
    28  	if projCtx.Patch == nil {
    29  		restapi.WriteJSON(w, http.StatusNotFound, responseError{Message: "patch not found"})
    30  		return
    31  	}
    32  
    33  	err := projCtx.Patch.FetchPatchFiles()
    34  	if err != nil {
    35  		restapi.LoggedError(w, r, http.StatusInternalServerError,
    36  			errors.Wrap(err, "error occurred fetching patch data"))
    37  		return
    38  	}
    39  
    40  	destPatch := &RestPatch{
    41  		Id:          projCtx.Patch.Id.Hex(),
    42  		Description: projCtx.Patch.Description,
    43  		Project:     projCtx.Patch.Project,
    44  		Revision:    projCtx.Patch.Githash,
    45  		PatchNumber: projCtx.Patch.PatchNumber,
    46  		Author:      projCtx.Patch.Author,
    47  		Version:     projCtx.Patch.Version,
    48  		CreateTime:  projCtx.Patch.CreateTime,
    49  		Patches:     projCtx.Patch.Patches,
    50  	}
    51  
    52  	restapi.WriteJSON(w, http.StatusOK, destPatch)
    53  	return
    54  }
    55  
    56  // getPatchConfig returns the patched config for a given patch.
    57  func (restapi restAPI) getPatchConfig(w http.ResponseWriter, r *http.Request) {
    58  	projCtx := MustHaveRESTContext(r)
    59  	if projCtx.Patch == nil {
    60  		restapi.WriteJSON(w, http.StatusNotFound, responseError{Message: "patch not found"})
    61  		return
    62  	}
    63  	w.Header().Set("Content-Type", "application/x-yaml; charset=utf-8")
    64  	w.WriteHeader(http.StatusOK)
    65  	_, err := w.Write([]byte(projCtx.Patch.PatchedConfig))
    66  	grip.Warning(err)
    67  }