Example #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",
	})
}
Example #2
0
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)
}
Example #3
0
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")
}