package main import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "net/http" "os" "os/exec" "path/filepath" "text/template" "time" "git.unistack.org/unistack-org/pkgdash/internal/modules" "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/options" "golang.org/x/mod/modfile" ) // 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}}` ) type Config struct { PullRequestTitle string `json:"pull_request_title" yaml:"pull_request_title"` PullRequestBody string `json:"pull_request_body" yaml:"pull_request_body"` } var ( configFiles = []string{ "dependabot.yml", "pkgdash.yml", "pkgdash.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 } func main() { var err error ctx, cancel := context.WithCancel(context.Background()) defer cancel() cfg := &Config{} if err = config.Load(ctx, []config.Config{ config.NewConfig( config.Struct(cfg), ), envconfig.NewConfig( config.Struct(cfg), ), }, config.LoadOverride(true), ); err != nil { logger.Fatalf(ctx, "failed to load config: %v", err) } for _, configDir := range configDirs { for _, configFile := range configFiles { c := fileconfig.NewConfig( config.AllowFail(true), config.Struct(cfg), options.Codec(yamlcodec.NewCodec()), fileconfig.Path(filepath.Join(configDir, configFile)), ) if err = c.Load(ctx, config.LoadOverride(true)); err != nil { logger.Fatalf(ctx, "failed to load config: %v", err) } } } 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: false, Major: false, Cached: true, OnUpdate: func(u modules.Update) { if u.Err != nil { logger.Errorf(ctx, "%s: failed: %v", u.Module.Path, u.Err) return } mvs[u.Module.Path] = u }, } for _, req := range mfile.Require { updateOptions.Modules = append(updateOptions.Modules, req.Mod) } modules.Updates(updateOptions) repoMgmt := getRepoMgmt() if repoMgmt == "unknown" { logger.Fatalf(ctx, "failed to get repo management") } switch repoMgmt { case "gitea": err = giteaPullRequest(ctx, cfg, mvs) } if err != nil { logger.Fatalf(ctx, "failed to create pr: %v", err) } } 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 *Config, mods map[string]modules.Update) error { envAPIURL := os.Getenv("GITHUB_API_URL") envREPOSITORY := os.Getenv("GITHUB_REPOSITORY") envTOKEN := os.Getenv("GITHUB_TOKEN") envBaseBranch := os.Getenv("GITHUB_REF_NAME") var buf []byte var err error tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle) if err != nil { logger.Fatalf(ctx, "failed to parse template: %v", err) } wTitle := bytes.NewBuffer(nil) tplBody, err := template.New("pull_request_body").Parse(cfg.PullRequestBody) if err != nil { logger.Fatalf(ctx, "failed to parse template: %v", err) } wBody := bytes.NewBuffer(nil) repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) if err != nil { logger.Fatalf(ctx, "failed to open repo: %v", err) } headRef, err := repo.Head() if err != nil { logger.Fatalf(ctx, "failed to get repo head: %v", err) } wtree, err := repo.Worktree() if err != nil { logger.Fatalf(ctx, "failed to get worktree: %v", err) } for path, mod := range mods { wTitle.Reset() wBody.Reset() logger.Infof(ctx, "update %s from %s to %s\n", path, mod.Module.Version, mod.Version) 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 { logger.Fatalf(ctx, "failed to create repo branch: %v", err) } logger.Infof(ctx, "reset worktree") if err = wtree.Reset(&git.ResetOptions{}); err != nil { logger.Fatalf(ctx, "failed to reset repo branch: %v", err) } logger.Infof(ctx, "checkout ref %s", headRef) if err = wtree.Checkout(&git.CheckoutOptions{ Hash: headRef.Hash(), Create: false, Force: true, }); err != nil { logger.Fatalf(ctx, "failed to get checkout tree: %v", err) } epath, err := exec.LookPath("go") if errors.Is(err, exec.ErrDot) { err = nil } if err != nil { logger.Fatalf(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 { logger.Fatalf(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 { logger.Fatalf(ctx, "failed to run go mod tidy: %s err: %v", out, err) } logger.Infof(ctx, "worktree add go.mod") if _, err = wtree.Add("go.mod"); err != nil { logger.Fatalf(ctx, "failed to add file: %v", err) } logger.Infof(ctx, "worktree add go.sum") if _, err = wtree.Add("go.sum"); err != nil { logger.Fatalf(ctx, "failed to add file: %v", err) } logger.Infof(ctx, "worktree commit") commit, err := wtree.Commit(wTitle.String(), &git.CommitOptions{ Author: &object.Signature{ Name: "gitea-actions ", Email: "info@unistack.org", When: time.Now(), }, }) if err != nil { logger.Fatalf(ctx, "failed to commit: %v", err) } refspec := gitconfig.RefSpec(commit.String() + ":" + newref.Name().String()) logger.Infof(ctx, "try to push %s", refspec) if err = repo.Push(&git.PushOptions{ RefSpecs: []gitconfig.RefSpec{refspec}, Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN}, Force: true, }); err != nil { logger.Fatalf(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 { logger.Fatalf(ctx, "failed to execute template: %v", err) } if err = tplBody.Execute(wBody, data); err != nil { logger.Fatalf(ctx, "failed to execute template: %v", err) } body := map[string]string{ "base": envBaseBranch, "body": wBody.String(), "head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version), "title": wTitle.String(), } logger.Infof(ctx, "raw body: %#+v", body) buf, err = json.Marshal(body) if err != nil { return err } logger.Infof(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 }