func Repo(c *gin.Context) *repo.Repo { v, ok := c.Get("repo") if !ok { return nil } r, ok := v.(*model.Repo) if !ok { return nil } return repo.NewRepo(r, repo.RepoStorage) }
func parseRepo(uri, basePath string) (*Repo, error) { matches := repoPattReg.FindStringSubmatch(uri) if len(matches) > 0 { return &Repo{ local: *repo.NewRepo(&model.Repo{ Name: matches[1], }, basePath), url: uri, }, nil } matches = repoPatt.FindStringSubmatch(uri) if len(matches) == 0 { return nil, fmt.Errorf("invalid repo uri: %s", uri) } return &Repo{ local: *repo.NewRepo(&model.Repo{ Owner: matches[2], Name: matches[3], }, basePath), url: uri, }, nil }
// Run runs the checker that checks for package updates in repos. func (c *Checker) Run() { for { select { case <-time.After(time.Minute * 10): // case <-time.After(time.Second * 60): // TODO: maybe run in goroutine repos, err := c.Store.Repos().GetRepoList() if err != nil { log.Errorf("failed to fetch repos from db: %s", err) break } if len(repos) > 0 { log.Info("Checking for package updates for all repos") } for _, r := range repos { // only check for updates if last check was // more than an hour ago last := r.LastCheck.Add(1 * time.Hour) // last := r.LastCheck.Add(1 * time.Minute) if time.Now().UTC().Before(last) { continue } user, err := c.Store.Users().Get(r.UserID) if err != nil { log.Errorf("failed to fetch user from db: %s", err) break } err = c.update(user, repo.NewRepo(r, repo.RepoStorage)) if err != nil { log.Errorf("failed to request update: %s", err) break } // update lastCheck r.LastCheck = time.Now().UTC() c.Store.Repos().Update(r) } // clear expired packages c.State.ClearExpired() } } }
// Test Add repo entry to pacman.conf.template. func TestAddRepoEntry(t *testing.T) { src := "pacman.conf.template" err := exec.Command("cp", "contrib/etc/pacman.conf.template", src).Run() assert.NoError(t, err, "should not fail") r := &Repo{ local: *repo.NewRepo(&model.Repo{Name: "test"}, "."), url: "http://test.repo.com", } err = addRepoEntry(src, r) assert.NoError(t, err, "should not fail") err = addRepoEntry(src, r) assert.NoError(t, err, "should not fail") // src does not exist err = addRepoEntry("fake", r) assert.Error(t, err, "should fail") // invalid src invalidSrc := "pacman.conf.template.fake" f, err := os.Create(invalidSrc) assert.NoError(t, err, "should not fail") err = f.Close() assert.NoError(t, err, "should not fail") err = addRepoEntry(invalidSrc, r) assert.Error(t, err, "should fail") // cleanup err = os.Remove(src) assert.NoError(t, err, "should not fail") err = os.Remove(invalidSrc) assert.NoError(t, err, "should not fail") }
"os" "path" "testing" "github.com/mikkeloscar/maze/model" "github.com/mikkeloscar/maze/repo" "github.com/stretchr/testify/assert" ) const ( baseDir = "build_home_test" ) var ( pkgRepo = &Repo{ local: *repo.NewRepo(&model.Repo{Name: "repo"}, baseDir), } builder = &Builder{ repo: pkgRepo, workdir: baseDir + "/sources", } aurSrc = &AUR{baseDir + "/sources"} ) func setupRepoDirs(repos []*Repo) error { for _, repo := range repos { err := repo.local.InitDir() if err != nil { return err } repo.url = repo.local.PathDeep("x86_64")
package main import ( "os" "testing" "github.com/mikkeloscar/maze/model" "github.com/mikkeloscar/maze/repo" "github.com/stretchr/testify/assert" ) var ( repo1 = &Repo{ local: *repo.NewRepo(&model.Repo{Name: "repo1"}, baseDir+"/repo"), } repo2 = &Repo{ local: *repo.NewRepo(&model.Repo{Name: "repo2"}, baseDir+"/repo"), } ) // Test GetUpdated. func TestGetUpdated(t *testing.T) { aurSrc = &AUR{baseDir + "/sources"} _, _, err := setupBuildDirs(baseDir, repo1, repo2) assert.NoError(t, err, "should not fail") pkgs, err := aurSrc.Get([]string{"wlc-git"}) assert.NoError(t, err, "should not fail") _, err = repo2.GetUpdated(pkgs)
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) }