func TriggerBuild(tokens oauth2.Tokens, ctx *macaron.Context) { r := ctx.Req owner, repo := r.FormValue("owner"), r.FormValue("repo") branch := r.FormValue("branch") gh := github.New(tokens.Access()) user, err := gh.User() if err != nil { ctx.Error(500, err.Error()) return } go func() { err := models.TriggerTravisBuild(owner, repo, branch, user.Email) if err != nil { log.Println(err) } }() ctx.JSON(200, map[string]string{ "message": "build is triggered", }) }
func AnonymousTriggerBuild(ctx *macaron.Context) { r := ctx.Req owner, repo := r.FormValue("owner"), r.FormValue("repo") branch := r.FormValue("branch") if owner == "" || repo == "" { ctx.Error(500, "owner or repo should not be empty") return } var mrepo = &models.Repository{ Owner: owner, Repo: repo, } has, err := models.DB.Get(mrepo) if err != nil { ctx.Error(500, err.Error()) return } if has && mrepo.UserId != 0 { ctx.Error(500, "This repo is limited to its author to build") // TODO: show who is owned return } if err := triggerBuild(owner, repo, branch, "*****@*****.**"); err != nil { ctx.Error(500, err.Error()) return } ctx.JSON(200, map[string]string{ "message": "build is triggered", }) }
func Static(ctx *macaron.Context) { if len(ctx.Params(":all")) > 0 { f, err := os.Open( path.Join( "docs", "gitea", "images", ctx.Params(":all"))) defer f.Close() if err != nil { ctx.JSON(500, map[string]interface{}{ "error": err.Error(), }) return } if _, err = io.Copy(ctx.RW(), f); err != nil { ctx.JSON(500, map[string]interface{}{ "error": err.Error(), }) return } return } ctx.Error(404) }
func GetCustomers(ctx *macaron.Context, x *xorm.Engine) { m, _ := url.ParseQuery(ctx.Req.URL.RawQuery) glog.V(1).Infof("Debug %#v", m) skip := 0 limit := 0 var err error if v, ok := m["skip"]; ok { skip, _ = strconv.Atoi(v[0]) } if v, ok := m["limit"]; ok { limit, _ = strconv.Atoi(v[0]) } cs := make([]Customer, 0) err = x.Limit(limit, skip).Find(&cs) if err != nil { glog.V(1).Infof("Get customer from db fail:%s", err.Error()) ctx.JSON(http.StatusInternalServerError, map[string]string{"message": err.Error()}) return } ctx.JSON(http.StatusOK, cs) }
func RecentBuild(ctx *macaron.Context) { var repos []models.Repository err := models.DB.Limit(10).Desc("trigger_at").Where("valid=?", true).Find(&repos) if err != nil { ctx.Error(500, err.Error()) return } ctx.JSON(200, repos) }
func PostGood(ctx *macaron.Context, x *xorm.Engine, g Goods) { _, err := x.Insert(g) if err != nil { glog.V(1).Infof("Insert good %#v fail:%s", g, err.Error()) ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()}) } ctx.JSON(http.StatusCreated, "SUCCESS") return }
func PostCustomer(ctx *macaron.Context, x *xorm.Engine, c Customer) { _, err := x.Insert(c) if err != nil { glog.V(1).Infof("Insert customer %#v fail:%s", c, err.Error()) ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()}) } ctx.JSON(http.StatusCreated, "SUCCESS") return }
func RepoList(ctx *macaron.Context) { var repos []models.Repository // TODO: change limit to paginate err := models.DB.Limit(100).Where("valid=?", true).Desc("updated_at").Find(&repos) if err != nil { ctx.Error(500, err.Error()) return } ctx.JSON(200, repos) }
func DownloadStats(ctx *macaron.Context, r *http.Request) { org := ctx.Params(":org") name := ctx.Params(":name") branch := ctx.Params(":branch") _ = branch repo := org + "/" + name osarch := ctx.Params(":os") + "-" + ctx.Params(":arch") rdx.Incr("downloads:" + repo) rdx.Incr("downloads:" + repo + ":" + osarch) ctx.JSON(200, "update success") }
func myDatabaseListHandler(ctx *macaron.Context) { db, err := gorm.Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True&loc=Local") if err != nil { log.Println("Database Error") } db.DB() contact_entries := []ContactEntry{} db.Find(&contact_entries) ctx.JSON(200, contact_entries) }
func Fibonacci(ctx *macaron.Context) { number := ctx.QueryInt("number") log.Println("DBG:", number) result := models.FibonacciSequence(number) if result < 0 { ctx.JSON(422, map[string]string{ "error": "Unprocessable number: " + com.ToStr(number), }) return } ctx.JSON(200, map[string]interface{}{ "result": result, }) }
// SignIn registers with user data func SignIn(ctx *macaron.Context, form auth.SignIn) { u, err := models.UserSignIn(form.Uname, form.Pass) if err != nil { ctx.JSON(http.StatusBadRequest, models.ResponseError{ Success: false, Message: err.Error(), }) return } tokens, _ := u.SignedString() ctx.JSON(http.StatusOK, auth.ResponseAuth{ Success: true, Token: tokens, }) }
func PostOrder(ctx *macaron.Context, x *xorm.Engine, o Order) { alipaytype := ctx.Params("alipaytype") _, err := x.Insert(o) if err != nil { glog.V(1).Infof("Insert order %#v fail:%s", o, err.Error()) ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()}) } //after saved to db,we call alipay and submit to alipay outhtml, outscript := alipay.Form(o, alipaytype) ob := map[string]string{"html": outhtml, "script": outscript} ctx.Resp.Header().Set("Content-Type", "application/json") js, _ := json.Marshal(ob) ctx.Resp.WriteHeader(http.StatusOK) ctx.Resp.Write([]byte(js)) return }
func UserRepoList(user *models.User, ctx *macaron.Context) { if ctx.Req.Method == "POST" { if time.Since(user.RepoUpdatedAt) > time.Minute { if err := user.SyncGithub(); err != nil { ctx.JSON(500, err.Error()) } else { ctx.JSON(200, map[string]string{ "message": "sync github success", }) } } else { ctx.JSON(200, map[string]string{ "message": "try after a minute", }) } return } if ctx.Req.Method != "GET" { ctx.JSON(500, map[string]string{ "message": fmt.Sprintf("Method %s not supported", ctx.Req.Method), }) return } repos, err := user.Repositories() if err != nil { ctx.Error(500, err.Error()) return } if len(repos) == 0 && time.Since(user.RepoUpdatedAt) > time.Hour*24 { user.SyncGithub() repos, _ = user.Repositories() } ctx.JSON(200, repos) }
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), }) }
func heartbeat(ctx *macaron.Context) { ctx.JSON(200, rbody.OkResp("heartbeat", nil)) }
func UserInfo(user *models.User, ctx *macaron.Context) { ctx.JSON(200, user) }
// unauthorized is a helper method to respond with HTTP 401 func unauthorized(ctx *macaron.Context) { ctx.JSON(401, models.ResponseError{Success: false, Message: "Unauthorized"}) }
func myJsonHandler(ctx *macaron.Context) { t := time.Now() p := Person{"James", 25, fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())} ctx.JSON(200, &p) }
func index(ctx *macaron.Context) { ctx.JSON(200, "ok") }