#8 Finished the update I need to redo the call of these methods.

This commit is contained in:
Gorbunov Kirill Andreevich
2024-03-15 21:18:58 +03:00
parent f559bc86b8
commit e57e1ff82e
11 changed files with 152 additions and 57 deletions

View File

@@ -17,7 +17,6 @@ import (
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
"git.unistack.org/unistack-org/pkgdash/internal/source"
"github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
@@ -26,13 +25,26 @@ import (
"go.unistack.org/micro/v4/logger"
)
var (
ErrPRExist = errors.New("Pull request exists")
ErrPRNotExist = errors.New("Pull request does not exist")
)
type Gitea struct {
Token string
URL string
Token string
PRTitle string
PRBody string
Repository string
}
func NewGitea(t string) *Gitea {
func NewGitea(cfg configcli.Config) *Gitea {
return &Gitea{
Token: t,
URL: cfg.Source.APIURL,
Token: cfg.Source.Token,
PRTitle: cfg.PullRequestTitle,
PRBody: cfg.PullRequestBody,
Repository: cfg.Source.Repository,
}
}
@@ -45,29 +57,20 @@ type giteaPull struct {
ID int64 `json:"id"`
}
func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
logger.Debugf(ctx, "RequestOpen 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)
tplTitle, err := template.New("pull_request_title").Parse(g.PRTitle)
if err != nil {
logger.Fatalf(ctx, "failed to parse template: %v", err)
}
wTitle := bytes.NewBuffer(nil)
// создания шаблона тела для пулл реквеста
tplBody, err := template.New("pull_request_body").Parse(cfg.PullRequestBody)
tplBody, err := template.New("pull_request_body").Parse(g.PRTitle)
if err != nil {
logger.Fatalf(ctx, "failed to parse template: %v", err)
}
@@ -80,7 +83,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
}
//извлекаем ссылки с объектами из удаленного объекта??
if err = repo.FetchContext(ctx, &git.FetchOptions{
Auth: &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate {
logger.Fatalf(ctx, "failed to fetch repo: %v", err)
@@ -115,7 +118,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
}
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)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, g.URL+"/repos/"+g.Repository+"/pulls?state=open&token="+g.Token, nil)
if err != nil {
return err
} //Получаем список пулл реквестов
@@ -140,7 +143,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
for _, pull := range pulls {
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
logger.Infof(ctx, "skip %s as pr already exists %s", path, pull.URL)
return source.ErrPRExist
return ErrPRExist
} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
}
@@ -152,7 +155,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
} //вроде меняем ветку todo вроде можно удалить
if err = wtree.PullContext(ctx, &git.PullOptions{
Auth: &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
Depth: 1,
// RemoteURL :
Force: true,
@@ -229,7 +232,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
if err = repo.PushContext(ctx, &git.PushOptions{
RefSpecs: []gitconfig.RefSpec{refspec},
Auth: &httpauth.BasicAuth{Username: cfg.Source.Token, Password: cfg.Source.Token},
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
Force: true,
}); err != nil {
logger.Fatalf(ctx, "failed to push repo branch: %v", err)
@@ -263,7 +266,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, cfg *configcli.Config, branch s
logger.Infof(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))
req, err = http.NewRequestWithContext(ctx, http.MethodPost, g.URL+"/repos/"+g.Repository+"/pulls?token="+g.Token, bytes.NewReader(buf))
if err != nil {
return err
}
@@ -293,6 +296,7 @@ func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch
Repository: os.Getenv("GITHUB_REPOSITORY"),
}
}
fmt.Printf("cfg: %v", cfg)
var buf []byte
var err error
@@ -364,7 +368,7 @@ func (g *Gitea) RequestClose(ctx context.Context, cfg *configcli.Config, branch
}
if !prExist {
logger.Errorf(ctx, " skip %s since pr does not exist", path)
return source.ErrPRNotExist
return ErrPRNotExist
}
req, err = DeleteBranch(ctx, cfg, branch)
@@ -497,7 +501,7 @@ func (g *Gitea) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch
}
if !prExist {
logger.Errorf(ctx, " skip %s since pr does not exist", path)
return source.ErrPRNotExist
return ErrPRNotExist
}
logger.Infof(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)

View File

@@ -1,15 +1,28 @@
package github
import (
"context"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
)
type Github struct {
Token string
}
func NewGithub(t string) *Github {
func NewGithub(cfg configcli.Config) *Github {
return &Github{
Token: t,
Token: cfg.Source.Token,
}
}
func (g *Github) RequestOpen() {}
func (g *Github) RequestClose() {}
func (g *Github) RequestUpdate() {}
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 {
return nil
}
func (g *Github) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
return nil
}

View File

@@ -1,15 +1,28 @@
package gitlab
import (
"context"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
)
type Gitlab struct {
Token string
}
func NewGitlab(t string) *Gitlab {
func NewGitlab(cfg configcli.Config) *Gitlab {
return &Gitlab{
Token: t,
Token: cfg.Source.Token,
}
}
func (g *Gitlab) RequestOpen() {}
func (g *Gitlab) RequestClose() {}
func (g *Gitlab) RequestUpdate() {}
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 {
return nil
}
func (g *Gitlab) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
return nil
}

View File

@@ -1,15 +1,28 @@
package gogs
import (
"context"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
)
type Gogs struct {
Token string
}
func NewGogs(t string) *Gogs {
func NewGogs(cfg configcli.Config) *Gogs {
return &Gogs{
Token: t,
Token: cfg.Source.Token,
}
}
func (g *Gogs) RequestOpen() {}
func (g *Gogs) RequestClose() {}
func (g *Gogs) RequestUpdate() {}
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 {
return nil
}
func (g *Gogs) RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error {
return nil
}

View File

@@ -2,7 +2,6 @@ package source
import (
"context"
"errors"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -12,27 +11,22 @@ import (
"git.unistack.org/unistack-org/pkgdash/internal/source/gogs"
)
var (
ErrPRExist = errors.New("Pull request exists")
ErrPRNotExist = errors.New("Pull request does not exist")
)
type SourceControl interface {
RequestOpen(ctx context.Context, cfg *configcli.Config, 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
RequestUpdate(ctx context.Context, cfg *configcli.Config, branch string, path string, mod modules.Update) error
}
func NewSourceControl(system, token string) SourceControl {
switch system {
func NewSourceControl(cfg configcli.Config) SourceControl {
switch cfg.Source.TypeGit {
case "github":
return github.NewGithub(token)
return github.NewGithub(cfg)
case "gitlab":
return gitlab.NewGitlab(token)
return gitlab.NewGitlab(cfg)
case "gitea":
return gitea.NewGitea(token)
return gitea.NewGitea(cfg)
case "gogs":
return gogs.NewGogs(token)
return gogs.NewGogs(cfg)
}
return nil
}