示例#1
0
文件: repos.go 项目: jonbodner/lgtm
// GetRepos gets the active repository list.
func GetRepos(c *gin.Context) {
	user := session.User(c)
	repos, err := cache.GetRepos(c, user)
	if err != nil {
		logrus.Errorf("Error getting remote repository list. %s", err)
		c.String(500, "Error getting remote repository list")
		return
	}

	// copy the slice since we are going to mutate it and don't
	// want any nasty data races if the slice came from the cache.
	repoc := make([]*model.Repo, len(repos))
	copy(repoc, repos)

	repom, err := store.GetRepoIntersectMap(c, repos)
	if err != nil {
		logrus.Errorf("Error getting active repository list. %s", err)
		c.String(500, "Error getting active repository list")
		return
	}

	// merges the slice of active and remote repositories favoring
	// and swapping in local repository information when possible.
	for i, repo := range repoc {
		repo_, ok := repom[repo.Slug]
		if ok {
			repoc[i] = repo_
		}
	}
	c.JSON(200, repoc)
}
示例#2
0
// GetMaintainer gets the MAINTAINER configuration file and returns
// a subset of the file with members belonging to the specified organization.
func GetMaintainerOrg(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		team  = c.Param("org")
		user  = session.User(c)
	)
	repo, err := store.GetRepoOwnerName(c, owner, name)
	if err != nil {
		log.Errorf("Error getting repository %s. %s", name, err)
		c.AbortWithStatus(404)
		return
	}
	file, err := remote.GetContents(c, user, repo, "MAINTAINERS")
	if err != nil {
		log.Errorf("Error getting repository %s. %s", repo.Slug, err)
		c.String(404, "MAINTAINERS file not found. %s", err)
		return
	}
	maintainer, err := model.ParseMaintainer(file)
	if err != nil {
		log.Errorf("Error parsing MAINTAINERS file for %s. %s", repo.Slug, err)
		c.String(500, "Error parsing MAINTAINERS file. %s.", err)
		return
	}
	subset, err := model.FromOrg(maintainer, team)
	if err != nil {
		log.Errorf("Error getting subset of MAINTAINERS file for %s/%s. %s", repo.Slug, team, err)
		c.String(500, "Error getting subset of MAINTAINERS file. %s.", err)
		return
	}
	c.JSON(200, subset)
}
示例#3
0
文件: repos.go 项目: jonbodner/lgtm
// DeleteRepo deletes a repository configuration.
func DeleteRepo(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)
	repo, err := store.GetRepoOwnerName(c, owner, name)
	if err != nil {
		logrus.Errorf("Error getting repository %s. %s", name, err)
		c.AbortWithStatus(404)
		return
	}
	err = store.DeleteRepo(c, repo)
	if err != nil {
		logrus.Errorf("Error deleting repository %s. %s", name, err)
		c.AbortWithStatus(500)
		return
	}
	link := fmt.Sprintf(
		"%s/hook",
		httputil.GetURL(c.Request),
	)
	err = remote.DelHook(c, user, repo, link)
	if err != nil {
		logrus.Errorf("Error deleting repository hook for %s. %s", name, err)
	}
	c.String(200, "")
}
示例#4
0
文件: index.go 项目: jonbodner/lgtm
func Index(c *gin.Context) {
	user := session.User(c)

	switch {
	case user == nil:
		c.HTML(200, "brand.html", gin.H{})
	default:
		teams, _ := cache.GetTeams(c, user)
		csrf, _ := token.New(token.CsrfToken, user.Login).Sign(user.Secret)
		c.HTML(200, "index.html", gin.H{"user": user, "csrf": csrf, "teams": teams})
	}
}
示例#5
0
文件: teams.go 项目: jonbodner/lgtm
// GetTeams gets the list of user teams.
func GetTeams(c *gin.Context) {
	user := session.User(c)
	teams, err := cache.GetTeams(c, user)
	if err != nil {
		logrus.Errorf("Error getting teams for user %s. %s", user.Login, err)
		c.String(500, "Error getting team list")
		return
	}
	teams = append(teams, &model.Team{
		Login:  user.Login,
		Avatar: user.Avatar,
	})
	c.JSON(200, teams)
}
示例#6
0
文件: repos.go 项目: jonbodner/lgtm
// PostRepo activates a new repository.
func PostRepo(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)

	// verify repo doesn't already exist
	if _, err := store.GetRepoOwnerName(c, owner, name); err == nil {
		c.AbortWithStatus(409)
		c.String(409, "Error activating a repository that is already active.")
		return
	}

	repo, err := remote.GetRepo(c, user, owner, name)
	if err != nil {
		c.String(404, "Error finding repository in GitHub. %s")
		return
	}
	repo.UserID = user.ID
	repo.Secret = model.Rand()

	// creates a token to authorize the link callback url
	t := token.New(token.HookToken, repo.Slug)
	sig, err := t.Sign(repo.Secret)
	if err != nil {
		c.String(500, "Error activating repository. %s")
		return
	}

	// create the hook callback url
	link := fmt.Sprintf(
		"%s/hook?access_token=%s",
		httputil.GetURL(c.Request),
		sig,
	)
	err = remote.SetHook(c, user, repo, link)
	if err != nil {
		c.String(500, "Error creating hook. %s", err)
		return
	}

	err = store.CreateRepo(c, repo)
	if err != nil {
		c.String(500, "Error activating the repository. %s", err)
		return
	}
	c.JSON(200, repo)
}
示例#7
0
// GetMaintainer gets the MAINTAINER configuration file.
func GetMaintainer(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)
	repo, err := store.GetRepoOwnerName(c, owner, name)
	if err != nil {
		log.Errorf("Error getting repository %s. %s", name, err)
		c.AbortWithStatus(404)
		return
	}
	file, err := remote.GetContents(c, user, repo, "MAINTAINERS")
	if err != nil {
		log.Debugf("no MAINTAINERS file for %s. Checking for team members.", repo.Slug)
		members, merr := cache.GetMembers(c, user, repo.Owner)
		if merr != nil {
			log.Errorf("Error getting repository %s. %s", repo.Slug, err)
			log.Errorf("Error getting org members %s. %s", repo.Owner, merr)
			c.String(404, "MAINTAINERS file not found. %s", err)
			return
		} else {
			log.Printf("found %v members", len(members))
			for _, member := range members {
				file = append(file, member.Login...)
				file = append(file, '\n')
			}
		}
	}

	maintainer, err := model.ParseMaintainer(file)
	if err != nil {
		log.Errorf("Error parsing MAINTAINERS file for %s. %s", repo.Slug, err)
		c.String(500, "Error parsing MAINTAINERS file. %s.", err)
		return
	}
	c.JSON(200, maintainer)
}
示例#8
0
文件: access.go 项目: jonbodner/lgtm
func RepoPull(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)

	perm, err := cache.GetPerm(c, user, owner, name)
	if err != nil {
		log.Errorf("Cannot find repository %s/%s. %s", owner, name, err)
		c.String(404, "Not Found")
		c.Abort()
		return
	}
	if !perm.Pull {
		log.Errorf("User %s does not have Pull access to repository %s/%s", user.Login, owner, name)
		c.String(404, "Not Found")
		c.Abort()
		return
	}
	log.Debugf("User %s granted Pull access to %s/%s", user.Login, owner, name)
	c.Next()
}
示例#9
0
文件: users.go 项目: jonbodner/lgtm
// GetUser gets the currently authenticated user.
func GetUser(c *gin.Context) {
	c.JSON(200, session.User(c))
}