Прикинул метод Open и Delete, Update пока не до делал. Co-authored-by: Gorbunov Kirill Andreevich <kgorbunov@mtsbank.ru> Reviewed-on: #9 Co-authored-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru> Co-committed-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
This commit is contained in:
@@ -15,7 +15,9 @@ import (
|
||||
"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"
|
||||
@@ -26,8 +28,10 @@ import (
|
||||
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"
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
// https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
||||
@@ -37,12 +41,6 @@ var (
|
||||
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"`
|
||||
Branches []string `json:"branches" yaml:"branches"`
|
||||
}
|
||||
|
||||
var (
|
||||
configFiles = []string{
|
||||
"dependabot.yml",
|
||||
@@ -66,47 +64,55 @@ type Data struct {
|
||||
Modules map[string]modules.Update
|
||||
}
|
||||
|
||||
func main() {
|
||||
func pkgdashcli() {
|
||||
var err error
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3)); err != nil {
|
||||
logger.Errorf(ctx, "logger init error: %v", err)
|
||||
logger.DefaultLogger = slog.NewLogger()
|
||||
|
||||
if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3), logger.WithLevel(logger.DebugLevel)); err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("logger init error: %v", err))
|
||||
}
|
||||
|
||||
cfg := &Config{}
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to load config: %v", err)
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to load config: %v", err))
|
||||
}
|
||||
|
||||
for _, configDir := range configDirs {
|
||||
for _, configFile := range configFiles {
|
||||
logger.Info(ctx, fmt.Sprintf("path: %s", filepath.Join(configDir, configFile)))
|
||||
c := fileconfig.NewConfig(
|
||||
config.AllowFail(true),
|
||||
config.AllowFail(false),
|
||||
config.Struct(cfg),
|
||||
options.Codec(yamlcodec.NewCodec()),
|
||||
fileconfig.Path(filepath.Join(configDir, configFile)),
|
||||
fileconfig.Path(".gitea/pkgdashcli.yaml"),
|
||||
)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to load config: %v", err)
|
||||
logger.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))
|
||||
|
||||
if cfg.PullRequestBody == "" {
|
||||
cfg.PullRequestBody = DefaultPullRequestBody
|
||||
}
|
||||
@@ -136,15 +142,28 @@ func main() {
|
||||
mvs := make(map[string]modules.Update)
|
||||
|
||||
updateOptions := modules.UpdateOptions{
|
||||
Pre: false,
|
||||
Major: false,
|
||||
Cached: true,
|
||||
Pre: cfg.UpdateOpt.Pre,
|
||||
Major: cfg.UpdateOpt.Major,
|
||||
UpMajor: cfg.UpdateOpt.UpMajor,
|
||||
Cached: cfg.UpdateOpt.Cached,
|
||||
OnUpdate: func(u modules.Update) {
|
||||
var modpath string // new mod path with major
|
||||
if u.Err != nil {
|
||||
logger.Errorf(ctx, "%s: failed: %v", u.Module.Path, u.Err)
|
||||
logger.Error(ctx, fmt.Sprintf("%s: failed: %v", u.Module.Path, u.Err))
|
||||
return
|
||||
}
|
||||
mvs[u.Module.Path] = u
|
||||
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
|
||||
}
|
||||
}
|
||||
mvs[modpath] = u
|
||||
},
|
||||
}
|
||||
|
||||
@@ -154,21 +173,50 @@ func main() {
|
||||
|
||||
modules.Updates(updateOptions)
|
||||
|
||||
repoMgmt := getRepoMgmt()
|
||||
if repoMgmt == "unknown" {
|
||||
logger.Fatalf(ctx, "failed to get repo management")
|
||||
/*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" {
|
||||
logger.Fatal(ctx, "pkgdash/main failed to get repo management")
|
||||
}
|
||||
}
|
||||
|
||||
switch repoMgmt {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to create pr: %v", err)
|
||||
logger.Info(ctx, fmt.Sprintf("cfg: %v", cfg))
|
||||
|
||||
gitSource := source.NewSourceControl(*cfg)
|
||||
for _, branch := range cfg.Branches {
|
||||
for pathMod, mod := range mvs {
|
||||
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 {
|
||||
logger.Error(ctx, fmt.Sprintf("failed to create pr: %v", err))
|
||||
}
|
||||
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 {
|
||||
@@ -192,7 +240,7 @@ func getRepoMgmt() string {
|
||||
}
|
||||
}
|
||||
|
||||
func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[string]modules.Update) error {
|
||||
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")
|
||||
@@ -202,34 +250,34 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
|
||||
tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle)
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to parse template: %v", err)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to parse template: %v", err)
|
||||
logger.Fatal(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)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to fetch repo: %v", err)
|
||||
logger.Fatal(ctx, "failed to fetch repo: %v", err)
|
||||
}
|
||||
|
||||
var headRef *plumbing.Reference
|
||||
refIter, err := repo.Branches()
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to get branches: %v", err)
|
||||
logger.Fatal(ctx, "failed to get branches: %v", err)
|
||||
}
|
||||
for {
|
||||
ref, err := refIter.Next()
|
||||
@@ -244,14 +292,14 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
refIter.Close()
|
||||
|
||||
if headRef == nil {
|
||||
logger.Fatalf(ctx, "failed to get repo branch head")
|
||||
logger.Fatal(ctx, "failed to get repo branch head")
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "repo head %s", headRef)
|
||||
logger.Info(ctx, "repo head %s", headRef)
|
||||
|
||||
wtree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to get worktree: %v", err)
|
||||
logger.Fatal(ctx, "failed to get worktree: %v", err)
|
||||
}
|
||||
|
||||
type giteaPull struct {
|
||||
@@ -282,13 +330,13 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(buf, &pulls); err != nil {
|
||||
logger.Fatalf(ctx, "failed to decode response %s err: %v", buf, err)
|
||||
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 {
|
||||
logger.Infof(ctx, "skip %s as pr already exists %s", path, pull.URL)
|
||||
logger.Info(ctx, "skip %s as pr already exists %s", path, pull.URL)
|
||||
delete(mods, path)
|
||||
}
|
||||
}
|
||||
@@ -298,11 +346,11 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
wTitle.Reset()
|
||||
wBody.Reset()
|
||||
|
||||
logger.Infof(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)
|
||||
logger.Info(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)
|
||||
|
||||
logger.Infof(ctx, "reset worktree")
|
||||
logger.Info(ctx, "reset worktree")
|
||||
if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
|
||||
logger.Fatalf(ctx, "failed to reset repo branch: %v", err)
|
||||
logger.Fatal(ctx, "failed to reset repo branch: %v", err)
|
||||
}
|
||||
|
||||
if err = wtree.PullContext(ctx, &git.PullOptions{
|
||||
@@ -312,17 +360,17 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
Force: true,
|
||||
RemoteName: "origin",
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
logger.Fatalf(ctx, "failed to pull repo: %v", err)
|
||||
logger.Fatal(ctx, "failed to pull repo: %v", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "checkout ref %s", headRef)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to checkout tree: %v", err)
|
||||
logger.Fatal(ctx, "failed to checkout tree: %v", err)
|
||||
}
|
||||
|
||||
epath, err := exec.LookPath("go")
|
||||
@@ -330,7 +378,7 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to find go command: %v", err)
|
||||
logger.Fatal(ctx, "failed to find go command: %v", err)
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
@@ -338,25 +386,25 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
|
||||
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)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to run go mod tidy: %s err: %v", out, err)
|
||||
logger.Fatal(ctx, "failed to run go mod tidy: %s err: %v", out, err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "worktree add go.mod")
|
||||
logger.Info(ctx, "worktree add go.mod")
|
||||
if _, err = wtree.Add("go.mod"); err != nil {
|
||||
logger.Fatalf(ctx, "failed to add file: %v", err)
|
||||
logger.Fatal(ctx, "failed to add file: %v", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "worktree add go.sum")
|
||||
logger.Info(ctx, "worktree add go.sum")
|
||||
if _, err = wtree.Add("go.sum"); err != nil {
|
||||
logger.Fatalf(ctx, "failed to add file: %v", err)
|
||||
logger.Fatal(ctx, "failed to add file: %v", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "worktree commit")
|
||||
logger.Info(ctx, "worktree commit")
|
||||
_, err = wtree.Commit(wTitle.String(), &git.CommitOptions{
|
||||
Parents: []plumbing.Hash{headRef.Hash()},
|
||||
Author: &object.Signature{
|
||||
@@ -366,27 +414,27 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to commit: %v", err)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to create repo branch: %v", err)
|
||||
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))
|
||||
|
||||
logger.Infof(ctx, "try to push refspec %s", refspec)
|
||||
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 {
|
||||
logger.Fatalf(ctx, "failed to push repo branch: %v", err)
|
||||
logger.Fatal(ctx, "failed to push repo branch: %v", err)
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
@@ -396,10 +444,10 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
}
|
||||
|
||||
if err = tplTitle.Execute(wTitle, data); err != nil {
|
||||
logger.Fatalf(ctx, "failed to execute template: %v", err)
|
||||
logger.Fatal(ctx, "failed to execute template: %v", err)
|
||||
}
|
||||
if err = tplBody.Execute(wBody, data); err != nil {
|
||||
logger.Fatalf(ctx, "failed to execute template: %v", err)
|
||||
logger.Fatal(ctx, "failed to execute template: %v", err)
|
||||
}
|
||||
|
||||
body := map[string]string{
|
||||
@@ -408,14 +456,14 @@ func giteaPullRequest(ctx context.Context, cfg *Config, branch string, mods map[
|
||||
"head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version),
|
||||
"title": wTitle.String(),
|
||||
}
|
||||
logger.Infof(ctx, "raw body: %#+v", body)
|
||||
logger.Info(ctx, "raw body: %#+v", body)
|
||||
|
||||
buf, err = json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "marshal body: %s", buf)
|
||||
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 {
|
||||
|
Reference in New Issue
Block a user