Exemplo 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",
	})
}
Exemplo n.º 2
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")
}
Exemplo n.º 3
0
func TriggerTravisBuild(owner, repo, branch, email string) error {
	cfg := travis.DefaultTravisConfig
	cfg.Env.Global = []string{
		"GIT_BRANCH=" + branch,
		"GITHUB_REPO=" + owner + "/" + repo,
	}
	cfg.Notifications.Email.Recipients = []string{email}
	data, _ := yaml.Marshal(cfg)

	gh := github.New(GITHUB_TOKEN)
	return gh.UpdateFile(DefaultTriggerOwner, DefaultTriggerRepo, &github.CommitFile{
		Path:    ".travis.yml",
		Message: fmt.Sprintf("trigger %s by %s", strconv.Quote(owner+"/"+repo), email),
		Content: string(data),
	})
}
Exemplo n.º 4
0
func (user *User) SyncGithub() error {
	gh := github.New(user.GithubToken)
	repos, err := gh.Repositories()
	if err != nil {
		return err
	}

	for _, ghRepo := range repos {
		parts := strings.Split(ghRepo.Fullname, "/")
		if len(parts) != 2 {
			continue
		}
		var repo = &Repository{
			Owner: parts[0],
			Repo:  parts[1],
		}
		exists, err := DB.Get(repo)
		if err != nil {
			return err
		}
		repo.UserId = user.Id
		if exists {
			repoId := repo.Id
			_, err := DB.Update(repo, &Repository{Id: repoId})
			if err != nil {
				return err
			}
		} else {
			_, err := DB.Insert(repo)
			if err != nil {
				return err
			}
		}
	}
	user.RepoUpdatedAt = time.Now()
	_, err = DB.Id(user.Id).Cols("repo_updated_at").Update(user)
	if err != nil {
		log.Println(err)
	}
	return nil
}
Exemplo n.º 5
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)
}