diff --git a/.gitea/pkgdashcli.yaml b/.gitea/pkgdashcli.yaml index 5264ebb..3cbceb9 100644 --- a/.gitea/pkgdashcli.yaml +++ b/.gitea/pkgdashcli.yaml @@ -3,4 +3,9 @@ source: type: gitea apiurl: https://git.unistack.org/api/v1 repository: pkgdash - owner: kgorbunov \ No newline at end of file + owner: kgorbunov +update_opt: + pre: false + major: false + up_major: false + cached: true \ No newline at end of file diff --git a/cmd/pkgdash/main.go b/cmd/pkgdash/main.go index 8c35c87..f09a20b 100644 --- a/cmd/pkgdash/main.go +++ b/cmd/pkgdash/main.go @@ -201,7 +201,7 @@ func main() { defer func() { if err := cw.Stop(); err != nil { - logger.Error(ctx, err) + logger.Error(ctx, err.Error()) } }() diff --git a/cmd/pkgdashcli/main.go b/cmd/pkgdashcli/main.go index 9aa93a4..1848c80 100644 --- a/cmd/pkgdashcli/main.go +++ b/cmd/pkgdashcli/main.go @@ -28,6 +28,7 @@ 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" @@ -71,7 +72,9 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) 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) } @@ -147,10 +150,10 @@ func main() { mvs := make(map[string]modules.Update) updateOptions := modules.UpdateOptions{ - Pre: false, - Major: false, - UpMajor: 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 { @@ -212,17 +215,23 @@ func main() { gitSource := source.NewSourceControl(*cfg) for _, branch := range cfg.Branches { 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) 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.Info(ctx, "PR create for %s - %s", pathMod, mod.Version) + logger.Debugf(ctx, "Update successful for %s", pathMod) } } - - if err != nil { - logger.Fatal(ctx, "failed to create pr: %v", err) - } + //err = gitSource.RequestClose(ctx, "master", "modernc.org/ccgo/v4") + logger.Info(ctx, "Pkgdash successfully updated dependencies") + time.Sleep(time.Second * 5) } func getRepoMgmt() string { diff --git a/internal/configcli/config.go b/internal/configcli/config.go index cfb1d47..a42dafd 100644 --- a/internal/configcli/config.go +++ b/internal/configcli/config.go @@ -1,10 +1,11 @@ package configcli 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"` - Source *Source `json:"source" yaml:"source"` + 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"` + Source *Source `json:"source" yaml:"source"` + UpdateOpt *UpdateOpt `json:"update_opt" yaml:"update_opt"` } type Source struct { @@ -15,6 +16,13 @@ type Source struct { 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 { return &Config{ Source: &Source{}, diff --git a/internal/modules/modproxy.go b/internal/modules/modproxy.go index 2b50f18..8c16244 100644 --- a/internal/modules/modproxy.go +++ b/internal/modules/modproxy.go @@ -163,7 +163,6 @@ func Query(modpath string, cached bool) (*Module, bool, error) { // cached sets the Disable-Module-Fetch: true header func Latest(modpath string, cached bool) (*Module, error) { latest, ok, err := Query(modpath, cached) - fmt.Println("latest: ", latest) if err != nil { return nil, err } diff --git a/internal/source/gitea/gitea.go b/internal/source/gitea/gitea.go index 79fc8e7..5a607a1 100644 --- a/internal/source/gitea/gitea.go +++ b/internal/source/gitea/gitea.go @@ -26,8 +26,8 @@ import ( ) var ( - ErrPRExist = errors.New("Pull request exists") - ErrPRNotExist = errors.New("Pull request does not exist") + ErrPRExist = errors.New("pull request exists") + ErrPRNotExist = errors.New("pull request does not exist") ) type Gitea struct { @@ -56,6 +56,9 @@ type giteaPull struct { Base struct { Ref string `json:"ref"` } `json:"base"` + Head struct { + Ref string `json:"ref"` + } `json:"head"` ID int64 `json:"id"` } @@ -133,43 +136,17 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod logger.Fatal(ctx, "failed to get worktree: %v", err) } - var pulls []*giteaPull - req, err := http.NewRequestWithContext( - ctx, - 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 { + pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token) + if err != nil && err != ErrPRNotExist { + logger.Error(ctx, "GetPulls error: %s", 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 { - if strings.Contains(pull.Title, path) && pull.Base.Ref == branch { - logger.Info(ctx, "skip %s as pr already exists %s", path, pull.URL) - return ErrPRExist - } // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим - } - case http.StatusNotFound: - logger.Info(ctx, "PL is not exist for %s", g.Repository) - default: - return fmt.Errorf("unknown error: %s", buf) - + for _, pull := range pulls { + 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) + return ErrPRExist + } // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим } 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) } - // 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 как будто нужно переделать 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) - req, err = http.NewRequestWithContext( + req, err := http.NewRequestWithContext( ctx, http.MethodPost, 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("Content-Type", "application/json") - rsp, err = http.DefaultClient.Do(req) + rsp, err := http.DefaultClient.Do(req) if err != nil { 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) } + logger.Info(ctx, "PR create for %s-%s", path, mod.Version) + return nil } -func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error { - logger.Debugf(ctx, "RequestOpen start, mod title: %s", path) +func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error { + logger.Debugf(ctx, "RequestClose start, mod title: %s", path) - if cfg.Source == nil { - 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) // выполнение запроса + pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token) if err != nil { + logger.Error(ctx, "GetPulls error: %s", 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 + var b string // Name of the branch to be deleted for _, pull := range pulls { 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 + b = pull.Head.Ref } } if !prExist { @@ -393,121 +301,37 @@ func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch return ErrPRNotExist } - req, err = DeleteBranch(ctx, cfg, branch) + req, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Token) if err != nil { logger.Error(ctx, "failed to create request for delete the branch: %s, err: %s", branch, err) return err } - rsp, err = http.DefaultClient.Do(req) + rsp, err := http.DefaultClient.Do(req) if err != nil { logger.Error(ctx, "failed to do request for delete the branch: %s, err: %s, code: %s", branch, err, rsp.StatusCode) return err } - logger.Info(ctx, "Delete branch %s successful", branch) + logger.Info(ctx, "Delete branch for %s successful", path) return nil } -func (g *Gitea) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error { - logger.Debugf(ctx, "RequestOpen start, mod title: %s", path) +func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error { + logger.Debugf(ctx, "RequestUpdate start, mod title: %s", path) - if cfg.Source == nil { - 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) // выполнение запроса + pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token) if err != nil { + logger.Error(ctx, "GetPulls error: %s", 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 for _, pull := range pulls { - 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 - titleVersions := getVersions(pull.Title) - if modules.IsNewerVersion(titleVersions.NewV, mod.Version, false) { - reqDel, err := DeleteBranch(ctx, cfg, branch) + if strings.Contains(pull.Title, path) && pull.Base.Ref == branch { + logger.Info(ctx, "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 { logger.Error(ctx, "Error with create request for branch: %s, err: %s", branch, err) 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) continue //думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации } + logger.Info(ctx, "Old pr %s successful delete", pull.Head.Ref) } 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, "reset worktree") - 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) - if err = wtree.Checkout(&git.CheckoutOptions{ - 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") - 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 - 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") - 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 + return g.RequestOpen(ctx, branch, path, mod) } -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 +func getVersions(s string) string { + re := regexp.MustCompile("[vV][0-9]+\\.[0-9]+\\.[0-9]+") + + version := re.FindString(s) + + return version } -type Tmod struct { - OldV string - NewV string -} - -func DeleteBranch(ctx context.Context, cfg *configcli.Config, branch string) (*http.Request, error) { +func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) { 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 { 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") 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) + } +} diff --git a/internal/source/github/github.go b/internal/source/github/github.go index 240ec48..9156bb3 100644 --- a/internal/source/github/github.go +++ b/internal/source/github/github.go @@ -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 { 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 } -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 } diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index f967528..dbc0af2 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -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 { 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 } -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 } diff --git a/internal/source/gogs/gogs.go b/internal/source/gogs/gogs.go index 8f49930..d7b8fdc 100644 --- a/internal/source/gogs/gogs.go +++ b/internal/source/gogs/gogs.go @@ -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 { 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 } -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 } diff --git a/internal/source/source.go b/internal/source/source.go index 6350163..fb6686c 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -13,8 +13,8 @@ import ( type SourceControl interface { RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error - RequestClose(ctx context.Context, cfg *configcli.Config, branch string, path string) error - RequestUpdate(ctx context.Context, cfg *configcli.Config, 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 } func NewSourceControl(cfg configcli.Config) SourceControl {