func HookGogs(w http.ResponseWriter, r *http.Request) { body, _ := ioutil.ReadAll(r.Body) payload := new(GogsPayload) err := json.Unmarshal(body, payload) // body) if err != nil { log.Errorf("unmarshal request hook infomation error: %v", err) return } if sh.Test("x", AUTHCHECKER) { err := sh.Command(AUTHCHECKER, payload.Secret).Run() if err != nil { log.Errorf("authchecker not passed: %v", err) return } } if !sh.Test("x", RECEIVER) { log.Errorf("need script '%s'", RECEIVER) return } env := map[string]string{ "PUSH_NAME": payload.Pusher.Name, "PUSH_EMAIL": payload.Pusher.Email, } if err := sh.Command(RECEIVER, payload.Ref, env).Run(); err != nil { log.Errorf("call %s error: %v", RECEIVER, err) } fmt.Printf("receive: %v\n", payload.Ref) }
func TriggerBuildRepositoryById(rid int64) (err error) { repo, err := models.GetRepositoryById(rid) if err != nil { log.Errorf("get repo by id error: %v", err) return } cvsinfo, _ := base.ParseCvsURI(repo.Uri) defaultBranch := "master" if cvsinfo.Provider == base.PVD_GOOGLE { defaultBranch = "" } models.CreateNewBuilding(rid, defaultBranch, "", "", models.AC_SRCPKG) if !repo.IsCmd { return nil } oas := map[string][]string{ "windows": []string{"386", "amd64"}, "linux": []string{"386", "amd64"}, "darwin": []string{"amd64"}, } if repo.IsCgo { delete(oas, "windows") oas["linux"] = []string{"amd64"} } for os, archs := range oas { for _, arch := range archs { err := models.CreateNewBuilding(rid, defaultBranch, os, arch, models.AC_BUILD) if err != nil { log.Errorf("create module error: %v", err) } } } return nil }
func Repo(ctx *middleware.Context, params martini.Params, req *http.Request) { reponame := params["_1"] var repo *models.Repository var err error var tasks []models.Task var recentTask *models.Task if reponame != "" { repo, err = models.GetRepositoryByName(reponame) if err == models.ErrRepositoryNotExists { r, er := models.AddRepository(reponame) if er != nil { err = er ctx.Data["Error"] = err.Error() ctx.HTML(200, "repo") return } TriggerBuildRepositoryById(r.Id) ctx.Redirect(302, "/"+r.Uri) return } if err != nil { log.Errorf("get single repo from db error: %v", err) } } else { id, _ := strconv.Atoi(req.FormValue("id")) rid := int64(id) repo, err = models.GetRepositoryById(rid) if err != nil { log.Errorf("get single repo from db error: %v", err) } } tasks, err = models.GetTasksByRid(repo.Id) if err != nil { log.Errorf("get tasks by id, error: %v", err) } recentTask, _ = models.GetTaskById(1) ctx.Data["Repo"] = repo ctx.Data["RecentTask"] = recentTask ctx.Data["Tasks"] = tasks ctx.Data["DownCnt"] = models.RefreshPageView("/d/"+base.ToStr(repo.Id), 0) ctx.Data["RootUrl"] = config.Config.Server.RootUrl rus, err := models.GetAllLastRepoUpdate(repo.Id) if err != nil { log.Error("get last repo error: %v", err) } ctx.Data["Last"] = rus ctx.HTML(200, "repo") }
func PkgList(ctx *middleware.Context) { rs, err := models.GetAllLastRepoByOsArch(ctx.Query("os"), ctx.Query("arch")) if err != nil { ctx.JSON(400, nil) return } var result []*PackageItem var lastRid int64 = 0 for _, lr := range rs { if lr.Rid == lastRid { if pos := len(result) - 1; pos >= 0 { r := result[pos] r.Branches = append(r.Branches, Branch{lr.TagBranch, lr.PushURI, fmtTime(lr.Updated), lr.Os, lr.Arch, lr.ZipBallUrl}) } continue } lastRid = lr.Rid repo, err := models.GetRepositoryById(lr.Rid) if err != nil { log.Errorf("a missing repo in last_repo_update: %v", lr) continue } br := Branch{lr.TagBranch, lr.PushURI, fmtTime(lr.Updated), lr.Os, lr.Arch, lr.ZipBallUrl} result = append(result, &PackageItem{ Name: repo.Uri, // "github.com/nsf/gocode", Description: repo.Brief, // "golang code complete", Branches: []Branch{br}, }) } ctx.JSON(200, result) }
func UpdatePubAddr(tid int64, pubAddr string) error { tk, err := GetTaskById(tid) if err != nil { return err } condi := LastRepoUpdate{ Rid: tk.Rid, TagBranch: tk.TagBranch, Os: tk.Os, Arch: tk.Arch} lr := condi pushURI := tk.PushType + ":" + tk.PushValue if has, err := orm.Get(&lr); err == nil && has { orm.Update(&LastRepoUpdate{PushURI: pushURI, ZipBallUrl: pubAddr}, &condi) } else { condi.ZipBallUrl = pubAddr condi.PushURI = pushURI if _, err := orm.Insert(&condi); err != nil { log.Errorf("insert last_repo_update failed: %v", err) } } if _, err := orm.Id(tid).Update(&Task{ZipBallUrl: pubAddr}); err != nil { return err } return nil }
func Objc2Str(v interface{}) string { data, err := json.Marshal(v) if err != nil { log.Errorf("json2str got error: %v", err) return "" } return string(data) }
func History(ctx *middleware.Context, params martini.Params, req *http.Request) { id, _ := strconv.Atoi(req.FormValue("id")) tid := int64(id) task, err := models.GetTaskById(tid) if err != nil { log.Errorf("get task by id error: %v", err) } history, err := models.GetAllBuildHistoryByTid(tid) if err != nil { log.Errorf("get task history error: %v", err) } ctx.Data = map[string]interface{}{ "Task": task, "History": history, "AutoRefresh": ctx.Query("auto_refresh") == "true", } ctx.HTML(200, "history") }
func Home(ctx *middleware.Context) { pv := models.RefreshPageView("/") repos, err := models.GetAllRepos(50, 0) if err != nil { log.Errorf("get repos from db error: %v", err) } ctx.Data["Title"] = "home" ctx.Data["Repos"] = repos ctx.Data["PV"] = pv ctx.HTML(200, "home") }
func Search(ctx *middleware.Context) { log.Info(ctx.Request.RequestURI) pv := models.RefreshPageView(ctx.Request.RequestURI) repos, err := models.GetAllRepos(50, 0) if err != nil { log.Errorf("get repos from db error: %v", err) } ctx.Data["Title"] = "home" ctx.Data["Repos"] = repos ctx.Data["PV"] = pv ctx.HTML(200, "search") }
func (r *Rpc) GetMission(args *HostInfo, rep *Mission) error { log.Debugf("arch: %v, host: %v", args.Arch, args.Host) tasks, err := models.GetAvaliableTasks(args.Os, args.Arch) if err == models.ErrTaskNotAvaliable { rep.Idle = time.Second * 3 return nil } if err != nil { log.Errorf("rpc: get mission error: %v", err) return err } task := tasks[0] // use first task rep.Mid = task.Id rep.Repo = task.Repo rep.PushURI = task.PushType + ":" + task.PushValue rep.CgoEnable = task.CgoEnable rep.PkgInfo, _ = json.MarshalIndent(PkgInfo{ PushURI: task.PushType + ":" + task.PushValue, Author: []string{"unknown"}, Description: "unknown", }, "", " ") for _, tk := range tasks { if tk.TagBranch == "" { tk.TagBranch = "temp-" + tk.PushType + ":" + tk.PushValue } filename := fmt.Sprintf("%s-%s-%s.%s", filepath.Base(rep.Repo.Uri), tk.Os, tk.Arch, "zip") if tk.Action == models.AC_SRCPKG { filename = fmt.Sprintf("%s-all-source.%s", filepath.Base(rep.Repo.Uri), "zip") } key := com.Expand("m{tid}/{reponame}/br-{branch}/{filename}", map[string]string{ "tid": strconv.Itoa(int(rep.Mid)), "reponame": rep.Repo.Uri, "branch": tk.TagBranch, "filename": filename, }) bi := BuildInfo{ Action: tk.Action, Os: tk.Os, Arch: tk.Arch, UploadType: UT_QINIU, UploadData: base.Objc2Str(QiniuInfo{ Bulket: defaultBulket, Key: key, Token: qntoken(key), }), } rep.Builds = append(rep.Builds, bi) } return nil }
func AddRepository(repoName string) (r *Repository, err error) { cvsinfo, err := base.ParseCvsURI(repoName) // base.SanitizedRepoPath(rf.Name) if err != nil { log.Errorf("parse cvs url error: %v", err) return } repoUri := cvsinfo.FullPath r = new(Repository) r.Uri = repoUri pkginfo, err := gowalker.GetPkgInfo(repoUri) if err != nil { log.Errorf("gowalker not passed check: %v", err) return } r.IsCgo = pkginfo.IsCgo r.IsCmd = pkginfo.IsCmd r.Tags = strings.Split(pkginfo.Tags, "|||") // description r.Brief = pkginfo.Description base.ParseCvsURI(repoUri) if strings.HasPrefix(repoUri, "github.com") { // comunicate with github fields := strings.Split(repoUri, "/") owner, repoName := fields[1], fields[2] repo, _, err := GHClient.Repositories.Get(owner, repoName) if err != nil { log.Errorf("get information from github error: %v", err) } else { r.Brief = *repo.Description } } if _, err = CreateRepository(r); err != nil { log.Errorf("create repo error: %v", err) return } return r, nil }
func Download(ctx *middleware.Context) { rid, _ := strconv.Atoi(ctx.Request.FormValue("rid")) os := ctx.Request.FormValue("os") arch := ctx.Request.FormValue("arch") task, err := models.GetOneDownloadableTask(int64(rid), os, arch) if err != nil { log.Errorf("get download task: %v", err) http.Error(ctx.ResponseWriter, err.Error(), http.StatusNotFound) return } models.RefreshPageView("/d/" + ctx.Query("rid")) ctx.Redirect(302, task.ZipBallUrl) }
func UpdateTaskStatus(tid int64, status string, output string) error { log.Debugf("update task(%d) status(%s)", tid, status) if _, err := orm.Id(tid).Update(&Task{Status: status}); err != nil { log.Errorf("update task status error: %v", err) } condi := &BuildHistory{Tid: tid, Status: status} if has, err := orm.Get(condi); err == nil && has { _, er := orm.Update(&BuildHistory{Output: output}, condi) return er } _, err := orm.Insert(&BuildHistory{Tid: tid, Status: status, Output: output}) return err }
func (this *gowatch) drainExec() { log.Println("command:", this.cmd) var msg string for { startTime := time.Now() cmd := this.cmd if len(cmd) == 0 { cmd = []string{"echo", "no command specified"} } cprintf(CGREEN, "exec start") c := StartCmd(cmd[0], cmd[1:]...) // Start to run command err := c.Start() if err != nil { cprintf("35", err.Error()) } // Wait until killed or finished select { case msg = <-this.sig: cprintf(CYELLOW, "program terminated, signal(%s)", this.KillSignal) if err := KillCmd(c, this.KillSignal); err != nil { log.Errorf("group kill: %v", err) } if msg == "EXIT" { os.Exit(1) } goto SKIP_WAITING case err = <-Go(c.Wait): if err != nil { cprintf(CPURPLE, "program exited: %v", err) } } log.Infof("finish in %s", time.Since(startTime)) // Whether to restart right now if this.AutoRestart { goto SKIP_WAITING } cprintf("33", "-- wait signal --") if msg = <-this.sig; msg == "EXIT" { os.Exit(1) } SKIP_WAITING: if this.RestartInterval > 0 { log.Infof("restart after %s", this.RestartInterval) } time.Sleep(this.RestartInterval) } }
func RefreshPageView(uri string, add ...int64) int64 { pvOnce.Do(func() { orm.Insert(&PageView{uri, 0}) go drainPv() }) if pageView[uri] == 0 { pv := &PageView{Uri: uri} _, err := orm.Get(pv) if err != nil { log.Errorf("get pv from db error: %v", err) return -1 } pageView[uri] = pv.TotalCount } if len(add) == 0 { pageView[uri] += 1 } else { pageView[uri] += add[0] } return pageView[uri] }
func setUp() error { var err error var binDir = filepath.Join(SELFDIR, "bin") var tmpDir = filepath.Join(SELFDIR, "tmp/tmp-gopath") os.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH")) if _, err := exec.LookPath("go"); err != nil { log.Error("require go tool installed") return err } sess := sh.NewSession() sess.SetEnv("GOBIN", binDir) sess.SetEnv("GOPATH", tmpDir) if !sh.Test("x", GOPM) { os.RemoveAll(filepath.Join(binDir, "gopm")) defer os.RemoveAll(tmpDir) err = sess.Command("go", "get", "-u", "-v", "github.com/gpmgo/gopm").Run() if err != nil { log.Errorf("install gopm error: %v", err) return err } } return nil }
func ForceRebuild(tf TaskForm, ctx *middleware.Context) { if err := models.ResetTask(tf.Tid); err != nil { log.Errorf("reset task failed: %v", err) } ctx.Redirect(302, "/history?id="+strconv.Itoa(int(tf.Tid))+"&auto_refresh=true") }