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 triggerBuild(owner, repo, ref string, email string) error { var mr = &models.Repository{ Owner: owner, Repo: repo, } has, err := models.DB.Get(mr) if err != nil { return err } var dur time.Duration if !has { // not exists in db, create one ri, err := GetRepoInfo(owner, repo) if err != nil { return err } if !ri.IsCmd { return errors.New("repo is not a go cli repo") } mr.Valid = true mr.TriggerAt = time.Now() if _, err = models.DB.Insert(mr); err != nil { return err } dur = LimitBuildInterval + time.Minute } else { dur = time.Since(mr.TriggerAt) } if dur < LimitBuildInterval { // too offen is not good return fmt.Errorf("Too offen, retry after %v", LimitBuildInterval-dur) } // update trigger time if has { mr.TriggerAt = time.Now() models.DB.Id(mr.Id).Update(mr) } go func() { err := models.TriggerTravisBuild(owner, repo, ref, email) if err != nil { log.Println(err) } }() return nil }