diff --git a/cmd/pkgdashcli/main.go b/cmd/pkgdashcli/main.go index 6303bf2..f9c062f 100644 --- a/cmd/pkgdashcli/main.go +++ b/cmd/pkgdashcli/main.go @@ -11,6 +11,8 @@ import ( "text/template" "git.unistack.org/unistack-org/pkgdash/internal/modules" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" yamlcodec "go.unistack.org/micro-codec-yaml/v4" envconfig "go.unistack.org/micro-config-env/v4" fileconfig "go.unistack.org/micro-config-file/v4" @@ -18,7 +20,6 @@ import ( "go.unistack.org/micro/v4/logger" "go.unistack.org/micro/v4/options" "golang.org/x/mod/modfile" - "golang.org/x/mod/module" ) // https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file @@ -53,7 +54,7 @@ var ( ) type Data struct { - Modules []module.Version + Modules map[string]modules.Update } func main() { @@ -132,10 +133,6 @@ func main() { modules.Updates(updateOptions) - for path, u := range mvs { - fmt.Printf("%s from %s to %s\n", path, u.Module.Version, u.Version) - } - repoMgmt := getRepoMgmt() if repoMgmt == "unknown" { logger.Fatalf(ctx, "failed to get repo management") @@ -143,7 +140,7 @@ func main() { switch repoMgmt { case "gitea": - err = giteaPullRequest(ctx, mvs) + err = giteaPullRequest(ctx, cfg, mvs) } if err != nil { @@ -152,29 +149,34 @@ func main() { } func getRepoMgmt() string { - for _, configDir := range configDirs { - _, err := os.Stat(configDir) - if err != nil { - continue - } - if name, ok := repoMgmt[configDir]; ok { - return name - } + wd, err := os.Getwd() + if err != nil { + return "unknown" } - 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 []module.Version) error { +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 - data := &Data{Modules: mods} - tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle) if err != nil { logger.Fatalf(ctx, "failed to parse template: %v", err) @@ -182,10 +184,6 @@ func giteaPullRequest(ctx context.Context, cfg *Config, mods []module.Version) e wTitle := bytes.NewBuffer(nil) - if err = tplTitle.Execute(wTitle, data); err != nil { - logger.Fatalf(ctx, "failed to execute template: %v", err) - } - tplBody, err := template.New("pull_request_title").Parse(cfg.PullRequestBody) if err != nil { logger.Fatalf(ctx, "failed to parse template: %v", err) @@ -193,18 +191,45 @@ func giteaPullRequest(ctx context.Context, cfg *Config, mods []module.Version) e wBody := bytes.NewBuffer(nil) - if err = tplBody.Execute(wBody, data); err != nil { - logger.Fatalf(ctx, "failed to execute template: %v", err) + 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) } - for _, mod := range mods { - body := map[string]string{ - "base": "", - "body": "", - "head": "", - "title": "", + for path, mod := range mods { + wTitle.Reset() + wBody.Reset() + + fmt.Printf("%s from %s to %s\n", path, mod.Module.Version, mod.Version) + ref := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/pkgdash/go_modules/%s-%s", path, mod.Version)), headRef.Hash()) + if err = repo.Storer.SetReference(ref); err != nil { + logger.Fatalf(ctx, "failed to create repo branch: %v", err) } - buf, err := json.Marshal(body) + + 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(), + } + buf, err = json.Marshal(body) if err != nil { return err } @@ -233,6 +258,7 @@ func giteaPullRequest(ctx context.Context, cfg *Config, mods []module.Version) e if rsp.StatusCode != http.StatusOK { return fmt.Errorf("unknown error") } + } return nil