Пример #1
0
func RepoBuild(user *models.User, ctx *macaron.Context) {
	repo, err := getRepository(ctx.ParamsInt64(":id"))
	if err != nil {
		ctx.Error(500, err.Error())
		return
	}
	branch := ctx.Req.FormValue("branch")
	if ctx.Req.Method == "POST" {
		err := triggerBuild(repo.Owner, repo.Repo, "", user.Email)
		if err != nil {
			ctx.Error(500, err.Error())
			return
		}
		// dur := time.Since(repo.TriggerAt)
		// var limitTime = time.Minute * 10
		// if dur < limitTime { // too offen is not good
		// 	ctx.Error(500, fmt.Sprintf("Too offen, retry after %v", limitTime-dur))
		// 	return
		// }

		// repo.TriggerAt = time.Now()
		// repo.Valid = true
		// log.Println("%v", repo)
		// models.DB.Id(repo.Id).Cols("trigger_at", "valid").Update(repo)

		// go func() {
		// 	err := models.TriggerTravisBuild(repo.Owner, repo.Repo, branch, user.Email)
		// 	if err != nil {
		// 		log.Println(err)
		// 	}
		// }()
		ctx.JSON(200, map[string]string{
			"message": fmt.Sprintf("build for repo: %s is triggered", strconv.Quote(repo.Owner+"/"+repo.Repo)),
		})
		return
	}
	if ctx.Req.Method == "PUT" {
		if err := repo.AddBranch(branch); err != nil {
			ctx.Error(500, err.Error())
			return
		}

		ctx.JSON(200, map[string]string{
			"message": "change to valid",
		})
		return
	}

	// other methods
	ctx.JSON(500, map[string]string{
		"message": fmt.Sprintf("Method %s not supported", ctx.Req.Method),
	})
}