#8 Code correction, refinement of pull request opening.

This commit is contained in:
Gorbunov Kirill Andreevich 2024-03-15 23:12:39 +03:00
parent f9fe7e183e
commit dc192ce1cc
4 changed files with 27 additions and 16 deletions

View File

@ -2,5 +2,6 @@ branches: [develop]
source: source:
type: gitea type: gitea
token: f9cc8a7894ba3ce8f5a15b589b4011d7f298fbd2 token: f9cc8a7894ba3ce8f5a15b589b4011d7f298fbd2
apiurl: https://git.unistack.org/kgorbunov apiurl: https://git.unistack.org/api/v1
repository: pkgdash repository: pkgdash
owner: kgorbunov

1
.gitignore vendored
View File

@ -30,3 +30,4 @@ cmd/pkgdash/pkgdash
cmd/pkgdashcli/pkgdashcli cmd/pkgdashcli/pkgdashcli
*.sqlite *.sqlite
*.db *.db
.gitea/pkgdashcli.yaml

View File

@ -12,6 +12,7 @@ type Source struct {
Token string `json:"token" yaml:"token"` Token string `json:"token" yaml:"token"`
APIURL string `json:"apiurl" yaml:"apiurl"` APIURL string `json:"apiurl" yaml:"apiurl"`
Repository string `json:"repository" yaml:"repository"` Repository string `json:"repository" yaml:"repository"`
Owner string `json:"owner" yaml:"owner"`
} }
func NewConfig() *Config { func NewConfig() *Config {

View File

@ -36,6 +36,7 @@ type Gitea struct {
PRTitle string PRTitle string
PRBody string PRBody string
Repository string Repository string
Owner string
} }
func NewGitea(cfg configcli.Config) *Gitea { func NewGitea(cfg configcli.Config) *Gitea {
@ -45,6 +46,7 @@ func NewGitea(cfg configcli.Config) *Gitea {
PRTitle: cfg.PullRequestTitle, PRTitle: cfg.PullRequestTitle,
PRBody: cfg.PullRequestBody, PRBody: cfg.PullRequestBody,
Repository: cfg.Source.Repository, 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) 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 // открытие гит репозитория с опцией обхода репозитория для нахождения .git
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true}) repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
if err != nil { 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) 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{ body := map[string]string{
"base": "refs/heads/develop", "base": "refs/heads/develop",
"body": wBody.String(), "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) 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 { if err != nil {
return err return err
} }