#8 implement interface Source and methods for gitea without Update #9

Merged
vtolstov merged 27 commits from kgorbunov/pkgdash:master into master 2024-03-24 20:52:34 +03:00
11 changed files with 152 additions and 57 deletions
Showing only changes of commit e57e1ff82e - Show all commits

6
.gitea/pkgdashcli.yaml Normal file
View File

@ -0,0 +1,6 @@
branches: [develop]
source:
type: gitea
token: f9cc8a7894ba3ce8f5a15b589b4011d7f298fbd2
apiurl: https://git.unistack.org/kgorbunov
repository: pkgdash

View File

@ -2,6 +2,10 @@
build:
CGO_ENABLED=0 go build -o bin/app -mod=readonly git.unistack.org/unistack-org/pkgdash/cmd/pkgdash
.PHONY: buildcli
buildcli:
CGO_ENABLED=0 go build -o bin/app -mod=readonly git.unistack.org/unistack-org/pkgdash/cmd/pkgdashcli
.PHONY: test
test:
go test -v ./... -race -cover

View File

@ -17,6 +17,7 @@ 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"
@ -29,6 +30,8 @@ import (
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/options"
"golang.org/x/mod/modfile"
"gopkg.in/yaml.v2"
)
// https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
@ -62,6 +65,7 @@ type Data struct {
}
func main() {
fmt.Println("Start...")
var err error
ctx, cancel := context.WithCancel(context.Background())
@ -71,14 +75,13 @@ func main() {
logger.Errorf(ctx, "logger init error: %v", err)
}
cfg := &configcli.Config{}
cfg := configcli.NewConfig()
if err = config.Load(ctx,
[]config.Config{
config.NewConfig(
config.Struct(cfg),
),
envconfig.NewConfig(
config.Struct(cfg),
),
@ -99,9 +102,23 @@ func main() {
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
logger.Fatalf(ctx, "failed to load config: %v", err)
}
logger.Infof(ctx, "c: %v", c)
}
}
file, err := os.Open(".gitea/pkgdashcli.yaml")
if err != nil {
logger.Fatalf(ctx, "file open: %s", err)
}
defer file.Close()
d := yaml.NewDecoder(file)
if err = d.Decode(&cfg); err != nil {
logger.Fatalf(ctx, "Decode err: %s", d)
}
logger.Infof(ctx, "Load config... %s", cfg)
if cfg.PullRequestBody == "" {
cfg.PullRequestBody = DefaultPullRequestBody
}
@ -149,23 +166,45 @@ func main() {
modules.Updates(updateOptions)
var repoGit, tokenGit string
/*var repoGit, tokenGit string // nameGit = gitea, repoGit = pkgdash, tokenGit = {xxx}, machine??
if cfg.Source != nil {
repoGit = cfg.Source.TypeGit
tokenGit = cfg.Source.Token
} else {
repoGit = getRepoMgmt()
if repoGit == "unknown" {
logger.Fatalf(ctx, "failed to get repo management")
logger.Fatalf(ctx, "pkgdash/main failed to get repo management")
}
}
_ = tokenGit
usr, err := user.Current()
if err != nil {
logger.Errorf(ctx, "pkgdash/main can t get info user: %s", err)
} else {
n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
if err != nil {
logger.Errorf(ctx, "pkgdash/main can t parse .netrc: %s", err)
}
tokenGit = n.Machine(repoGit).Get("password")
}
switch repoGit {
case "gitea":
for _, branch := range cfg.Branches {
err = giteaPullRequest(ctx, cfg, branch, mvs)
}
}*/
logger.Infof(ctx, "cfg: %v", cfg)
gitSource := source.NewSourceControl(*cfg)
for _, branch := range cfg.Branches {
for pathMod, mod := range mvs {
err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
if err != nil {
logger.Fatalf(ctx, "failed to create pr: %v", err)
}
}
}
if err != nil {

1
go.mod
View File

@ -67,6 +67,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jdx/go-netrc v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lyft/protoc-gen-star/v2 v2.0.3 // indirect

2
go.sum
View File

@ -677,6 +677,8 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jdx/go-netrc v1.0.0 h1:QbLMLyCZGj0NA8glAhxUpf1zDg6cxnWgMBbjq40W0gQ=
github.com/jdx/go-netrc v1.0.0/go.mod h1:Gh9eFQJnoTNIRHXl2j5bJXA1u84hQWJWgGh569zF3v8=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

View File

@ -13,3 +13,9 @@ type Source struct {
APIURL string `json:"apiurl" yaml:"apiurl"`
Repository string `json:"repository" yaml:"repository"`
}
func NewConfig() *Config {
return &Config{
Source: &Source{},
}
}

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 {
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
}