Exemple #1
0
func GetTagComment(repo, item, tag string) string {
	path := "/api/repositories/" + repo + "/" + item + "/" + tag

	resp, err := commToServerGetRsp("get", path, nil)
	if err != nil {
		log.Error(err)
		return ""
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest {
		err = errors.New("unkown error")
		log.Error("GET", path, resp.StatusCode)
		return ""
	}
	result := ds.Response{}
	struComment := &struct {
		Comment string `json:"comment"`
	}{}
	result.Data = struComment

	respbody, _ := ioutil.ReadAll(resp.Body)
	log.Println(string(respbody))
	unmarshalerr := json.Unmarshal(respbody, &result)
	if unmarshalerr != nil {
		log.Error(unmarshalerr)
		return ""
	}
	log.Println(result)

	return struComment.Comment
}
Exemple #2
0
func buildResp(code int, msg string, data interface{}) (body []byte, err error) {
	r := ds.Response{}

	r.Code = code
	r.Msg = msg
	r.Data = data

	return json.Marshal(r)

}
Exemple #3
0
func repoDelOneItemHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

	if len(loginAuthStr) == 0 {
		HttpNoData(w, http.StatusUnauthorized, cmd.ErrorServiceUnavailable, " ")
		return
	}
	repository := ps.ByName("repo")
	dataitem := ps.ByName("item")

	r.ParseForm()
	ensure, _ := strconv.Atoi(r.Form.Get("ensure"))

	reqBody, _ := ioutil.ReadAll(r.Body)

	exist, msg, err := judgeRepoOrItemExist(repository, dataitem)
	if err != nil {
		log.Error(err)
		HttpNoData(w, http.StatusInternalServerError, cmd.ErrorServiceUnavailable, err.Error())
		return
	}
	if exist == false {
		HttpNoData(w, http.StatusOK, cmd.RepoOrItemNotExist, msg)
		return
	}

	if ensure == 0 {
		path := "/api/subscriptions/push/" + repository + "/" + dataitem + "?phase=1"

		retResp := ds.Response{}
		Pages := ds.ResultPages{}
		retResp.Data = &Pages

		resp, err := commToServerGetRsp("get", path, reqBody)
		defer resp.Body.Close()
		if err != nil {
			log.Error(err)
			HttpNoData(w, http.StatusInternalServerError, cmd.ErrorServiceUnavailable, err.Error())
			return
		}
		respbody, _ := ioutil.ReadAll(resp.Body)
		if resp.StatusCode == http.StatusOK {
			unmarshalerr := json.Unmarshal(respbody, &retResp)
			if unmarshalerr != nil {
				log.Error(unmarshalerr)
				HttpNoData(w, http.StatusInternalServerError, cmd.ErrorUnmarshal, "error while unmarshal respBody")
				return
			}
			log.Info(string(respbody))
			if Pages.Total > 0 {

				HttpNoData(w, http.StatusOK, cmd.ExitsConsumingPlan, "Exist consuming subscription plan")
				return
			} else {

				HttpNoData(w, http.StatusOK, cmd.NoConsumingPlan, "No consuming subscription plan")
				return
			}
		} else {
			HttpNoData(w, resp.StatusCode, cmd.ErrorUnknowError, "")
			log.Error(string(respbody))
			return
		}

	} else if ensure == 1 {
		err = delTagsForDelItem(repository, dataitem)
		if err != nil {
			log.Error(err)
			HttpNoData(w, http.StatusInternalServerError, cmd.ErrorSqlExec, "error while delete tags")
			return
		}
		err := delItem(repository, dataitem)
		if err != nil {
			log.Error(err)
			HttpNoData(w, http.StatusInternalServerError, cmd.ErrorSqlExec, "error while delete item")
			return
		}

		resp, err := commToServerGetRsp("delete", r.URL.Path, reqBody)
		if err != nil {
			log.Error(err)
			HttpNoData(w, resp.StatusCode, cmd.ErrorServiceUnavailable, "commToServer err")
			return
		}
		defer resp.Body.Close()

		respbody, _ := ioutil.ReadAll(resp.Body)
		retResp := ds.Response{}
		unmarshalerr := json.Unmarshal(respbody, &retResp)
		if unmarshalerr != nil {
			log.Error(unmarshalerr)
			HttpNoData(w, http.StatusInternalServerError, cmd.ErrorUnmarshal, "error while unmarshal respBody")
			return
		}
		if resp.StatusCode == http.StatusOK {
			HttpNoData(w, http.StatusOK, cmd.ResultOK, retResp.Msg)
			log.Info("Msg :", retResp.Msg, "HttpCode :", resp.StatusCode)
		} else {
			HttpNoData(w, resp.StatusCode, retResp.Code, retResp.Msg)
			log.Error("Error :", retResp.Msg, "HttpCode :", resp.StatusCode)
			rollbackDelItem(repository, dataitem)
			rollbackDelTags(repository, dataitem)
		}
	}

}