#19 #20
@@ -4,3 +4,8 @@ source:
 | 
				
			|||||||
  apiurl: https://git.unistack.org/api/v1
 | 
					  apiurl: https://git.unistack.org/api/v1
 | 
				
			||||||
  repository: pkgdash
 | 
					  repository: pkgdash
 | 
				
			||||||
  owner: kgorbunov
 | 
					  owner: kgorbunov
 | 
				
			||||||
 | 
					update_opt:
 | 
				
			||||||
 | 
					  pre: false
 | 
				
			||||||
 | 
					  major: false
 | 
				
			||||||
 | 
					  up_major: false
 | 
				
			||||||
 | 
					  cached: true
 | 
				
			||||||
@@ -201,7 +201,7 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		if err := cw.Stop(); err != nil {
 | 
							if err := cw.Stop(); err != nil {
 | 
				
			||||||
			logger.Error(ctx, err)
 | 
								logger.Error(ctx, err.Error())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,7 @@ import (
 | 
				
			|||||||
	fileconfig "go.unistack.org/micro-config-file/v4"
 | 
						fileconfig "go.unistack.org/micro-config-file/v4"
 | 
				
			||||||
	"go.unistack.org/micro/v4/config"
 | 
						"go.unistack.org/micro/v4/config"
 | 
				
			||||||
	"go.unistack.org/micro/v4/logger"
 | 
						"go.unistack.org/micro/v4/logger"
 | 
				
			||||||
 | 
						"go.unistack.org/micro/v4/logger/slog"
 | 
				
			||||||
	"go.unistack.org/micro/v4/options"
 | 
						"go.unistack.org/micro/v4/options"
 | 
				
			||||||
	"golang.org/x/mod/modfile"
 | 
						"golang.org/x/mod/modfile"
 | 
				
			||||||
	"golang.org/x/mod/semver"
 | 
						"golang.org/x/mod/semver"
 | 
				
			||||||
@@ -71,7 +72,9 @@ func main() {
 | 
				
			|||||||
	ctx, cancel := context.WithCancel(context.Background())
 | 
						ctx, cancel := context.WithCancel(context.Background())
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3)); err != nil {
 | 
						logger.DefaultLogger = slog.NewLogger()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3), logger.WithLevel(logger.DebugLevel)); err != nil {
 | 
				
			||||||
		logger.Error(ctx, "logger init error: %v", err)
 | 
							logger.Error(ctx, "logger init error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -147,10 +150,10 @@ func main() {
 | 
				
			|||||||
	mvs := make(map[string]modules.Update)
 | 
						mvs := make(map[string]modules.Update)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	updateOptions := modules.UpdateOptions{
 | 
						updateOptions := modules.UpdateOptions{
 | 
				
			||||||
		Pre:     false,
 | 
							Pre:     cfg.UpdateOpt.Pre,
 | 
				
			||||||
		Major:   false,
 | 
							Major:   cfg.UpdateOpt.Major,
 | 
				
			||||||
		UpMajor: false,
 | 
							UpMajor: cfg.UpdateOpt.UpMajor,
 | 
				
			||||||
		Cached:  true,
 | 
							Cached:  cfg.UpdateOpt.Cached,
 | 
				
			||||||
		OnUpdate: func(u modules.Update) {
 | 
							OnUpdate: func(u modules.Update) {
 | 
				
			||||||
			var modpath string // new mod path with major
 | 
								var modpath string // new mod path with major
 | 
				
			||||||
			if u.Err != nil {
 | 
								if u.Err != nil {
 | 
				
			||||||
@@ -212,17 +215,23 @@ func main() {
 | 
				
			|||||||
	gitSource := source.NewSourceControl(*cfg)
 | 
						gitSource := source.NewSourceControl(*cfg)
 | 
				
			||||||
	for _, branch := range cfg.Branches {
 | 
						for _, branch := range cfg.Branches {
 | 
				
			||||||
		for pathMod, mod := range mvs {
 | 
							for pathMod, mod := range mvs {
 | 
				
			||||||
 | 
								logger.Debugf(ctx, "Start update %s from %s to %s", pathMod, mod.Module.Version, mod.Version)
 | 
				
			||||||
			err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
 | 
								err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
 | 
									if err.Error() == "pull request exists" {
 | 
				
			||||||
 | 
										err = gitSource.RequestUpdate(ctx, branch, pathMod, mod)
 | 
				
			||||||
 | 
										if err != nil {
 | 
				
			||||||
 | 
											logger.Error(ctx, "Update PR error: %s", err)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				logger.Error(ctx, "failed to create pr: %v", err)
 | 
									logger.Error(ctx, "failed to create pr: %v", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			logger.Info(ctx, "PR create for %s - %s", pathMod, mod.Version)
 | 
								logger.Debugf(ctx, "Update successful for %s", pathMod)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						//err = gitSource.RequestClose(ctx, "master", "modernc.org/ccgo/v4")
 | 
				
			||||||
	if err != nil {
 | 
						logger.Info(ctx, "Pkgdash successfully updated dependencies")
 | 
				
			||||||
		logger.Fatal(ctx, "failed to create pr: %v", err)
 | 
						time.Sleep(time.Second * 5)
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getRepoMgmt() string {
 | 
					func getRepoMgmt() string {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,7 @@ type Config struct {
 | 
				
			|||||||
	PullRequestBody  string     `json:"pull_request_body" yaml:"pull_request_body"`
 | 
						PullRequestBody  string     `json:"pull_request_body" yaml:"pull_request_body"`
 | 
				
			||||||
	Branches         []string   `json:"branches" yaml:"branches"`
 | 
						Branches         []string   `json:"branches" yaml:"branches"`
 | 
				
			||||||
	Source           *Source    `json:"source" yaml:"source"`
 | 
						Source           *Source    `json:"source" yaml:"source"`
 | 
				
			||||||
 | 
						UpdateOpt        *UpdateOpt `json:"update_opt" yaml:"update_opt"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Source struct {
 | 
					type Source struct {
 | 
				
			||||||
@@ -15,6 +16,13 @@ type Source struct {
 | 
				
			|||||||
	Owner      string `json:"owner" yaml:"owner"`
 | 
						Owner      string `json:"owner" yaml:"owner"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type UpdateOpt struct {
 | 
				
			||||||
 | 
						Pre     bool `json:"pre" yaml:"pre" default:"false"`
 | 
				
			||||||
 | 
						Major   bool `json:"major" yaml:"major" default:"false"`
 | 
				
			||||||
 | 
						UpMajor bool `json:"up_major" yaml:"up_major" default:"false"`
 | 
				
			||||||
 | 
						Cached  bool `json:"cached" yaml:"cached" default:"true"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewConfig() *Config {
 | 
					func NewConfig() *Config {
 | 
				
			||||||
	return &Config{
 | 
						return &Config{
 | 
				
			||||||
		Source: &Source{},
 | 
							Source: &Source{},
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -163,7 +163,6 @@ func Query(modpath string, cached bool) (*Module, bool, error) {
 | 
				
			|||||||
// cached sets the Disable-Module-Fetch: true header
 | 
					// cached sets the Disable-Module-Fetch: true header
 | 
				
			||||||
func Latest(modpath string, cached bool) (*Module, error) {
 | 
					func Latest(modpath string, cached bool) (*Module, error) {
 | 
				
			||||||
	latest, ok, err := Query(modpath, cached)
 | 
						latest, ok, err := Query(modpath, cached)
 | 
				
			||||||
	fmt.Println("latest: ", latest)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,8 +26,8 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	ErrPRExist    = errors.New("Pull request exists")
 | 
						ErrPRExist    = errors.New("pull request exists")
 | 
				
			||||||
	ErrPRNotExist = errors.New("Pull request does not exist")
 | 
						ErrPRNotExist = errors.New("pull request does not exist")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Gitea struct {
 | 
					type Gitea struct {
 | 
				
			||||||
@@ -56,6 +56,9 @@ type giteaPull struct {
 | 
				
			|||||||
	Base  struct {
 | 
						Base  struct {
 | 
				
			||||||
		Ref string `json:"ref"`
 | 
							Ref string `json:"ref"`
 | 
				
			||||||
	} `json:"base"`
 | 
						} `json:"base"`
 | 
				
			||||||
 | 
						Head struct {
 | 
				
			||||||
 | 
							Ref string `json:"ref"`
 | 
				
			||||||
 | 
						} `json:"head"`
 | 
				
			||||||
	ID int64 `json:"id"`
 | 
						ID int64 `json:"id"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -133,44 +136,18 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
 | 
				
			|||||||
		logger.Fatal(ctx, "failed to get worktree: %v", err)
 | 
							logger.Fatal(ctx, "failed to get worktree: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var pulls []*giteaPull
 | 
						pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
 | 
				
			||||||
	req, err := http.NewRequestWithContext(
 | 
						if err != nil && err != ErrPRNotExist {
 | 
				
			||||||
		ctx,
 | 
							logger.Error(ctx, "GetPulls error: %s", err)
 | 
				
			||||||
		http.MethodGet,
 | 
					 | 
				
			||||||
		fmt.Sprintf("%s/repos/%s/%s/pulls?state=open&token=%s", g.URL, g.Owner, g.Repository, g.Token),
 | 
					 | 
				
			||||||
		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
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	buf, _ = io.ReadAll(rsp.Body)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	switch rsp.StatusCode {
 | 
					 | 
				
			||||||
	case http.StatusOK:
 | 
					 | 
				
			||||||
		if err = json.Unmarshal(buf, &pulls); err != nil {
 | 
					 | 
				
			||||||
			logger.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
 | 
					 | 
				
			||||||
		} // записываем ответ от гита по пулл реквестам, видимо существующим
 | 
					 | 
				
			||||||
		// перебираем наши модификации и если они уже есть в гите удаляем их из mods
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, pull := range pulls {
 | 
						for _, pull := range pulls {
 | 
				
			||||||
			if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
 | 
							if strings.Contains(pull.Title, path) && strings.Contains(pull.Base.Ref, branch) {
 | 
				
			||||||
			logger.Info(ctx, "skip %s as pr already exists %s", path, pull.URL)
 | 
								logger.Info(ctx, "skip %s as pr already exists %s", path, pull.URL)
 | 
				
			||||||
			return ErrPRExist
 | 
								return ErrPRExist
 | 
				
			||||||
		} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
 | 
							} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	case http.StatusNotFound:
 | 
					 | 
				
			||||||
		logger.Info(ctx, "PL is not exist for %s", g.Repository)
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		return fmt.Errorf("unknown error: %s", buf)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Info(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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -248,14 +225,6 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
 | 
				
			|||||||
		logger.Fatal(ctx, "failed to commit: %v", err)
 | 
							logger.Fatal(ctx, "failed to commit: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// newref := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/pkgdash-1/go_modules/%s-%s", path, mod.Version)), headRef.Hash())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/*
 | 
					 | 
				
			||||||
		if err = repo.Storer.SetReference(newref); err != nil {
 | 
					 | 
				
			||||||
			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)) //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, "try to push refspec %s", refspec)
 | 
						logger.Info(ctx, "try to push refspec %s", refspec)
 | 
				
			||||||
@@ -283,7 +252,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	logger.Info(ctx, "marshal body: %s", buf)
 | 
						logger.Info(ctx, "marshal body: %s", buf)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req, err = http.NewRequestWithContext(
 | 
						req, err := http.NewRequestWithContext(
 | 
				
			||||||
		ctx,
 | 
							ctx,
 | 
				
			||||||
		http.MethodPost,
 | 
							http.MethodPost,
 | 
				
			||||||
		fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
 | 
							fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
 | 
				
			||||||
@@ -295,7 +264,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
 | 
				
			|||||||
	req.Header.Add("Accept", "application/json")
 | 
						req.Header.Add("Accept", "application/json")
 | 
				
			||||||
	req.Header.Add("Content-Type", "application/json")
 | 
						req.Header.Add("Content-Type", "application/json")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rsp, err = http.DefaultClient.Do(req)
 | 
						rsp, err := http.DefaultClient.Do(req)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	} //Вроде создаем новый реквест на создание пулл реквеста
 | 
						} //Вроде создаем новый реквест на создание пулл реквеста
 | 
				
			||||||
@@ -304,88 +273,27 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
 | 
				
			|||||||
		return fmt.Errorf("unknown error: %s", buf)
 | 
							return fmt.Errorf("unknown error: %s", buf)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger.Info(ctx, "PR create for %s-%s", path, mod.Version)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error {
 | 
					func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error {
 | 
				
			||||||
	logger.Debugf(ctx, "RequestOpen start, mod title: %s", path)
 | 
						logger.Debugf(ctx, "RequestClose start, mod title: %s", path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cfg.Source == nil {
 | 
						pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
 | 
				
			||||||
		cfg.Source = &configcli.Source{
 | 
					 | 
				
			||||||
			TypeGit:    "gitea",
 | 
					 | 
				
			||||||
			Token:      os.Getenv("GITHUB_TOKEN"),
 | 
					 | 
				
			||||||
			APIURL:     os.Getenv("GITHUB_API_URL"),
 | 
					 | 
				
			||||||
			Repository: os.Getenv("GITHUB_REPOSITORY"),
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	fmt.Printf("cfg: %v", cfg)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var buf []byte
 | 
					 | 
				
			||||||
	var err error
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to open repo: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	//извлекаем ссылки с объектами из удаленного объекта??
 | 
					 | 
				
			||||||
	if err = repo.FetchContext(ctx, &git.FetchOptions{
 | 
					 | 
				
			||||||
		Auth:  &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
 | 
					 | 
				
			||||||
		Force: true,
 | 
					 | 
				
			||||||
	}); err != nil && err != git.NoErrAlreadyUpToDate {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to fetch repo: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var headRef *plumbing.Reference // вроде ссылка на гит
 | 
					 | 
				
			||||||
	refIter, err := repo.Branches() //получение веток
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		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
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} //перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
 | 
					 | 
				
			||||||
	refIter.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if headRef == nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to get repo branch head")
 | 
					 | 
				
			||||||
	} // Не получили нужную ветку
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Info(ctx, "repo head %s", headRef)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var pulls []*giteaPull
 | 
					 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, cfg.Source.APIURL+"/repos/"+cfg.Source.Repository+"/pulls?state=open&token="+cfg.Source.Token, 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 {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							logger.Error(ctx, "GetPulls error: %s", err)
 | 
				
			||||||
		return err
 | 
							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 {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
 | 
					 | 
				
			||||||
	} // записываем ответ от гита по пулл реквестам, видимо существующим
 | 
					 | 
				
			||||||
	// перебираем наши модификации и если они уже есть в гите удаляем их из mods
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	prExist := false
 | 
						prExist := false
 | 
				
			||||||
 | 
						var b string // Name of the branch to be deleted
 | 
				
			||||||
	for _, pull := range pulls {
 | 
						for _, pull := range pulls {
 | 
				
			||||||
		if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
 | 
							if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
 | 
				
			||||||
			logger.Info(ctx, "skip %s since pr does not exist %s", path, pull.URL)
 | 
								logger.Info(ctx, "PR for %s exists: %s", path, pull.URL)
 | 
				
			||||||
			prExist = true
 | 
								prExist = true
 | 
				
			||||||
 | 
								b = pull.Head.Ref
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !prExist {
 | 
						if !prExist {
 | 
				
			||||||
@@ -393,121 +301,37 @@ func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch
 | 
				
			|||||||
		return ErrPRNotExist
 | 
							return ErrPRNotExist
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req, err = DeleteBranch(ctx, cfg, branch)
 | 
						req, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Token)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error(ctx, "failed to create request for delete the branch: %s, err: %s", branch, err)
 | 
							logger.Error(ctx, "failed to create request for delete the branch: %s, err: %s", branch, err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	rsp, err = http.DefaultClient.Do(req)
 | 
						rsp, err := http.DefaultClient.Do(req)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error(ctx, "failed to do request for delete the branch: %s, err: %s, code: %s", branch, err, rsp.StatusCode)
 | 
							logger.Error(ctx, "failed to do request for delete the branch: %s, err: %s, code: %s", branch, err, rsp.StatusCode)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Info(ctx, "Delete branch %s successful", branch)
 | 
						logger.Info(ctx, "Delete branch for %s successful", path)
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (g *Gitea) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
 | 
					func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	logger.Debugf(ctx, "RequestOpen start, mod title: %s", path)
 | 
						logger.Debugf(ctx, "RequestUpdate start, mod title: %s", path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cfg.Source == nil {
 | 
						pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
 | 
				
			||||||
		cfg.Source = &configcli.Source{
 | 
					 | 
				
			||||||
			TypeGit:    "gitea",
 | 
					 | 
				
			||||||
			Token:      os.Getenv("GITHUB_TOKEN"),
 | 
					 | 
				
			||||||
			APIURL:     os.Getenv("GITHUB_API_URL"),
 | 
					 | 
				
			||||||
			Repository: os.Getenv("GITHUB_REPOSITORY"),
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var buf []byte
 | 
					 | 
				
			||||||
	var err error
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// создания шаблона названия для пулл реквеста
 | 
					 | 
				
			||||||
	tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		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.Fatal(ctx, "failed to parse template: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	wBody := bytes.NewBuffer(nil)
 | 
					 | 
				
			||||||
	// открытие гит репозитория с опцией обхода репозитория для нахождения .git
 | 
					 | 
				
			||||||
	repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to open repo: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	//извлекаем ссылки с объектами из удаленного объекта??
 | 
					 | 
				
			||||||
	if err = repo.FetchContext(ctx, &git.FetchOptions{
 | 
					 | 
				
			||||||
		Auth:  &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
 | 
					 | 
				
			||||||
		Force: true,
 | 
					 | 
				
			||||||
	}); err != nil && err != git.NoErrAlreadyUpToDate {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to fetch repo: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var headRef *plumbing.Reference // вроде ссылка на гит
 | 
					 | 
				
			||||||
	refIter, err := repo.Branches() //получение веток
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		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
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} //перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
 | 
					 | 
				
			||||||
	refIter.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if headRef == nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to get repo branch head")
 | 
					 | 
				
			||||||
	} // Не получили нужную ветку
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Info(ctx, "repo head %s", headRef)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	wtree, err := repo.Worktree()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to get worktree: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var pulls []*giteaPull
 | 
					 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, cfg.Source.APIURL+"/repos/"+cfg.Source.Repository+"/pulls?state=open&token="+cfg.Source.Token, 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 {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							logger.Error(ctx, "GetPulls error: %s", err)
 | 
				
			||||||
		return err
 | 
							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 {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
 | 
					 | 
				
			||||||
	} // записываем ответ от гита по пулл реквестам, видимо существующим
 | 
					 | 
				
			||||||
	// перебираем наши модификации и если они уже есть в гите удаляем их из mods
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	prExist := false
 | 
						prExist := false
 | 
				
			||||||
	for _, pull := range pulls {
 | 
						for _, pull := range pulls {
 | 
				
			||||||
		if strings.Contains(pull.Title, path) && pull.Base.Ref != branch {
 | 
							if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
 | 
				
			||||||
			logger.Info(ctx, "skip %s since pr does not exist %s", path, pull.URL) //todo
 | 
								logger.Info(ctx, "don't skip %s since pr exist %s", path, pull.URL) //todo
 | 
				
			||||||
			titleVersions := getVersions(pull.Title)
 | 
								tVersion := getVersions(pull.Head.Ref)                              //Надо взять просто из названия ветки последнюю версию
 | 
				
			||||||
			if modules.IsNewerVersion(titleVersions.NewV, mod.Version, false) {
 | 
								if modules.IsNewerVersion(tVersion, mod.Version, false) {
 | 
				
			||||||
				reqDel, err := DeleteBranch(ctx, cfg, branch)
 | 
									reqDel, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, pull.Head.Ref, g.Token)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					logger.Error(ctx, "Error with create request for branch: %s, err: %s", branch, err)
 | 
										logger.Error(ctx, "Error with create request for branch: %s, err: %s", branch, err)
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
@@ -517,6 +341,7 @@ func (g *Gitea) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch
 | 
				
			|||||||
					logger.Error(ctx, "Error with do request for branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode)
 | 
										logger.Error(ctx, "Error with do request for branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode)
 | 
				
			||||||
					continue //думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации
 | 
										continue //думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									logger.Info(ctx, "Old pr %s successful delete", pull.Head.Ref)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			prExist = true
 | 
								prExist = true
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -528,167 +353,20 @@ func (g *Gitea) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	logger.Info(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.Info(ctx, "reset worktree")
 | 
						return g.RequestOpen(ctx, branch, path, mod)
 | 
				
			||||||
	if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to reset repo branch: %v", err)
 | 
					 | 
				
			||||||
	} //вроде меняем ветку
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err = wtree.PullContext(ctx, &git.PullOptions{
 | 
					 | 
				
			||||||
		Auth:  &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
 | 
					 | 
				
			||||||
		Depth: 1,
 | 
					 | 
				
			||||||
		//	RemoteURL :
 | 
					 | 
				
			||||||
		Force:      true,
 | 
					 | 
				
			||||||
		RemoteName: "origin",
 | 
					 | 
				
			||||||
	}); err != nil && err != git.NoErrAlreadyUpToDate {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to pull repo: %v", err)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Info(ctx, "checkout ref %s", headRef)
 | 
					func getVersions(s string) string {
 | 
				
			||||||
	if err = wtree.Checkout(&git.CheckoutOptions{
 | 
						re := regexp.MustCompile("[vV][0-9]+\\.[0-9]+\\.[0-9]+")
 | 
				
			||||||
		Hash:   headRef.Hash(),
 | 
					 | 
				
			||||||
		Branch: headRef.Name(),
 | 
					 | 
				
			||||||
		Create: false,
 | 
					 | 
				
			||||||
		Force:  true,
 | 
					 | 
				
			||||||
	}); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to checkout tree: %v", err)
 | 
					 | 
				
			||||||
	} //вроде как переходим на другую ветку
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	epath, err := exec.LookPath("go")
 | 
						version := re.FindString(s)
 | 
				
			||||||
	if errors.Is(err, exec.ErrDot) {
 | 
					 | 
				
			||||||
		err = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to find go command: %v", err)
 | 
					 | 
				
			||||||
	} // ищем go файл
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var cmd *exec.Cmd
 | 
						return version
 | 
				
			||||||
	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.Fatal(ctx, "failed to run go mod edit: %s err: %v", out, err)
 | 
					 | 
				
			||||||
	} // пытаемся выполнить команду go mod edit с новой версией модуля
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	cmd = exec.CommandContext(ctx, epath, "mod", "tidy")
 | 
					 | 
				
			||||||
	if out, err = cmd.CombinedOutput(); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to run go mod tidy: %s err: %v", out, err)
 | 
					 | 
				
			||||||
	} // пытаемся выполнить команду go mod tidy пытаемся подтянуть новую версию модуля
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Info(ctx, "worktree add go.mod")
 | 
					 | 
				
			||||||
	if _, err = wtree.Add("go.mod"); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to add file: %v", err)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Info(ctx, "worktree add go.sum")
 | 
					func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) {
 | 
				
			||||||
	if _, err = wtree.Add("go.sum"); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to add file: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	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 {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to commit: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// newref := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/pkgdash-1/go_modules/%s-%s", path, mod.Version)), headRef.Hash())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/*
 | 
					 | 
				
			||||||
		if err = repo.Storer.SetReference(newref); err != nil {
 | 
					 | 
				
			||||||
			logger.Fatal(ctx, "failed to create repo branch: %v", err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	refspec := gitconfig.RefSpec(fmt.Sprintf("+refs/heads/pkgdash-1/go_modules/%s-%s:refs/heads/pkgdash-1/go_modules/%s-%s", path, mod.Version, path, mod.Version))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Info(ctx, "try to push refspec %s", refspec)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err = repo.PushContext(ctx, &git.PushOptions{
 | 
					 | 
				
			||||||
		RefSpecs: []gitconfig.RefSpec{refspec},
 | 
					 | 
				
			||||||
		Auth:     &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
 | 
					 | 
				
			||||||
		Force:    true,
 | 
					 | 
				
			||||||
	}); err != nil {
 | 
					 | 
				
			||||||
		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 {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to execute template: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if err = tplBody.Execute(wBody, data); err != nil {
 | 
					 | 
				
			||||||
		logger.Fatal(ctx, "failed to execute template: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	body := map[string]string{
 | 
					 | 
				
			||||||
		"base":  branch,
 | 
					 | 
				
			||||||
		"body":  wBody.String(),
 | 
					 | 
				
			||||||
		"head":  fmt.Sprintf("pkgdash-1/go_modules/%s-%s", path, mod.Version),
 | 
					 | 
				
			||||||
		"title": wTitle.String(),
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	logger.Info(ctx, "raw body: %#+v", body)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	buf, err = json.Marshal(body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Info(ctx, "marshal body: %s", buf)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	req, err = http.NewRequestWithContext(ctx, http.MethodPost, cfg.Source.APIURL+"/repos/"+cfg.Source.Repository+"/pulls?token="+cfg.Source.Token, 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
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func getVersions(s string) *Tmod {
 | 
					 | 
				
			||||||
	rsp := new(Tmod)
 | 
					 | 
				
			||||||
	re := regexp.MustCompile("[0-9]+\\.[0-9]+\\.[0-9]+")
 | 
					 | 
				
			||||||
	versions := re.FindAllString(s, -1)
 | 
					 | 
				
			||||||
	if len(versions) < 2 {
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if modules.IsNewerVersion(versions[0], versions[1], false) {
 | 
					 | 
				
			||||||
		rsp.OldV = versions[0]
 | 
					 | 
				
			||||||
		rsp.NewV = versions[1]
 | 
					 | 
				
			||||||
		return rsp
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	rsp.OldV = versions[1]
 | 
					 | 
				
			||||||
	rsp.NewV = versions[0]
 | 
					 | 
				
			||||||
	return rsp
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Tmod struct {
 | 
					 | 
				
			||||||
	OldV string
 | 
					 | 
				
			||||||
	NewV string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func DeleteBranch(ctx context.Context, cfg *configcli.Config, branch string) (*http.Request, error) {
 | 
					 | 
				
			||||||
	var buf []byte
 | 
						var buf []byte
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, cfg.Source.APIURL+"/repos/"+cfg.Source.Repository+"/branches/"+branch+"?token="+cfg.Source.Token, bytes.NewReader(buf))
 | 
						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))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -696,3 +374,42 @@ func DeleteBranch(ctx context.Context, cfg *configcli.Config, branch string) (*h
 | 
				
			|||||||
	req.Header.Add("Content-Type", "application/json")
 | 
						req.Header.Add("Content-Type", "application/json")
 | 
				
			||||||
	return req, err
 | 
						return req, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull, error) {
 | 
				
			||||||
 | 
						var pulls []*giteaPull
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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
 | 
				
			||||||
 | 
						} //вроде запроса к репозиторию
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
 | 
				
			||||||
 | 
								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)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,9 +20,9 @@ func NewGithub(cfg configcli.Config) *Github {
 | 
				
			|||||||
func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
					func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Github) RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error {
 | 
					func (g *Github) RequestClose(ctx context.Context, branch string, path string) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Github) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
 | 
					func (g *Github) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,9 +20,9 @@ func NewGitlab(cfg configcli.Config) *Gitlab {
 | 
				
			|||||||
func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
					func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Gitlab) RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error {
 | 
					func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Gitlab) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
 | 
					func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,9 +20,9 @@ func NewGogs(cfg configcli.Config) *Gogs {
 | 
				
			|||||||
func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
					func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Gogs) RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error {
 | 
					func (g *Gogs) RequestClose(ctx context.Context, branch string, path string) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (g *Gogs) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
 | 
					func (g *Gogs) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,8 +13,8 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type SourceControl interface {
 | 
					type SourceControl interface {
 | 
				
			||||||
	RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error
 | 
						RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error
 | 
				
			||||||
	RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error
 | 
						RequestClose(ctx context.Context, branch string, path string) error
 | 
				
			||||||
	RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error
 | 
						RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewSourceControl(cfg configcli.Config) SourceControl {
 | 
					func NewSourceControl(cfg configcli.Config) SourceControl {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user