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 docs(ctx *macaron.Context, locale i18n.Locale, name string) { docRoot := models.GetDocByLocale(name, locale.Lang) if docRoot == nil { docRoot = models.GetDocByLocale(name, "en-US") } link := strings.TrimPrefix(ctx.Params("*"), "/") link = strings.TrimSuffix(link, ".html") link = strings.TrimSuffix(link, ".md") ctx.Data["Link"] = "/docs/" + link var doc *models.DocNode if len(link) == 0 { ctx.Redirect("/docs/intro/") return } doc, _ = docRoot.GetNodeByLink(link) if doc == nil { doc, _ = docRoot.GetNodeByLink(link + "/") } if doc == nil { ctx.Error(404) return } ctx.Data["DocRoot"] = docRoot ctx.Data["Doc"] = doc ctx.Data["Title"] = doc.Name ctx.Data["Data"] = doc.GetContent() ctx.HTML(200, "document_"+name, ctx.Data) }
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 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 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 Build(tokens oauth2.Tokens, ctx *macaron.Context, req *http.Request) { gh := github.New(tokens.Access()) user, err := gh.User() if err != nil { ctx.Error(500, err.Error()) return } muser := &models.User{ Name: user.Name, Email: user.Email, GithubToken: tokens.Access(), Admin: false, } models.DB.Insert(muser) // repos var repos []*github.Repository reposKey := "orgs:" + user.Login + ":repos" if !rdx.Exists(reposKey).Val() || req.FormValue("refresh") != "" { var err error repos, err = gh.Repositories() if err != nil { ctx.Error(500, err.Error()) return } for _, repo := range repos { rdx.HMSet(reposKey, repo.Fullname, "") } } else { for _, repoName := range rdx.HKeys(reposKey).Val() { repos = append(repos, &github.Repository{ Fullname: repoName, }) } } // token rdx.Set("user:"******":github_token", tokens.Access(), 0) tokenKey := "user:"******":token" if !rdx.Exists(tokenKey).Val() { rdx.Set(tokenKey, "gr"+goutils.RandNString(40), 0) } token := rdx.Get(tokenKey).Val() rdx.Set("token:"+token+":user", user.Login, 0) ctx.Data["User"] = user ctx.Data["Token"] = token ctx.Data["Repos"] = repos ctx.HTML(200, "build") }
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 Team(ctx *macaron.Context, locale i18n.Locale) { ctx.Data["Link"] = "/team" df := models.GetDoc( "gitea", "team", locale.Lang) if df == nil { ctx.Error(404) return } ctx.Data["Data"] = string(df.Data) ctx.HTML(200, "page", ctx.Data) }
func Docs(ctx *macaron.Context, locale i18n.Locale) { docRoot := models.GetDocByLocale( "gitea", locale.Lang) if docRoot == nil { ctx.Error(404) return } link := strings.TrimSuffix( strings.TrimSuffix( strings.TrimPrefix( ctx.Params("*"), "/"), ".html"), ".md") ctx.Data["Link"] = "/docs/" + link if len(link) == 0 { ctx.Redirect("/docs/intro/") return } doc, _ := docRoot.GetNodeByLink(link) if doc == nil { doc, _ = docRoot.GetNodeByLink(link + "/") } if doc == nil { ctx.Error(404) return } ctx.Data["DocRoot"] = docRoot ctx.Data["Doc"] = doc ctx.Data["Title"] = doc.Name ctx.Data["Data"] = doc.GetContent() ctx.HTML(200, "gitea", ctx.Data) }
func render(ctx *macaron.Context, output []byte, err error, contentType string) { if err != nil { ctx.Error(500, err.Error()+"\n"+string(output)) return } if contentType == "" { contentType = "text" } switch contentType { case "html": ctx.Header().Set("Content-Type", "text/html") case "json": ctx.Header().Set("Content-Type", "application/json; charset=UTF-8") case "text": ctx.Header().Set("Content-Type", "text/plain") default: ctx.Header().Set("Content-Type", contentType) } ctx.Write(output) }
func UserNeeded(tokens oauth2.Tokens, ctx *macaron.Context) { user := &models.User{GithubToken: tokens.Access()} has, err := models.DB.Get(user) if err != nil { ctx.Error(500, err.Error()) return } if !has { gh := github.New(tokens.Access()) ghuser, err := gh.User() if err != nil { ctx.Error(500, err.Error()) return } user.Name = ghuser.Name user.Email = ghuser.Email user.Admin = false models.DB.Insert(user) } ctx.Map(user) }
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), }) }