2023-08-18 23:59:15 +03:00
|
|
|
package worker
|
2023-08-11 20:12:15 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-18 23:59:15 +03:00
|
|
|
"database/sql"
|
2023-08-11 20:12:15 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-08-18 23:59:15 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/models"
|
2023-08-19 16:55:52 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/modules"
|
2023-08-18 23:59:15 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/storage"
|
2023-08-11 20:12:15 +03:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/filemode"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
"github.com/go-git/go-git/v5/storage/memory"
|
2023-08-11 21:45:08 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-08-11 20:12:15 +03:00
|
|
|
"golang.org/x/mod/modfile"
|
|
|
|
"golang.org/x/mod/module"
|
|
|
|
)
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func Run(ctx context.Context, store storage.Storage, td time.Duration) {
|
2023-08-19 16:55:52 +03:00
|
|
|
modTicker := time.NewTicker(5 * time.Second)
|
|
|
|
defer modTicker.Stop()
|
|
|
|
pkgTicker := time.NewTicker(5 * time.Second)
|
|
|
|
defer pkgTicker.Stop()
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2023-08-19 16:55:52 +03:00
|
|
|
case <-pkgTicker.C:
|
2023-08-18 23:59:15 +03:00
|
|
|
packages, err := store.PackagesProcess(ctx, td)
|
2023-08-19 16:55:52 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Fatal(ctx, "failed to get packages to process: %v", err)
|
2023-08-18 23:59:15 +03:00
|
|
|
}
|
|
|
|
wg.Add(len(packages))
|
|
|
|
for _, pkg := range packages {
|
|
|
|
go func(p *models.Package) {
|
2023-08-19 16:55:52 +03:00
|
|
|
if err := parseModFile(ctx, store, p); err != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "failed to process package %s: %v", p.Name, err)
|
2023-08-18 23:59:15 +03:00
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
p.LastCheck.Time = time.Now()
|
2023-08-18 23:59:15 +03:00
|
|
|
wg.Done()
|
|
|
|
}(pkg)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
2023-08-18 23:59:15 +03:00
|
|
|
wg.Wait()
|
2023-08-19 16:55:52 +03:00
|
|
|
if err = store.PackagesUpdateLastCheck(ctx, packages); err != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "update packages last_check %#+v, err: %v", packages, err)
|
2023-08-19 16:55:52 +03:00
|
|
|
}
|
|
|
|
case <-modTicker.C:
|
|
|
|
modules, err := store.ModulesProcess(ctx, td)
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Fatal(ctx, "failed to get modules to process: %v", err)
|
2023-08-19 16:55:52 +03:00
|
|
|
}
|
|
|
|
if err := processModules(ctx, store, modules); err != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "failed to process modules: %v", err)
|
2023-08-19 16:55:52 +03:00
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
2023-08-18 23:59:15 +03:00
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
func parseModFile(ctx context.Context, store storage.Storage, pkg *models.Package) error {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Info(ctx, "process package %v", pkg)
|
2023-08-11 20:12:15 +03:00
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
u, err := url.Parse(pkg.URL)
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var rev string
|
|
|
|
if idx := strings.Index(u.Path, "@"); idx > 0 {
|
|
|
|
rev = u.Path[idx+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneOpts := &git.CloneOptions{
|
2023-08-19 16:55:52 +03:00
|
|
|
URL: pkg.URL,
|
2023-08-11 20:12:15 +03:00
|
|
|
Progress: os.Stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rev) == 0 {
|
|
|
|
cloneOpts.SingleBranch = true
|
|
|
|
cloneOpts.Depth = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cloneOpts.Validate(); err != nil {
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := git.CloneContext(ctx, memory.NewStorage(), nil, cloneOpts)
|
|
|
|
if err != nil {
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := repo.Head()
|
|
|
|
if err != nil {
|
2024-04-02 22:54:15 +03:00
|
|
|
return fmt.Error("failed to get head: %v", err)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := repo.CommitObject(ref.Hash())
|
|
|
|
if err != nil {
|
2024-04-02 22:54:15 +03:00
|
|
|
return fmt.Error("failed to get commit: %v", err)
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := commit.Tree()
|
|
|
|
if err != nil {
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
unique := make(map[string]*models.Module)
|
2023-08-11 20:12:15 +03:00
|
|
|
var mvs []module.Version
|
|
|
|
err = tree.Files().ForEach(func(file *object.File) error {
|
|
|
|
if file == nil {
|
|
|
|
err = errors.New("file pointer is nil")
|
2024-03-25 22:54:25 +03:00
|
|
|
logger.Error(ctx, "file tree error", err)
|
2023-08-11 20:12:15 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch file.Mode {
|
|
|
|
case filemode.Regular:
|
|
|
|
if strings.HasSuffix(file.Name, "go.mod") {
|
2023-08-19 16:55:52 +03:00
|
|
|
if mvs, err = parseFile(file); err != nil {
|
2023-08-11 20:12:15 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range mvs {
|
2023-08-18 23:59:15 +03:00
|
|
|
unique[mvs[i].Path] = &models.Module{
|
2023-08-19 16:55:52 +03:00
|
|
|
Name: mvs[i].Path,
|
|
|
|
Version: mvs[i].Version,
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
modules := make([]*models.Module, 0, len(unique))
|
2023-08-11 20:12:15 +03:00
|
|
|
for _, v := range unique {
|
2023-08-19 16:55:52 +03:00
|
|
|
modules = append(modules, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(modules, func(i, j int) bool {
|
|
|
|
return modules[i].Name < modules[j].Name
|
|
|
|
})
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
if err = store.PackageModulesCreate(ctx, pkg, modules); err != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "failed to set create modules: %v", err)
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func processModules(ctx context.Context, store storage.Storage, mods []*models.Module) error {
|
|
|
|
mvs := make(map[string]*models.Module, len(mods))
|
|
|
|
|
|
|
|
for _, mod := range mods {
|
|
|
|
mvs[mod.Name] = mod
|
|
|
|
}
|
|
|
|
|
|
|
|
mvsu := make([]module.Version, 0, len(mvs))
|
|
|
|
for _, mv := range mvs {
|
|
|
|
mvsu = append(mvsu, module.Version{Path: mv.Name, Version: mv.Version})
|
|
|
|
}
|
|
|
|
|
|
|
|
modules.Updates(modules.UpdateOptions{
|
|
|
|
Pre: false,
|
|
|
|
Major: false,
|
|
|
|
Cached: false,
|
|
|
|
Modules: mvsu,
|
|
|
|
OnUpdate: func(u modules.Update) {
|
|
|
|
if u.Err != nil {
|
2024-03-24 20:52:32 +03:00
|
|
|
logger.Error(ctx, "%s: failed: %v", u.Module.Path, u.Err)
|
2023-08-19 16:55:52 +03:00
|
|
|
} else {
|
|
|
|
mvs[u.Module.Path].Version = u.Version
|
|
|
|
}
|
|
|
|
},
|
2023-08-11 20:12:15 +03:00
|
|
|
})
|
|
|
|
|
2023-08-20 14:19:57 +03:00
|
|
|
if err := store.ModuleCreate(ctx, mods); err != nil {
|
2023-08-19 16:55:52 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-19 16:55:52 +03:00
|
|
|
func parseFile(file *object.File) ([]module.Version, error) {
|
2023-08-11 20:12:15 +03:00
|
|
|
r, err := file.Reader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
data, err := io.ReadAll(r)
|
2023-08-19 16:55:52 +03:00
|
|
|
r.Close()
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
modfile, err := modfile.ParseLax("go.mod", data, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
|
|
|
|
mods := make([]module.Version, 0, len(modfile.Require))
|
2023-08-11 20:12:15 +03:00
|
|
|
for _, req := range modfile.Require {
|
|
|
|
mods = append(mods, req.Mod)
|
|
|
|
}
|
2023-08-19 16:55:52 +03:00
|
|
|
|
|
|
|
sort.Slice(mods, func(i, j int) bool {
|
|
|
|
return mods[i].Path < mods[j].Path
|
|
|
|
})
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
return mods, nil
|
|
|
|
}
|