pkgdash/cmd/pkgdashcli/main.go

488 lines
13 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
"git.unistack.org/unistack-org/pkgdash/internal/source"
"github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
envconfig "go.unistack.org/micro-config-env/v4"
fileconfig "go.unistack.org/micro-config-file/v4"
"go.unistack.org/micro/v4/config"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/logger/slog"
"go.unistack.org/micro/v4/options"
"golang.org/x/mod/modfile"
2024-03-17 15:41:25 +03:00
"golang.org/x/mod/semver"
)
// https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
var (
DefaultPullRequestTitle = `Bump {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
DefaultPullRequestBody = `Bumps {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
)
var (
configFiles = []string{
"dependabot.yml",
"pkgdashcli.yml",
"pkgdashcli.yaml",
}
configDirs = []string{
".gitea",
".github",
".gitlab",
}
repoMgmt = map[string]string{
".gitea": "gitea",
".gogs": "gogs",
".github": "github",
".gitlab": "gitlab",
}
)
type Data struct {
Modules map[string]modules.Update
}
2024-03-23 19:53:12 +03:00
func pkgdashcli() {
var err error
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger.DefaultLogger = slog.NewLogger()
if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3), logger.WithLevel(logger.DebugLevel)); err != nil {
2024-03-21 15:13:38 +03:00
logger.Error(ctx, fmt.Sprintf("logger init error: %v", err))
}
cfg := configcli.NewConfig()
if err = config.Load(ctx,
[]config.Config{
config.NewConfig(
config.Struct(cfg),
),
envconfig.NewConfig(
config.Struct(cfg),
),
},
config.LoadOverride(true),
); err != nil {
2024-03-21 15:13:38 +03:00
logger.Fatal(ctx, fmt.Sprintf("failed to load config: %v", err))
}
for _, configDir := range configDirs {
for _, configFile := range configFiles {
2024-03-23 18:52:13 +03:00
logger.Info(ctx, fmt.Sprintf("path: %s", filepath.Join(configDir, configFile)))
c := fileconfig.NewConfig(
2024-03-23 18:52:13 +03:00
config.AllowFail(false),
config.Struct(cfg),
options.Codec(yamlcodec.NewCodec()),
2024-03-23 18:52:13 +03:00
fileconfig.Path(".gitea/pkgdashcli.yaml"),
)
2024-03-23 18:52:13 +03:00
err = c.Init(options.Context(ctx))
if err != nil {
logger.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
}
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
2024-03-23 18:52:13 +03:00
logger.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
}
}
}
2024-03-23 18:52:13 +03:00
logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))
if cfg.PullRequestBody == "" {
cfg.PullRequestBody = DefaultPullRequestBody
}
if cfg.PullRequestTitle == "" {
cfg.PullRequestTitle = DefaultPullRequestTitle
}
path := "."
if len(os.Args) > 1 {
path = os.Args[1]
}
name, err := modules.FindModFile(path)
if err != nil {
panic(err)
}
buf, err := os.ReadFile(name)
if err != nil {
panic(err)
}
mfile, err := modfile.Parse(name, buf, nil)
if err != nil {
panic(err)
}
mvs := make(map[string]modules.Update)
updateOptions := modules.UpdateOptions{
Pre: cfg.UpdateOpt.Pre,
Major: cfg.UpdateOpt.Major,
UpMajor: cfg.UpdateOpt.UpMajor,
Cached: cfg.UpdateOpt.Cached,
OnUpdate: func(u modules.Update) {
2024-03-17 15:41:25 +03:00
var modpath string // new mod path with major
if u.Err != nil {
2024-03-21 15:29:36 +03:00
logger.Error(ctx, fmt.Sprintf("%s: failed: %v", u.Module.Path, u.Err))
return
}
2024-03-17 15:41:25 +03:00
modpath = u.Module.Path
v := semver.Major(u.Version)
p := modules.ModPrefix(modpath)
if !strings.HasPrefix(u.Module.Version, v) && v != "v1" && v != "v0" {
switch strings.HasPrefix(u.Module.Path, "gopkg.in") {
case true:
modpath = p + "." + v
case false:
modpath = p + "/" + v
}
2024-03-16 01:56:36 +03:00
}
2024-03-17 15:41:25 +03:00
mvs[modpath] = u
},
}
for _, req := range mfile.Require {
updateOptions.Modules = append(updateOptions.Modules, req.Mod)
}
modules.Updates(updateOptions)
/*var repoGit, tokenGit string // nameGit = gitea, repoGit = pkgdash, tokenGit = {xxx}, machine??
if cfg.Source != nil {
repoGit = cfg.Source.TypeGit
tokenGit = cfg.Source.Token
} else {
repoGit = getRepoMgmt()
if repoGit == "unknown" {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "pkgdash/main failed to get repo management")
}
}
usr, err := user.Current()
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Error(ctx, "pkgdash/main can t get info user: %s", err)
} else {
n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Error(ctx, "pkgdash/main can t parse .netrc: %s", err)
}
tokenGit = n.Machine(repoGit).Get("password")
}
switch repoGit {
case "gitea":
for _, branch := range cfg.Branches {
err = giteaPullRequest(ctx, cfg, branch, mvs)
}
}*/
2024-03-21 15:13:38 +03:00
logger.Info(ctx, fmt.Sprintf("cfg: %v", cfg))
gitSource := source.NewSourceControl(*cfg)
for _, branch := range cfg.Branches {
for pathMod, mod := range mvs {
2024-03-21 15:29:36 +03:00
logger.Debugf(ctx, fmt.Sprintf("Start update %s from %s to %s", pathMod, mod.Module.Version, mod.Version))
err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
if err != nil {
2024-03-21 15:13:38 +03:00
logger.Error(ctx, fmt.Sprintf("failed to create pr: %v", err))
}
2024-03-21 15:29:36 +03:00
logger.Debugf(ctx, fmt.Sprintf("Update successful for %s", pathMod))
}
}
//err = gitSource.RequestClose(ctx, "master", "modernc.org/ccgo/v4")
logger.Info(ctx, "Pkgdash successfully updated dependencies")
}
func getRepoMgmt() string {
wd, err := os.Getwd()
if err != nil {
return "unknown"
}
p := filepath.Clean(wd)
for {
for _, configDir := range configDirs {
_, err := os.Stat(filepath.Join(p, configDir))
if name, ok := repoMgmt[configDir]; ok && err == nil {
return name
}
}
if p == "/" {
return "unknown"
}
p = filepath.Clean(filepath.Join(p, ".."))
}
}
func giteaPullRequest(ctx context.Context, cfg *configcli.Config, branch string, mods map[string]modules.Update) error {
envAPIURL := os.Getenv("GITHUB_API_URL")
envREPOSITORY := os.Getenv("GITHUB_REPOSITORY")
envTOKEN := os.Getenv("GITHUB_TOKEN")
var buf []byte
var err error
tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle)
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to parse template: %v", err)
}
wTitle := bytes.NewBuffer(nil)
tplBody, err := template.New("pull_request_body").Parse(cfg.PullRequestBody)
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to parse template: %v", err)
}
wBody := bytes.NewBuffer(nil)
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to open repo: %v", err)
}
if err = repo.FetchContext(ctx, &git.FetchOptions{
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to fetch repo: %v", err)
}
var headRef *plumbing.Reference
refIter, err := repo.Branches()
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to get branches: %v", err)
}
for {
ref, err := refIter.Next()
if err != nil {
break
}
if ref.Name().String() == branch {
headRef = ref
break
}
}
refIter.Close()
if headRef == nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to get repo branch head")
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "repo head %s", headRef)
wtree, err := repo.Worktree()
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to get worktree: %v", err)
}
type giteaPull struct {
URL string `json:"url"`
Title string `json:"title"`
Base struct {
Ref string `json:"ref"`
} `json:"base"`
ID int64 `json:"id"`
}
var pulls []*giteaPull
req, err := http.NewRequestWithContext(ctx, http.MethodGet, envAPIURL+"/repos/"+envREPOSITORY+"/pulls?state=open&token="+envTOKEN, nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
buf, _ = io.ReadAll(rsp.Body)
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unknown error: %s", buf)
}
if err = json.Unmarshal(buf, &pulls); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
}
for path := range mods {
for _, pull := range pulls {
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "skip %s as pr already exists %s", path, pull.URL)
delete(mods, path)
}
}
}
for path, mod := range mods {
wTitle.Reset()
wBody.Reset()
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "reset worktree")
if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to reset repo branch: %v", err)
}
if err = wtree.PullContext(ctx, &git.PullOptions{
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
Depth: 1,
// RemoteURL :
Force: true,
RemoteName: "origin",
}); err != nil && err != git.NoErrAlreadyUpToDate {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to pull repo: %v", err)
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "checkout ref %s", headRef)
if err = wtree.Checkout(&git.CheckoutOptions{
Hash: headRef.Hash(),
Branch: plumbing.NewBranchReferenceName(fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version)),
Create: true,
Force: true,
}); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to checkout tree: %v", err)
}
epath, err := exec.LookPath("go")
if errors.Is(err, exec.ErrDot) {
err = nil
}
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to find go command: %v", err)
}
var cmd *exec.Cmd
var out []byte
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
if out, err = cmd.CombinedOutput(); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to run go mod edit: %s err: %v", out, err)
}
cmd = exec.CommandContext(ctx, epath, "mod", "tidy")
if out, err = cmd.CombinedOutput(); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to run go mod tidy: %s err: %v", out, err)
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "worktree add go.mod")
if _, err = wtree.Add("go.mod"); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to add file: %v", err)
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "worktree add go.sum")
if _, err = wtree.Add("go.sum"); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to add file: %v", err)
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "worktree commit")
_, err = wtree.Commit(wTitle.String(), &git.CommitOptions{
Parents: []plumbing.Hash{headRef.Hash()},
Author: &object.Signature{
Name: "gitea-actions",
Email: "info@unistack.org",
When: time.Now(),
},
})
if err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to commit: %v", err)
}
// newref := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/pkgdash/go_modules/%s-%s", path, mod.Version)), headRef.Hash())
/*
if err = repo.Storer.SetReference(newref); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to create repo branch: %v", err)
}
*/
refspec := gitconfig.RefSpec(fmt.Sprintf("+refs/heads/pkgdash/go_modules/%s-%s:refs/heads/pkgdash/go_modules/%s-%s", path, mod.Version, path, mod.Version))
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "try to push refspec %s", refspec)
if err = repo.PushContext(ctx, &git.PushOptions{
RefSpecs: []gitconfig.RefSpec{refspec},
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
Force: true,
}); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to push repo branch: %v", err)
}
data := map[string]string{
"Name": path,
"VersionOld": mod.Module.Version,
"VersionNew": mod.Version,
}
if err = tplTitle.Execute(wTitle, data); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to execute template: %v", err)
}
if err = tplBody.Execute(wBody, data); err != nil {
2024-03-17 16:45:46 +03:00
logger.Fatal(ctx, "failed to execute template: %v", err)
}
body := map[string]string{
"base": branch,
"body": wBody.String(),
"head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version),
"title": wTitle.String(),
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "raw body: %#+v", body)
buf, err = json.Marshal(body)
if err != nil {
return err
}
2024-03-17 16:45:46 +03:00
logger.Info(ctx, "marshal body: %s", buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, envAPIURL+"/repos/"+envREPOSITORY+"/pulls?token="+envTOKEN, bytes.NewReader(buf))
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if rsp.StatusCode != http.StatusCreated {
buf, _ = io.ReadAll(rsp.Body)
return fmt.Errorf("unknown error: %s", buf)
}
}
return nil
}