Exemple #1
0
// AppRegistScanHooksV1Handler adds a scan plugin to a user repo
// TODO: to make it easier as a start, we assume each repo could only have one scan plugin
func AppRegistScanHooksV1Handler(ctx *macaron.Context) (int, []byte) {
	data, err := ctx.Req.Body().Bytes()
	if err != nil {
		log.Errorf("[%s] Req.Body.Bytes error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Req.Body.Bytes Error"})
		return http.StatusBadRequest, result
	}

	type scanPlugin struct {
		//Name should be simple name or group name
		Name string
	}
	var n scanPlugin
	err = json.Unmarshal(data, &n)
	if err != nil {
		log.Errorf("[%s] Invalid body data: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Parse Req.Body.Bytes Error"})
		return http.StatusBadRequest, result
	}

	var reg models.ScanHookRegist
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	err = reg.Regist("appv1", namespace, repository, n.Name)
	if err != nil {
		log.Errorf("[%s] scan hook regist error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Scan Hook Regist Error"})
		return http.StatusBadRequest, result
	}

	return httpRet("AppV1 Scan Hook Regist", nil, err)
}