Пример #1
0
func DeployCreateHandler(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	namespace := strings.Join([]string{params["owner"], params["repo"]}, "/")
	logHandler(r, "DeploycreateHandler", params, nil)

	// Get POST data
	decoder := json.NewDecoder(r.Body)
	d := data.Deployment{}
	err := decoder.Decode(&d)

	// Error on the json we received
	if err != nil {
		log.Errorf("Error decoding json: %v", err)
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(`{"error": "Invalid json"}`))
		return
	}

	// Check required params are present
	if len(d.Ref) <= 0 {
		log.Warning("Can't create deployment, ref missing")
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(`{"error": "Ref field is missing"}`))
		return
	}

	// Set basic data
	d.CreatedAt = time.Now().UTC()
	d.UpdatedAt = d.CreatedAt
	d.Namespace = namespace
	d.Save()

	// Set Urls for the response
	d.Url = utils.ApiUrl(fmt.Sprintf("repos/%s/deployments/%d", namespace, d.Id))
	d.StatusesUrl = fmt.Sprintf("%s/statuses", d.Url)
	//d.RepositoryUrl

	// Write final response
	b, err := json.Marshal(d)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Error("Error Marshalling json")
		return
	}

	//w.Header().Set("Location", d.Url)
	writeJson(w, b, map[string]string{"Location": d.Url}, http.StatusCreated)
}
Пример #2
0
func ListDeployments(namespace string) ([]Deployment, error) {
	dJson, err := ListDeploymentsAsJson(namespace)
	if err != nil {
		return nil, err
	}
	ds := []Deployment{}
	for _, i := range dJson {
		d := &Deployment{}
		err := json.Unmarshal(i, d)
		if err != nil {
			return nil, err
		}

		// Create the dynamic urls
		d.Url = utils.ApiUrl(fmt.Sprintf("repos/%s/deployments/%d", namespace, d.Id))
		d.StatusesUrl = fmt.Sprintf("%s/statuses", d.Url)

		ds = append(ds, *d)
	}
	return ds, err
}