From dc192ce1ccd9cd40438b424e35cc98184344acc4 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Fri, 15 Mar 2024 23:12:39 +0300 Subject: [PATCH] #8 Code correction, refinement of pull request opening. --- .gitea/pkgdashcli.yaml | 5 +++-- .gitignore | 1 + internal/configcli/config.go | 1 + internal/source/gitea/gitea.go | 36 +++++++++++++++++++++------------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.gitea/pkgdashcli.yaml b/.gitea/pkgdashcli.yaml index 2b572e1..02cae11 100644 --- a/.gitea/pkgdashcli.yaml +++ b/.gitea/pkgdashcli.yaml @@ -2,5 +2,6 @@ branches: [develop] source: type: gitea token: f9cc8a7894ba3ce8f5a15b589b4011d7f298fbd2 - apiurl: https://git.unistack.org/kgorbunov - repository: pkgdash \ No newline at end of file + apiurl: https://git.unistack.org/api/v1 + repository: pkgdash + owner: kgorbunov \ No newline at end of file diff --git a/.gitignore b/.gitignore index d2176e5..71d09b2 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ cmd/pkgdash/pkgdash cmd/pkgdashcli/pkgdashcli *.sqlite *.db +.gitea/pkgdashcli.yaml \ No newline at end of file diff --git a/internal/configcli/config.go b/internal/configcli/config.go index fe93691..cfb1d47 100644 --- a/internal/configcli/config.go +++ b/internal/configcli/config.go @@ -12,6 +12,7 @@ type Source struct { Token string `json:"token" yaml:"token"` APIURL string `json:"apiurl" yaml:"apiurl"` Repository string `json:"repository" yaml:"repository"` + Owner string `json:"owner" yaml:"owner"` } func NewConfig() *Config { diff --git a/internal/source/gitea/gitea.go b/internal/source/gitea/gitea.go index 0913055..536fcbc 100644 --- a/internal/source/gitea/gitea.go +++ b/internal/source/gitea/gitea.go @@ -36,6 +36,7 @@ type Gitea struct { PRTitle string PRBody string Repository string + Owner string } func NewGitea(cfg configcli.Config) *Gitea { @@ -45,6 +46,7 @@ func NewGitea(cfg configcli.Config) *Gitea { PRTitle: cfg.PullRequestTitle, PRBody: cfg.PullRequestBody, Repository: cfg.Source.Repository, + Owner: cfg.Source.Owner, } } @@ -76,6 +78,20 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod } wBody := bytes.NewBuffer(nil) + + data := map[string]string{ + "Name": path, + "VersionOld": mod.Module.Version, + "VersionNew": mod.Version, + } + + if err = tplTitle.Execute(wTitle, data); err != nil { + logger.Fatalf(ctx, "failed to execute template: %v", err) + } + if err = tplBody.Execute(wBody, data); err != nil { + logger.Fatalf(ctx, "failed to execute template: %v", err) + } + // открытие гит репозитория с опцией обхода репозитория для нахождения .git repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) if err != nil { @@ -243,19 +259,6 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod logger.Fatalf(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.Fatalf(ctx, "failed to execute template: %v", err) - } - if err = tplBody.Execute(wBody, data); err != nil { - logger.Fatalf(ctx, "failed to execute template: %v", err) - } - body := map[string]string{ "base": "refs/heads/develop", "body": wBody.String(), @@ -271,7 +274,12 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod logger.Infof(ctx, "marshal body: %s", buf) - req, err = http.NewRequestWithContext(ctx, http.MethodPost, g.URL+"/repos/"+g.Repository+"/pulls?token="+g.Token, bytes.NewReader(buf)) + req, err = http.NewRequestWithContext( + ctx, + http.MethodPost, + fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token), + bytes.NewReader(buf), + ) if err != nil { return err }