Ejemplo n.º 1
1
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",
	})
}
Ejemplo n.º 2
0
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",
	})
}
Ejemplo n.º 3
0
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)
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 8
0
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)
}
Ejemplo n.º 9
0
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")
}
Ejemplo n.º 10
0
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)
}
Ejemplo n.º 11
0
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,
	})
}
Ejemplo n.º 12
0
// 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
}
Ejemplo n.º 14
0
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)
}
Ejemplo n.º 15
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),
	})
}
Ejemplo n.º 16
0
func heartbeat(ctx *macaron.Context) {
	ctx.JSON(200, rbody.OkResp("heartbeat", nil))
}
Ejemplo n.º 17
0
func UserInfo(user *models.User, ctx *macaron.Context) {
	ctx.JSON(200, user)
}
Ejemplo n.º 18
0
// unauthorized is a helper method to respond with HTTP 401
func unauthorized(ctx *macaron.Context) {
	ctx.JSON(401, models.ResponseError{Success: false, Message: "Unauthorized"})
}
Ejemplo n.º 19
0
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)
}
Ejemplo n.º 20
0
func index(ctx *macaron.Context) {
	ctx.JSON(200, "ok")
}