예제 #1
0
파일: repo.go 프로젝트: mikkeloscar/maze
func SetRepo() gin.HandlerFunc {
	return func(c *gin.Context) {
		owner := c.Param("owner")
		name := c.Param("name")

		repo, err := store.GetRepoByOwnerName(c, owner, name)
		if err != nil {
			log.Errorf("failed to find repo: %s/%s: %s", owner, name, err)
			c.AbortWithStatus(http.StatusNotFound)
		}

		c.Set("repo", repo)
		c.Next()
	}
}
예제 #2
0
파일: repo.go 프로젝트: mikkeloscar/maze
func PostRepo(c *gin.Context) {
	remote := remote.FromContext(c)
	user := session.User(c)
	owner := c.Param("owner")
	name := c.Param("name")

	if !repo.ValidRepoName(name) {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if owner != user.Login {
		c.AbortWithStatus(http.StatusUnauthorized)
		return
	}

	in := struct {
		SourceRepo   *string   `json:"source_repo" binding:"required"`
		SourceBranch *string   `json:"source_branch,omitempty"`
		BuildBranch  *string   `json:"build_branch,omitempty"`
		Archs        *[]string `json:"archs,omitempty"`
		Private      *bool     `json:"private,omitempty"`
	}{}
	err := c.BindJSON(&in)
	if err != nil {
		log.Errorf("failed to parse request body: %s", err)
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if in.Private == nil {
		private := false
		in.Private = &private
	}

	if in.Archs == nil || len(*in.Archs) == 0 {
		defArch := []string{"x86_64"}
		in.Archs = &defArch
	}

	if !repo.ValidArchs(*in.Archs) {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	sourceOwner, sourceName, err := splitRepoName(*in.SourceRepo)
	if err != nil {
		log.Error(err)
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if in.SourceBranch == nil {
		sourceBranch := "master"
		in.SourceBranch = &sourceBranch
	}

	if in.BuildBranch == nil {
		buildBranch := "build"
		in.BuildBranch = &buildBranch
	}

	// error if the repository already exists
	_r, err := store.GetRepoByOwnerName(c, owner, name)
	if _r != nil {
		log.Errorf("unable to add repo: %s/%s, already exists", owner, name)
		c.AbortWithStatus(http.StatusConflict)
		return
	}

	// Fetch source repo
	r, err := remote.Repo(user, sourceOwner, sourceName)
	if err != nil {
		log.Errorf("unable to get repo: %s/%s: %s", sourceOwner, sourceName, err)
		c.AbortWithStatus(http.StatusNotFound)
		return
	}

	p, err := remote.Perm(user, sourceOwner, sourceName)
	if err != nil {
		log.Errorf("unable to get repo permission for: %s/%s: %s", sourceOwner, sourceName, err)
		c.AbortWithStatus(http.StatusNotFound)
		return
	}

	if !(p.Admin || (p.Read && p.Write)) {
		log.Errorf("pull/push access required")
		c.AbortWithStatus(http.StatusUnauthorized)
		return
	}

	err = remote.SetupBranch(user, sourceOwner, sourceName, *in.SourceBranch, *in.BuildBranch)
	if err != nil {
		log.Errorf("failed to setup build branch: %s", err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	r.UserID = user.ID
	r.Owner = owner
	r.Name = name
	r.Private = *in.Private
	r.SourceBranch = *in.SourceBranch
	r.BuildBranch = *in.BuildBranch
	r.LastCheck = time.Now().UTC().Add(-1 * time.Hour)
	r.Hash = base32.StdEncoding.EncodeToString(
		securecookie.GenerateRandomKey(32),
	)

	fsRepo := repo.NewRepo(r, repo.RepoStorage)

	err = fsRepo.InitDir()
	if err != nil {
		log.Errorf("failed to create repo storage path '%s' on disk: %s", fsRepo.Path(), err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	err = fsRepo.InitEmptyDBs()
	if err != nil {
		log.Errorf("failed to initialize empty dbs: %s", err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	err = store.CreateRepo(c, r)
	if err != nil {
		log.Errorf("failed to add repo: %s", err)
		err = fsRepo.ClearPath()
		if err != nil {
			log.Errorf("failed to cleanup repo path '%s': %s", fsRepo.Path(), err)
		}
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.JSON(http.StatusOK, r)
}