#8 delete cobra, add micro-config-flag (#11)

Убрана кобра, доработаны методы создания, закрытия пулл реквестов.

Co-authored-by: Gorbunov Kirill Andreevich <kgorbunov@mtsbank.ru>
Reviewed-on: #11
Co-authored-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
Co-committed-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
This commit is contained in:
Кирилл Горбунов
2024-04-02 22:54:15 +03:00
parent f913c01fca
commit eb2a21a1b1
24 changed files with 1337 additions and 1749 deletions

View File

@@ -1,94 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"context"
"fmt"
"os"
"strings"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
"github.com/spf13/cobra"
"go.unistack.org/micro/v4/logger"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
// checkupdateCmd represents the checkupdate command
var checkupdateCmd = NewCheckUpdateCommand()
var mvs = make(map[string]modules.Update)
func init() {
rootCmd.AddCommand(checkupdateCmd)
}
func NewCheckUpdateCommand() *cobra.Command {
ctx := context.Background()
cmd := &cobra.Command{
Use: "checkupdate",
Short: "CheckUpdate collects a list of dependencies with the latest updates.",
Long: `CheckUpdate collects a list of dependencies with the latest updates.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("checkupdate called")
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) {
var modpath string // new mod path with major
if u.Err != nil {
logger.Error(ctx, fmt.Sprintf("%s: failed: %v", u.Module.Path, u.Err))
return
}
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
},
}
for _, req := range mfile.Require {
updateOptions.Modules = append(updateOptions.Modules, req.Mod)
}
modules.Updates(updateOptions)
logger.Info(ctx, fmt.Sprintf("Modules get update: /n %s", mvs))
return nil
},
}
return cmd
}

View File

@@ -1,74 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
var deleteCmd = NewDeleteCommand()
type DeleteFlags struct {
all bool
mod string
}
func init() {
rootCmd.AddCommand(deleteCmd)
}
func NewDeleteCommand() *cobra.Command {
var flags DeleteFlags
ctx := context.Background()
cmd := &cobra.Command{
Use: "delete",
Short: "Delete closes the merge requests created with a dependency update.",
Long: `Delete closes the merge requests created with a dependency update.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("delete called")
var err error
if gitsource == nil {
return errSourceNil
}
if len(prList) == 0 {
return errPRNotExist
}
if flags.all {
for _, branch := range cfg.Branches {
rMap := prList[branch]
for path, _ := range rMap {
err = gitsource.RequestClose(ctx, branch, path)
if err != nil {
return err
}
}
}
}
if flags.mod != "" {
for _, branch := range cfg.Branches {
err = gitsource.RequestClose(ctx, branch, flags.mod)
if err != nil {
return err
}
}
}
return errFlagsNotExist
},
}
cmd.Flags().BoolVarP(&flags.all, "all", "a", false, "Deletes everything depending")
cmd.Flags().StringVarP(&flags.mod, "mod", "m", "", "Deletes one dependency")
return cmd
}

View File

@@ -1,121 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"context"
"fmt"
"path/filepath"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/source"
"github.com/spf13/cobra"
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"
)
// initCmd represents the init command
var initCmd = NewInitCommand()
var gitsource = source.SourceControl(nil)
var cfg = configcli.NewConfig()
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",
}
)
func init() {
rootCmd.AddCommand(initCmd)
}
func NewInitCommand() *cobra.Command {
ctx := context.Background()
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))
}
cmd := &cobra.Command{
Use: "init",
Short: "Init fills the config with data from the configuration file.",
Long: `Init fills the config with data from the configuration file.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("init called")
var err error
if err = config.Load(ctx,
[]config.Config{
config.NewConfig(
config.Struct(cfg),
),
envconfig.NewConfig(
config.Struct(cfg),
),
},
config.LoadOverride(true),
); err != nil {
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(false),
config.Struct(cfg),
options.Codec(yamlcodec.NewCodec()),
fileconfig.Path(".gitea/pkgdashcli.yaml"),
)
err = c.Init()
if err != nil {
logger.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
return err
}
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
return err
}
}
}
logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))
if cfg.PullRequestBody == "" {
cfg.PullRequestBody = DefaultPullRequestBody
}
if cfg.PullRequestTitle == "" {
cfg.PullRequestTitle = DefaultPullRequestTitle
}
gitsource = source.NewSourceControl(*cfg)
return nil
},
}
return cmd
}

View File

@@ -1,73 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"go.unistack.org/micro/v4/logger"
)
// updateCmd represents the update command
var listCmd = NewListCommand()
var prList = map[string]map[string]string{}
/* [
"master":
[
"go.unistack.org/micro/v4" : "Bump go.unistack.org/micro/v4 from v4.0.0 to v4.0.1",
"go.unistack.org/micro-client-http/v4":"Bump go.unistack.org/micro-client-http/v4 from v4.0.0 to v4.0.2",
],
"v3":
[
"go.unistack.org/micro/v3" : "Bump go.unistack.org/micro/v4 from v3.0.0 to v3.0.1",
],
] */
func init() {
rootCmd.AddCommand(listCmd)
}
func NewListCommand() *cobra.Command {
ctx := context.Background()
logger.Info(ctx, "RequestList start")
cmd := &cobra.Command{
Use: "list",
Short: "Update allows you to start the process of creating a PR in the repository",
Long: `
Update allows you to start the process of creating a PR in the repository.
Use the -a flag to update all dependencies or -m flag a specific module.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if gitsource == nil {
return errSourceNil
}
if cfg == nil {
return errCfgNil
}
prList = make(map[string]map[string]string)
for _, branch := range cfg.Branches {
rMap, err := gitsource.RequestList(ctx, branch)
if err != nil {
return err
}
prList[branch] = rMap
logger.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap))
}
return nil
},
}
return cmd
}

View File

@@ -1,42 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
errUpdateNotExist = fmt.Errorf("mod not in list of update. Call init")
errFlagsNotExist = fmt.Errorf("empty flags")
errSourceNil = fmt.Errorf("source nil. Call init")
errCfgNil = fmt.Errorf("cfg nil. Call init")
errPRNotExist = fmt.Errorf("pr not exist. Call list")
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "pkgdashcli <command> [flags]",
Short: "Pkgdashcli application to update dependencies.",
Long: `Pkgdashcli allows you to define a version update for a dependency and start merge requests in version control systems.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
}

View File

@@ -1,95 +0,0 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"go.unistack.org/micro/v4/logger"
)
var updateCmd = NewUpdateCommand()
type UpdateFlags struct {
all bool
mod string
}
func init() {
rootCmd.AddCommand(updateCmd)
}
func NewUpdateCommand() *cobra.Command {
var flags UpdateFlags
ctx := context.Background()
cmd := &cobra.Command{
Use: "update",
Short: "Update allows you to start the process of creating a PR in the repository",
Long: `
Update allows you to start the process of creating a PR in the repository.
Use the -a flag to update all dependencies or -m flag a specific module.
`,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if gitsource == nil {
return errSourceNil
}
if len(mvs) == 0 {
return errUpdateNotExist
}
if flags.all {
logger.Info(ctx, "update all")
for pathMod, m := range mvs {
for _, branch := range cfg.Branches {
logger.Info(ctx, fmt.Sprintf("update mod: %s", m))
if err = gitsource.RequestOpen(ctx, branch, pathMod, m); err != nil {
logger.Error(ctx, fmt.Sprintf("PR open error: %s, base branch: %s", pathMod, branch))
return err
}
logger.Info(ctx, fmt.Sprintf("Successful update mod: %s", pathMod))
}
delete(mvs, pathMod) // todo возможно чисть не стоит и нужно просто пропускать
}
return nil
}
if flags.mod != "" {
for _, branch := range cfg.Branches {
logger.Info(ctx, fmt.Sprintf("update mod: %s", flags.mod))
if _, ok := mvs[flags.mod]; !ok {
return errUpdateNotExist
}
if err = gitsource.RequestOpen(ctx, branch, flags.mod, mvs[flags.mod]); err != nil {
logger.Error(ctx, fmt.Sprintf("PR open error: %s, base branch: %s", flags.mod, branch))
return err
}
logger.Info(ctx, fmt.Sprintf("Successful update mod: %s", flags.mod))
}
delete(mvs, flags.mod)
return nil
}
return errFlagsNotExist
},
}
cmd.Flags().BoolVarP(&flags.all, "all", "a", false, "Updates everything depending")
cmd.Flags().StringVarP(&flags.mod, "mod", "m", "", "Update one dependency")
return cmd
}

View File

@@ -9,11 +9,11 @@ type Config struct {
}
type Source struct {
TypeGit string `json:"type" yaml:"type"`
Token string `json:"token" yaml:"token"`
APIURL string `json:"apiurl" yaml:"apiurl"`
Repository string `json:"repository" yaml:"repository"`
Owner string `json:"owner" yaml:"owner"`
TypeGit string `json:"type" yaml:"type" env:"GIT_TYPE"`
Token string `json:"token" yaml:"token" env:"GIT_TOKEN"`
APIURL string `json:"apiurl" yaml:"apiurl" env:"GIT_API"`
Repository string `json:"repository" yaml:"repository" env:"GIT_REPO"`
Owner string `json:"owner" yaml:"owner" env:"GIT_OWNER"`
}
type UpdateOpt struct {
@@ -23,8 +23,14 @@ type UpdateOpt struct {
Cached bool `json:"cached" yaml:"cached" default:"true"`
}
type Cli struct {
Command string `flag:"name=command,desc='choice command(update, close, checkupdaue, list)',default=''"`
Path string `flag:"name=path,desc='title of mod',default=''"`
}
func NewConfig() *Config {
return &Config{
Source: &Source{},
Source: &Source{},
UpdateOpt: &UpdateOpt{},
}
}

View File

@@ -80,7 +80,7 @@ func ParseDSN(cfg *appconfig.DatabaseConfig) error {
case "sqlite", "sqlite3":
u.Scheme = "sqlite"
default:
return fmt.Errorf("unknown database %s", u.Scheme)
return fmt.Error("unknown database %s", u.Scheme)
}
cfg.Type = u.Scheme
@@ -104,7 +104,7 @@ func connect(ctx context.Context, cfg *appconfig.DatabaseConfig, log logger.Logg
db, err = connectSqlite(ctx, cfg.ConnStr)
cfg.Type = "sqlite"
default:
return nil, fmt.Errorf("unknown database type %s", cfg.Type)
return nil, fmt.Error("unknown database type %s", cfg.Type)
}
if err != nil {

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
@@ -34,12 +33,13 @@ type Gitea struct {
PRBody string
Repository string
Owner string
pulls []*giteaPull
}
func NewGitea(cfg configcli.Config) *Gitea {
return &Gitea{
URL: cfg.Source.APIURL,
Token: os.Getenv("gitea_token"),
Token: cfg.Source.Token,
PRTitle: cfg.PullRequestTitle,
PRBody: cfg.PullRequestBody,
Repository: cfg.Source.Repository,
@@ -59,8 +59,12 @@ type giteaPull struct {
ID int64 `json:"id"`
}
func (g *Gitea) Name() string {
return "gitea"
}
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
logger.Debug(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
logger.Debugf(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
var buf []byte
var err error
@@ -97,16 +101,16 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
}
// извлекаем ссылки с объектами из удаленного объекта??
//извлекаем ссылки с объектами из удаленного объекта??
if err = repo.FetchContext(ctx, &git.FetchOptions{
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate {
logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo: %v", err))
} // обновляем репозиторий
} //обновляем репозиторий
var headRef *plumbing.Reference // вроде ссылка на гит
refIter, err := repo.Branches() // получение веток
refIter, err := repo.Branches() //получение веток
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
return err
@@ -116,11 +120,11 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
if err != nil {
break
}
if strings.Contains(ref.Name().String(), branch) { // todo вот тут возможно нужно переделать
if ref.Name().Short() == branch { //todo вот тут возможно нужно переделать
headRef = ref
break
}
} // перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
} //перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
refIter.Close()
if headRef == nil {
@@ -130,19 +134,18 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
logger.Info(ctx, fmt.Sprintf("repo head %s", headRef))
wtree, err := repo.Worktree() // todo вроде рабочее дерево не нужно
wtree, err := repo.Worktree() //todo вроде рабочее дерево не нужно
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
}
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
if err != nil && err != ErrPRNotExist {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
}
for _, pull := range pulls {
logger.Debug(ctx, fmt.Sprintf("PULL title - %s | ref - %s", pull.Title, pull.Base.Ref))
for _, pull := range g.pulls {
if strings.Contains(pull.Title, path) && strings.Contains(pull.Base.Ref, branch) {
logger.Info(ctx, fmt.Sprintf("PR for %s exists %s, call RequestUpdate", path, pull.URL))
return g.RequestUpdate(ctx, branch, path, mod)
@@ -154,7 +157,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
logger.Info(ctx, "reset worktree")
if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to reset repo branch: %v", err))
} // вроде меняем ветку todo вроде можно удалить
} //вроде меняем ветку todo вроде можно удалить
if err = wtree.PullContext(ctx, &git.PullOptions{
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
@@ -163,7 +166,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
Force: true,
RemoteName: "origin",
}); err != nil && err != git.NoErrAlreadyUpToDate {
logger.Error(ctx, fmt.Sprintf("failed to pull repo: %v", err)) // подтягиваем изменения с удаленого репозитория
logger.Error(ctx, fmt.Sprintf("failed to pull repo: %v", err)) //подтягиваем изменения с удаленого репозитория
}
logger.Info(ctx, fmt.Sprintf("checkout ref %s", headRef))
@@ -175,7 +178,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
}); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to checkout tree: %v", err))
return err
} // создаем новую ветку
} //создаем новую ветку
epath, err := exec.LookPath("go")
if errors.Is(err, exec.ErrDot) {
@@ -226,7 +229,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
logger.Fatal(ctx, fmt.Sprintf("failed to commit: %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)) // todo как будто нужно переделать
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)) //todo как будто нужно переделать
logger.Info(ctx, fmt.Sprintf("try to push refspec %s", refspec))
@@ -256,7 +259,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
bytes.NewReader(buf),
)
if err != nil {
@@ -268,7 +271,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
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)
@@ -276,11 +279,16 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
logger.Info(ctx, fmt.Sprintf("PR create for %s-%s", path, mod.Version))
repo, err = git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
}
return nil
}
func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error {
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
logger.Debugf(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
if err != nil {
@@ -309,7 +317,7 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
logger.Error(ctx, fmt.Sprintf("failed to do request for delete the branch: %s, err: %s, code: %s", branch, err, rsp.StatusCode))
logger.Error(ctx, fmt.Sprintf("failed to do request for delete the branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
return err
}
@@ -318,19 +326,22 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
}
func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
logger.Debugf(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
var err error
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
}
}
prExist := false
for _, pull := range pulls {
for _, pull := range g.pulls {
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
logger.Info(ctx, fmt.Sprintf("don't skip %s since pr exist %s", path, pull.URL)) // todo
tVersion := getVersions(pull.Head.Ref) // Надо взять просто из названия ветки последнюю версию
logger.Info(ctx, fmt.Sprintf("don't skip %s since pr exist %s", path, pull.URL)) //todo
tVersion := getVersions(pull.Head.Ref) //Надо взять просто из названия ветки последнюю версию
if modules.IsNewerVersion(tVersion, mod.Version, false) {
reqDel, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, pull.Head.Ref, g.Token)
if err != nil {
@@ -340,10 +351,11 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
rsp, err := http.DefaultClient.Do(reqDel)
if err != nil {
logger.Error(ctx, fmt.Sprintf("Error with do request for branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
continue // думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации
continue //думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации
}
logger.Info(ctx, fmt.Sprintf("Old pr %s successful delete", pull.Head.Ref))
} else {
logger.Debugf(ctx, "The existing PR is relevant")
return nil
}
prExist = true
@@ -360,7 +372,7 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
}
func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]string, error) {
logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
logger.Debugf(ctx, fmt.Sprintf("RequestList for %s", branch))
gPulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
if err != nil {
@@ -371,7 +383,10 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
rMap := make(map[string]string)
for _, pull := range gPulls {
path = strings.Split(pull.Title, " ")[1] // todo Работет только для дефолтного шаблона
if !strings.HasPrefix(pull.Title, "Bump ") { //добавляем только реквесты бота по обновлению модулей
continue
}
path = strings.Split(pull.Title, " ")[1] //todo Работет только для дефолтного шаблона
rMap[path] = pull.Title
}
@@ -388,7 +403,7 @@ func getVersions(s string) string {
func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) {
var buf []byte
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/api/v1/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
if err != nil {
return nil, err
}
@@ -398,39 +413,48 @@ func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (
}
func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull, error) {
var pulls []*giteaPull
var err error
var pullsAll, pulls []*giteaPull
page := 1
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s/repos/%s/%s/pulls?state=open&token=%s", url, owner, repo, token),
nil)
if err != nil {
return nil, err
} // вроде запроса к репозиторию
for {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("https://%s/api/v1//repos/%s/%s/pulls?state=open&page=%v&token=%s", url, owner, repo, page, token),
nil)
if err != nil {
return nil, err
} //вроде запроса к репозиторию
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil {
return nil, err
}
buf, _ := io.ReadAll(rsp.Body)
switch rsp.StatusCode {
case http.StatusOK:
if err = json.Unmarshal(buf, &pulls); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil {
return nil, err
}
return pulls, nil
case http.StatusNotFound:
logger.Info(ctx, "PL is not exist for %s", repo)
return nil, ErrPRNotExist
default:
return nil, fmt.Errorf("unknown error: %s", buf)
buf, _ := io.ReadAll(rsp.Body)
switch rsp.StatusCode {
case http.StatusOK:
if err = json.Unmarshal(buf, &pulls); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
return nil, err
}
pullsAll = append(pullsAll, pulls...)
page++
case http.StatusNotFound:
logger.Info(ctx, "PL is not exist for %s", repo)
return nil, ErrPRNotExist
default:
return nil, fmt.Errorf("unknown error: %s", buf)
}
if len(pulls) == 0 {
break
}
}
return pullsAll, nil
}

View File

@@ -2,6 +2,7 @@ package github
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGithub(cfg configcli.Config) *Github {
}
}
func (g *Github) Name() string {
return "github"
}
func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -2,6 +2,7 @@ package gitlab
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGitlab(cfg configcli.Config) *Gitlab {
}
}
func (g *Gitlab) Name() string {
return "gitlab"
}
func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -2,6 +2,7 @@ package gogs
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGogs(cfg configcli.Config) *Gogs {
}
}
func (g *Gogs) Name() string {
return "gogs"
}
func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -12,6 +12,7 @@ import (
)
type SourceControl interface {
Name() string
RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error
RequestClose(ctx context.Context, branch string, path string) error
RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error

View File

@@ -64,11 +64,11 @@ func (s *Sqlite) PackageModulesCreate(ctx context.Context, pkg *models.Package,
}
func (s *Sqlite) PackageDelete(ctx context.Context, req *pb.PackageDeleteReq) error {
return fmt.Errorf("need implement")
return fmt.Error("need implement")
}
func (s *Sqlite) PackageUpdate(ctx context.Context, req *pb.PackageUpdateReq) (*models.Package, error) {
return nil, fmt.Errorf("need implement")
return nil, fmt.Error("need implement")
}
func (s *Sqlite) PackageLookup(ctx context.Context, req *pb.PackageLookupReq) (*models.Package, error) {

View File

@@ -107,12 +107,12 @@ func parseModFile(ctx context.Context, store storage.Storage, pkg *models.Packag
ref, err := repo.Head()
if err != nil {
return fmt.Errorf("failed to get head: %v", err)
return fmt.Error("failed to get head: %v", err)
}
commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return fmt.Errorf("failed to get commit: %v", err)
return fmt.Error("failed to get commit: %v", err)
}
tree, err := commit.Tree()