#19 reset HEAD
This commit is contained in:
48
internal/source/git/gogit.go
Normal file
48
internal/source/git/gogit.go
Normal file
@@ -0,0 +1,48 @@
|
||||
//go:build gogit
|
||||
|
||||
package git
|
||||
|
||||
/*
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
Checkout(ctx context.Context, hash string) error
|
||||
}
|
||||
|
||||
type repository struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func NewRepositoryFromURL(ctx context.Context, url string) (Repository, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
Branches() {
|
||||
refIter, err := repo.Branches() // получение веток
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to get branches", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
ref, err := refIter.Next()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
g.logger.Error(ctx, "ref iter error", err)
|
||||
return err
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("check %s == %s", ref.Name().Short(), branch))
|
||||
if ref.Name().Short() == branch {
|
||||
headRef = plumbing.NewHashReference(ref.Name(), ref.Hash())
|
||||
g.logger.Info(ctx, "headRef set to "+headRef.String())
|
||||
break
|
||||
}
|
||||
} // перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
|
||||
|
||||
refIter.Close()
|
||||
}
|
||||
|
||||
*/
|
262
internal/source/git/nogogit.go
Normal file
262
internal/source/git/nogogit.go
Normal file
@@ -0,0 +1,262 @@
|
||||
//go:build !gogit
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Branches() ([]*plumbing.Reference, error)
|
||||
// Auth(username string, password string) error
|
||||
FetchContext(ctx context.Context, opts *git.FetchOptions) error
|
||||
PushContext(ctx context.Context, opts *git.PushOptions) error
|
||||
Head() (*plumbing.Reference, error)
|
||||
Worktree() (Worktree, error)
|
||||
}
|
||||
|
||||
type Worktree interface {
|
||||
Checkout(*git.CheckoutOptions) error
|
||||
PullContext(ctx context.Context, opts *git.PullOptions) error
|
||||
Status() (git.Status, error)
|
||||
AddWithOptions(opts *git.AddOptions) error
|
||||
Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)
|
||||
Reset(opts *git.ResetOptions) error
|
||||
}
|
||||
|
||||
type repository struct {
|
||||
gocmd string
|
||||
path string
|
||||
// authUsername string
|
||||
// authPassword string
|
||||
}
|
||||
|
||||
func PlainOpenWithOptions(path string, opts *git.PlainOpenOptions) (Repository, error) {
|
||||
gopath, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &repository{path: path, gocmd: gopath}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (r *repository) Auth(username string, password string) error {
|
||||
r.authUsername = username
|
||||
r.authPassword = password
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (r *repository) Branches() ([]*plumbing.Reference, error) {
|
||||
var branches []*plumbing.Reference
|
||||
cmd := exec.Command(r.gocmd, "show-ref", "--branches")
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
br := bytes.NewBuffer(buf)
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF && line == "" {
|
||||
break
|
||||
} else if err != io.EOF && line == "" {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
return nil, fmt.Errorf("invalid fields %s", line)
|
||||
}
|
||||
branches = append(branches, plumbing.NewReferenceFromStrings(fields[1], fields[0]))
|
||||
}
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func (r *repository) FetchContext(ctx context.Context, opts *git.FetchOptions) error {
|
||||
args := []string{"fetch"}
|
||||
if opts.Force {
|
||||
args = append(args, "-f")
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, r.gocmd, args...)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repository) PushContext(ctx context.Context, opts *git.PushOptions) error {
|
||||
args := []string{"push"}
|
||||
|
||||
if opts.Force {
|
||||
args = append(args, "-f")
|
||||
}
|
||||
|
||||
/* TODO
|
||||
var refs []string
|
||||
for _, ref := range opts.RefSpecs {
|
||||
refs = append(refs, ref.String())
|
||||
}
|
||||
|
||||
args = append(args, strings.Join(refs, " "))
|
||||
*/
|
||||
cmd := exec.CommandContext(ctx, r.gocmd, args...)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repository) Head() (*plumbing.Reference, error) {
|
||||
var head *plumbing.Reference
|
||||
cmd := exec.Command(r.gocmd, "symbolic-ref", "--short", "HEAD")
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
br := bytes.NewBuffer(buf)
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF && line == "" {
|
||||
break
|
||||
} else if err != io.EOF && line == "" {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
return nil, fmt.Errorf("invalid fields %s", line)
|
||||
}
|
||||
head = plumbing.NewReferenceFromStrings("HEAD", fields[0])
|
||||
}
|
||||
return head, nil
|
||||
}
|
||||
|
||||
type worktree struct {
|
||||
gocmd string
|
||||
}
|
||||
|
||||
func (r *repository) Worktree() (Worktree, error) {
|
||||
return &worktree{gocmd: r.gocmd}, nil
|
||||
}
|
||||
|
||||
func (w *worktree) Checkout(opts *git.CheckoutOptions) error {
|
||||
args := []string{"checkout"}
|
||||
if opts.Create {
|
||||
args = append(args, "-b", opts.Branch.Short())
|
||||
}
|
||||
if opts.Force {
|
||||
args = append(args, "-f")
|
||||
}
|
||||
if opts.Hash.IsZero() {
|
||||
args = append(args, opts.Branch.Short())
|
||||
} else {
|
||||
args = append(args, opts.Hash.String())
|
||||
}
|
||||
cmd := exec.Command(w.gocmd, args...)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *worktree) Status() (git.Status, error) {
|
||||
return git.Status{}, nil
|
||||
}
|
||||
|
||||
func (w *worktree) Reset(opts *git.ResetOptions) error {
|
||||
args := []string{"reset"}
|
||||
if opts.Mode == git.HardReset {
|
||||
args = append(args, "--hard")
|
||||
}
|
||||
|
||||
args = append(args, opts.Commit.String())
|
||||
|
||||
cmd := exec.Command(w.gocmd, args...)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = buf
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *worktree) Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error) {
|
||||
cmd := exec.Command(w.gocmd, `commit`,
|
||||
fmt.Sprintf(`--author="%s <%s>"`, opts.Author.Name, opts.Author.Email),
|
||||
"-m", msg,
|
||||
fmt.Sprintf(`--date="%s"`, opts.Author.When.Format(`Mon Jan _2 15:04:05 2006 -0700`)),
|
||||
)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return plumbing.ZeroHash, fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
var head *plumbing.Reference
|
||||
cmd = exec.Command(w.gocmd, "show-ref", "HEAD")
|
||||
buf, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
br := bytes.NewBuffer(buf)
|
||||
for {
|
||||
line, err := br.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF && line == "" {
|
||||
break
|
||||
} else if err != io.EOF && line == "" {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
return plumbing.ZeroHash, fmt.Errorf("invalid fields %s", line)
|
||||
}
|
||||
head = plumbing.NewReferenceFromStrings("HEAD", fields[0])
|
||||
}
|
||||
|
||||
return head.Hash(), nil
|
||||
}
|
||||
|
||||
func (w *worktree) PullContext(ctx context.Context, opts *git.PullOptions) error {
|
||||
args := []string{"pull"}
|
||||
if opts.Force {
|
||||
args = append(args, "-f")
|
||||
}
|
||||
if opts.Depth != 0 {
|
||||
args = append(args, fmt.Sprintf("--depth=%d", opts.Depth))
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, w.gocmd, args...)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *worktree) AddWithOptions(opts *git.AddOptions) error {
|
||||
cmd := exec.Command(w.gocmd, "add", opts.Path)
|
||||
buf, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("output %s error %w", buf, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
676
internal/source/gitea/gitea.go
Normal file
676
internal/source/gitea/gitea.go
Normal file
@@ -0,0 +1,676 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
gitconfig "github.com/go-git/go-git/v5/config"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/configcli"
|
||||
"go.unistack.org/pkgdash/internal/modules"
|
||||
//gogit "go.unistack.org/pkgdash/internal/source/git"
|
||||
)
|
||||
|
||||
var ErrPRNotExist = errors.New("pull request does not exist")
|
||||
|
||||
type Gitea struct {
|
||||
logger logger.Logger
|
||||
URL string
|
||||
Username string
|
||||
Password string
|
||||
PRTitle string
|
||||
PRBody string
|
||||
Repository string
|
||||
Owner string
|
||||
pulls []*giteaPull
|
||||
}
|
||||
|
||||
func NewGitea(cfg configcli.Config, log logger.Logger) *Gitea {
|
||||
return &Gitea{
|
||||
logger: log,
|
||||
URL: cfg.Source.APIURL,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
PRTitle: cfg.PullRequestTitle,
|
||||
PRBody: cfg.PullRequestBody,
|
||||
Repository: cfg.Source.Repository,
|
||||
Owner: cfg.Source.Owner,
|
||||
}
|
||||
}
|
||||
|
||||
type giteaPull struct {
|
||||
URL string `json:"url"`
|
||||
Title string `json:"title"`
|
||||
Base struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"base"`
|
||||
Head struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"head"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (g *Gitea) Name() string {
|
||||
return "gitea"
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
|
||||
|
||||
var buf []byte
|
||||
var err error
|
||||
// создания шаблона названия для пулл реквеста
|
||||
tplTitle, err := template.New("pull_request_title").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
wTitle := bytes.NewBuffer(nil)
|
||||
// создания шаблона тела для пулл реквеста
|
||||
tplBody, err := template.New("pull_request_body").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
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 {
|
||||
g.logger.Error(ctx, "failed to execute template", err)
|
||||
return err
|
||||
}
|
||||
if err = tplBody.Execute(wBody, data); err != nil {
|
||||
g.logger.Error(ctx, "failed to execute template", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// открытие гит репозитория с опцией обхода репозитория для нахождения .git
|
||||
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
|
||||
}
|
||||
|
||||
wtree, headRef, err := g.fetchCheckout(ctx, repo, branch, path, mod)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to checkout", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = g.checkout(wtree, headRef)
|
||||
}()
|
||||
|
||||
if err = g.scopeUpdateDep(ctx, path, mod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.mod")
|
||||
if err = wtree.AddWithOptions(&git.AddOptions{Path: "go.mod"}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.sum")
|
||||
if err = wtree.AddWithOptions(&git.AddOptions{Path: "go.sum"}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
return err
|
||||
}
|
||||
|
||||
g.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 {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to commit: %v", err))
|
||||
return 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 как будто нужно переделать
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("try to push refspec %s", refspec))
|
||||
|
||||
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||
RefSpecs: []gitconfig.RefSpec{refspec},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, "failed to push repo branch", err)
|
||||
return err
|
||||
} // пытаемся за пушить изменения
|
||||
|
||||
rsp, err := g.postPullRequest(ctx, wBody, wTitle, branch, path, mod)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Вроде создаем новый реквест на создание пулл реквеста
|
||||
if rsp.StatusCode != http.StatusCreated {
|
||||
buf, _ = io.ReadAll(rsp.Body)
|
||||
return fmt.Errorf("unknown error: %s", buf)
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR create for %s-%s", path, mod.Version))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
|
||||
var err error
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
prExist := false
|
||||
var b string // Name of the branch to be deleted
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
|
||||
prExist = true
|
||||
b = pull.Head.Ref
|
||||
}
|
||||
}
|
||||
if !prExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("skip %s since pr does not exist", path))
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
req, err := g.DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to create request for delete the branch: %s, err: %s", branch, err))
|
||||
return err
|
||||
}
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to do request for delete the branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("Delete branch for %s successful", path))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
|
||||
var err error
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
prExist := false
|
||||
var pullId int64
|
||||
var targetBranch plumbing.ReferenceName
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
|
||||
g.logger.Info(ctx, fmt.Sprintf("don't skip %s since pr exist %s", path, pull.URL)) // todo
|
||||
tVersion := getVersions(pull.Head.Ref) // Надо взять просто из названия ветки последнюю версию
|
||||
if !modules.IsNewerVersion(tVersion, mod.Version, false) {
|
||||
g.logger.Debug(ctx, "The existing PR is relevant")
|
||||
return nil
|
||||
}
|
||||
prExist = true
|
||||
pullId = pull.ID
|
||||
targetBranch = plumbing.ReferenceName(pull.Head.Ref)
|
||||
}
|
||||
}
|
||||
if !prExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("skip %s since pr does not exist", path))
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
// создания шаблона названия для пулл реквеста
|
||||
tplTitle, err := template.New("pull_request_title").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
wTitle := bytes.NewBuffer(nil)
|
||||
// создания шаблона тела для пулл реквеста
|
||||
tplBody, err := template.New("pull_request_body").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
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 {
|
||||
g.logger.Error(ctx, "failed to execute template", err)
|
||||
return err
|
||||
}
|
||||
if err = tplBody.Execute(wBody, data); err != nil {
|
||||
g.logger.Error(ctx, "failed to execute template", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// открытие гит репозитория с опцией обхода репозитория для нахождения .git
|
||||
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
|
||||
}
|
||||
|
||||
wtree, headRef, err := g.fetchCheckout(ctx, repo, targetBranch.Short(), path, mod)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to checkout", err)
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = g.checkout(wtree, headRef)
|
||||
}()
|
||||
|
||||
if err = g.scopeUpdateDep(ctx, path, mod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.mod")
|
||||
if err = wtree.AddWithOptions(&git.AddOptions{Path: "go.mod"}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.sum")
|
||||
if err = wtree.AddWithOptions(&git.AddOptions{Path: "go.sum"}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
return err
|
||||
}
|
||||
|
||||
g.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 {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to commit: %v", err))
|
||||
return 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 как будто нужно переделать
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("try to push refspec %s", refspec))
|
||||
|
||||
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||
//RefSpecs: []gitconfig.RefSpec{refspec},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, "failed to push repo branch", err)
|
||||
return err
|
||||
} // пытаемся за пушить изменения
|
||||
|
||||
err = g.patchPullRequest(ctx, wBody, wTitle, pullId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR update for %s-%s", path, mod.Version))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]string, error) {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
var err error
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var path string
|
||||
rMap := make(map[string]string)
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if !strings.HasPrefix(pull.Title, "Bump ") || pull.Base.Ref != branch { // добавляем только реквесты бота по обновлению модулей
|
||||
continue
|
||||
}
|
||||
path = strings.Split(pull.Title, " ")[1] // todo Работет только для дефолтного шаблона
|
||||
rMap[path] = pull.Title
|
||||
}
|
||||
return rMap, nil
|
||||
}
|
||||
|
||||
func getVersions(s string) string {
|
||||
re := regexp.MustCompile("[vV][0-9]+\\.[0-9]+\\.[0-9]+")
|
||||
|
||||
version := re.FindString(s)
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
func (g *Gitea) DeleteBranch(ctx context.Context, url, owner, repo, branch, password string) (*http.Request, error) {
|
||||
var buf []byte
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/api/v1/repos/%s/%s/branches/%s", url, owner, repo, branch), bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func (g *Gitea) GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaPull, error) {
|
||||
var pullsAll []*giteaPull
|
||||
page := 1
|
||||
|
||||
for {
|
||||
pulls := make([]*giteaPull, 0, 10)
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls?state=open&page=%v", url, owner, repo, page),
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} // вроде запроса к репозиторию
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
|
||||
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 {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
|
||||
return nil, err
|
||||
}
|
||||
pullsAll = append(pullsAll, pulls...)
|
||||
page++
|
||||
case http.StatusNotFound:
|
||||
g.logger.Info(ctx, fmt.Sprintf("pull-request is not exist for %s", repo))
|
||||
return nil, ErrPRNotExist
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown error: %s", buf)
|
||||
}
|
||||
|
||||
if len(pulls) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return pullsAll, nil
|
||||
}
|
||||
|
||||
func (g *Gitea) checkout(w *git.Worktree, ref *plumbing.Reference) error {
|
||||
ctx := context.Background()
|
||||
g.logger.Debug(ctx, "checkout: "+ref.String())
|
||||
|
||||
if err := w.Checkout(&git.CheckoutOptions{
|
||||
Branch: ref.Name(),
|
||||
Create: false,
|
||||
Force: true,
|
||||
Keep: false,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, "failed to reset", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Gitea) fetchCheckout(ctx context.Context, repo *git.Repository, branch, path string, mod modules.Update) (*git.Worktree, *plumbing.Reference, error) {
|
||||
// обновляем ветки
|
||||
if err := repo.FetchContext(ctx, &git.FetchOptions{
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Error(ctx, "failed to fetch repo", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var headRef *plumbing.Reference
|
||||
|
||||
branches, err := repo.Branches()
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "cant get repo branch", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
ref, err := branches.Next()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if ref.Name().Short() == branch {
|
||||
//Получаем ссылку на нужную ветку
|
||||
headRef = ref
|
||||
g.logger.Info(ctx, "headRef set to "+headRef.String())
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if headRef == nil {
|
||||
g.logger.Error(ctx, "failed to get repo branch head")
|
||||
return nil, nil, err
|
||||
} // Не получили нужную ветку
|
||||
|
||||
g.logger.Info(ctx, "repo head "+headRef.String())
|
||||
|
||||
wtree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to get worktree", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err = wtree.Reset(&git.ResetOptions{
|
||||
Mode: git.HardReset,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, "reset work_tree error: ", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
g.logger.Error(ctx, "GetPulls error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var pullExist bool
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) && (strings.Contains(pull.Base.Ref, branch) || strings.Contains(pull.Head.Ref, branch)) {
|
||||
pullExist = true
|
||||
} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
||||
|
||||
wstatus, err := wtree.Status()
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to get worktree status", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree status "+wstatus.String())
|
||||
|
||||
if err = wtree.PullContext(ctx, &git.PullOptions{
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
// Depth: 1,
|
||||
// RemoteURL :
|
||||
ReferenceName: headRef.Name(),
|
||||
Force: true,
|
||||
RemoteName: "origin",
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to pull repo: %v", err)) // подтягиваем изменения с удаленого репозитория
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("checkout ref %s", headRef))
|
||||
|
||||
if pullExist {
|
||||
if err = wtree.Checkout(&git.CheckoutOptions{
|
||||
Branch: headRef.Name(),
|
||||
Create: false,
|
||||
Force: true,
|
||||
}); err != nil && err != git.ErrBranchExists {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to checkout tree: %v", err))
|
||||
return nil, nil, err
|
||||
} //переходим на существующею
|
||||
} else {
|
||||
if err = wtree.Checkout(&git.CheckoutOptions{
|
||||
Hash: headRef.Hash(),
|
||||
Branch: plumbing.NewBranchReferenceName(fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version)),
|
||||
Create: true,
|
||||
Force: true,
|
||||
}); err != nil && err != git.ErrBranchExists {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to checkout tree: %v", err))
|
||||
return nil, nil, err
|
||||
} // создаем новую ветку
|
||||
}
|
||||
return wtree, headRef, nil
|
||||
}
|
||||
|
||||
func (g *Gitea) postPullRequest(ctx context.Context, wBody, wTitle *bytes.Buffer, branch, path string, mod modules.Update) (*http.Response, error) {
|
||||
body := map[string]string{
|
||||
"base": branch,
|
||||
"body": wBody.String(),
|
||||
"head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version),
|
||||
"title": wTitle.String(),
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("raw body: %#+v", body))
|
||||
|
||||
buf, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to marshal", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("marshal body: %s", buf))
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls", g.URL, g.Owner, g.Repository),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "http request error", err)
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+g.Password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to call http request", err)
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
func (g *Gitea) patchPullRequest(ctx context.Context, wBody, wTitle *bytes.Buffer, indexPR int64) error {
|
||||
body := map[string]string{
|
||||
"body": wBody.String(),
|
||||
"title": wTitle.String(),
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("raw body: %#+v", body))
|
||||
|
||||
buf, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to marshal", err)
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("marshal body: %s", buf))
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPatch,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls/%d", g.URL, g.Owner, g.Repository, indexPR),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "http request error", err)
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+g.Password)
|
||||
|
||||
_, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, "failed to call http request", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitea) scopeUpdateDep(ctx context.Context, path string, mod modules.Update) error {
|
||||
epath, err := exec.LookPath("go")
|
||||
if errors.Is(err, exec.ErrDot) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to find go command: %v", err))
|
||||
} // ищем go файл
|
||||
|
||||
var cmd *exec.Cmd
|
||||
var out []byte
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-droprequire=%s", mod.Module.Path))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to run go mod edit: %s err: %v", out, err))
|
||||
return err
|
||||
}
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to run go mod edit: %s err: %v", out, err))
|
||||
return err
|
||||
} // пытаемся выполнить команду go mod edit с новой версией модуля
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "tidy")
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to run go mod tidy: %s err: %v", out, err))
|
||||
return err
|
||||
} // пытаемся выполнить команду go mod tidy пытаемся подтянуть новую версию модуля
|
||||
|
||||
return nil
|
||||
}
|
396
internal/source/github/github.go
Normal file
396
internal/source/github/github.go
Normal file
@@ -0,0 +1,396 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
gitconfig "github.com/go-git/go-git/v5/config"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/configcli"
|
||||
"go.unistack.org/pkgdash/internal/modules"
|
||||
)
|
||||
|
||||
var ErrPRNotExist = errors.New("pull request does not exist")
|
||||
|
||||
type Github struct {
|
||||
logger logger.Logger
|
||||
URL string
|
||||
Username string
|
||||
Password string
|
||||
PRTitle string
|
||||
PRBody string
|
||||
Repository string
|
||||
Owner string
|
||||
pulls []*githubPull
|
||||
baseRef *plumbing.Reference
|
||||
}
|
||||
|
||||
func NewGithub(cfg configcli.Config, log logger.Logger) *Github {
|
||||
return &Github{
|
||||
logger: log,
|
||||
URL: cfg.Source.APIURL,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
PRTitle: cfg.PullRequestTitle,
|
||||
PRBody: cfg.PullRequestBody,
|
||||
Repository: cfg.Source.Repository,
|
||||
Owner: cfg.Source.Owner,
|
||||
}
|
||||
}
|
||||
|
||||
type githubPull struct {
|
||||
URL string `json:"url"`
|
||||
Title string `json:"title"`
|
||||
Base struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"base"`
|
||||
Head struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"head"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (g *Github) Name() string {
|
||||
return "github"
|
||||
}
|
||||
|
||||
func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
|
||||
|
||||
var buf []byte
|
||||
var err error
|
||||
// создания шаблона названия для пулл реквеста
|
||||
tplTitle, err := template.New("pull_request_title").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
wTitle := bytes.NewBuffer(nil)
|
||||
// создания шаблона тела для пулл реквеста
|
||||
tplBody, err := template.New("pull_request_body").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to execute template: %v", err))
|
||||
}
|
||||
if err = tplBody.Execute(wBody, data); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to execute template: %v", err))
|
||||
}
|
||||
|
||||
// открытие гит репозитория с опцией обхода репозитория для нахождения .git
|
||||
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
|
||||
}
|
||||
// извлекаем ссылки с объектами из удаленного объекта??
|
||||
if err = repo.FetchContext(ctx, &git.FetchOptions{
|
||||
// Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err))
|
||||
} // обновляем репозиторий
|
||||
|
||||
var headRef *plumbing.Reference // вроде ссылка на гит
|
||||
|
||||
if g.baseRef == nil {
|
||||
g.baseRef, err = repo.Head()
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
refIter, err := repo.Branches() // получение веток
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
|
||||
return err
|
||||
}
|
||||
for {
|
||||
ref, err := refIter.Next()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if ref.Name().Short() == branch { // todo вот тут возможно нужно переделать
|
||||
headRef = ref
|
||||
break
|
||||
}
|
||||
} // перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
|
||||
refIter.Close()
|
||||
|
||||
if headRef == nil {
|
||||
g.logger.Fatal(ctx, "failed to get repo branch head")
|
||||
return err
|
||||
} // Не получили нужную ветку
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("repo head %s", headRef))
|
||||
|
||||
wtree, err := repo.Worktree() // todo вроде рабочее дерево не нужно
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
|
||||
}
|
||||
defer g.checkout(*wtree, *g.baseRef)
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) && strings.Contains(pull.Base.Ref, branch) {
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR for %s exists %s, call RequestUpdate", path, pull.URL))
|
||||
return g.RequestUpdate(ctx, branch, path, mod)
|
||||
} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
||||
|
||||
g.logger.Info(ctx, "reset worktree")
|
||||
if err = wtree.Reset(&git.ResetOptions{Commit: headRef.Hash(), Mode: git.HardReset}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to reset repo branch: %v", err))
|
||||
} // вроде меняем ветку todo вроде можно удалить
|
||||
|
||||
if err = wtree.PullContext(ctx, &git.PullOptions{
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Depth: 1,
|
||||
// RemoteURL :
|
||||
Force: true,
|
||||
RemoteName: "origin",
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to pull repo: %v", err)) // подтягиваем изменения с удаленого репозитория
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("checkout ref %s", headRef))
|
||||
if err = wtree.Checkout(&git.CheckoutOptions{
|
||||
Hash: headRef.Hash(),
|
||||
Branch: plumbing.NewBranchReferenceName(fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version)),
|
||||
Create: true,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to checkout tree: %v", err))
|
||||
return err
|
||||
} // создаем новую ветку
|
||||
|
||||
epath, err := exec.LookPath("go")
|
||||
if errors.Is(err, exec.ErrDot) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to find go command: %v", err))
|
||||
} // ищем go файл
|
||||
|
||||
var cmd *exec.Cmd
|
||||
var out []byte
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-droprequire=%s", mod.Module.Path))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to run go mod edit: %s err: %v", out, err))
|
||||
}
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to run go mod tidy: %s err: %v", out, err))
|
||||
} // пытаемся выполнить команду go mod tidy пытаемся подтянуть новую версию модуля
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.mod")
|
||||
if _, err = wtree.Add("go.mod"); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.sum")
|
||||
if _, err = wtree.Add("go.sum"); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
}
|
||||
|
||||
g.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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to commit: %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 как будто нужно переделать
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("try to push refspec %s", refspec))
|
||||
|
||||
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||
RefSpecs: []gitconfig.RefSpec{refspec},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to push repo branch: %v", err))
|
||||
} // пытаемся за пушить изменения
|
||||
|
||||
body := map[string]string{
|
||||
"base": branch,
|
||||
"body": wBody.String(),
|
||||
"head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version),
|
||||
"title": wTitle.String(),
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("raw body: %#+v", body))
|
||||
|
||||
buf, err = json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("marshal body: %s", buf))
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls", g.URL, g.Owner, g.Repository),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+g.Password)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR create for %s-%s", path, mod.Version))
|
||||
|
||||
repo, err = git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Github) RequestClose(ctx context.Context, branch string, path string) error {
|
||||
return fmt.Errorf("implement me")
|
||||
}
|
||||
|
||||
func (g *Github) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
return fmt.Errorf("implement me")
|
||||
}
|
||||
|
||||
func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
var err error
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var path string
|
||||
rMap := make(map[string]string)
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if !strings.HasPrefix(pull.Title, "Bump ") || pull.Base.Ref != branch { // добавляем только реквесты бота по обновлению модулей
|
||||
continue
|
||||
}
|
||||
path = strings.Split(pull.Title, " ")[1] // todo Работет только для дефолтного шаблона
|
||||
rMap[path] = pull.Title
|
||||
}
|
||||
return rMap, nil
|
||||
}
|
||||
|
||||
func (g *Github) GetPulls(ctx context.Context, url, owner, repo, password string) ([]*githubPull, error) {
|
||||
var pullsAll []*githubPull
|
||||
page := 1
|
||||
|
||||
for {
|
||||
pulls := make([]*githubPull, 0, 10)
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls?state=open&page=%v", url, owner, repo, page),
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} // вроде запроса к репозиторию
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
|
||||
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 {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
|
||||
return nil, err
|
||||
}
|
||||
pullsAll = append(pullsAll, pulls...)
|
||||
page++
|
||||
case http.StatusNotFound:
|
||||
g.logger.Info(ctx, fmt.Sprintf("pull-request is not exist for %s", repo))
|
||||
return nil, ErrPRNotExist
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown error: %s", buf)
|
||||
}
|
||||
|
||||
if len(pulls) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return pullsAll, nil
|
||||
}
|
||||
|
||||
func (g *Github) checkout(w git.Worktree, ref plumbing.Reference) {
|
||||
ctx := context.Background()
|
||||
g.logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
|
||||
|
||||
if err := w.Checkout(&git.CheckoutOptions{
|
||||
Branch: ref.Name(),
|
||||
Create: false,
|
||||
Force: true,
|
||||
Keep: false,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err))
|
||||
}
|
||||
}
|
522
internal/source/gitlab/gitlab.go
Normal file
522
internal/source/gitlab/gitlab.go
Normal file
@@ -0,0 +1,522 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
gitconfig "github.com/go-git/go-git/v5/config"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/configcli"
|
||||
"go.unistack.org/pkgdash/internal/modules"
|
||||
)
|
||||
|
||||
var ErrPRNotExist = errors.New("pull request does not exist")
|
||||
|
||||
type Gitlab struct {
|
||||
logger logger.Logger
|
||||
URL string
|
||||
Username string
|
||||
Password string
|
||||
PRTitle string
|
||||
PRBody string
|
||||
Repository string
|
||||
RepositoryId string
|
||||
Owner string
|
||||
pulls []*gitlabPull
|
||||
baseRef *plumbing.Reference
|
||||
}
|
||||
|
||||
func NewGitlab(cfg configcli.Config, log logger.Logger) *Gitlab {
|
||||
return &Gitlab{
|
||||
logger: log,
|
||||
URL: cfg.Source.APIURL,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
PRTitle: cfg.PullRequestTitle,
|
||||
PRBody: cfg.PullRequestBody,
|
||||
Repository: cfg.Source.Repository,
|
||||
Owner: cfg.Source.Owner,
|
||||
}
|
||||
}
|
||||
|
||||
type gitlabPull struct {
|
||||
URL string `json:"web_url"`
|
||||
Title string `json:"title"`
|
||||
Target string `json:"target_branch"`
|
||||
Source string `json:"source_branch"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type gitlabProject struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (g *Gitlab) Name() string {
|
||||
return "gitlab"
|
||||
}
|
||||
|
||||
func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
|
||||
|
||||
var buf []byte
|
||||
var err error
|
||||
// создания шаблона названия для пулл реквеста
|
||||
tplTitle, err := template.New("pull_request_title").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
wTitle := bytes.NewBuffer(nil)
|
||||
// создания шаблона тела для пулл реквеста
|
||||
tplBody, err := template.New("pull_request_body").Parse(g.PRTitle)
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to parse template: %v", err))
|
||||
}
|
||||
|
||||
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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to execute template: %v", err))
|
||||
}
|
||||
if err = tplBody.Execute(wBody, data); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to execute template: %v", err))
|
||||
}
|
||||
|
||||
// открытие гит репозитория с опцией обхода репозитория для нахождения .git
|
||||
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to open repo: %v", err))
|
||||
}
|
||||
// извлекаем ссылки с объектами из удаленного объекта??
|
||||
if err = repo.FetchContext(ctx, &git.FetchOptions{
|
||||
// Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err))
|
||||
} // обновляем репозиторий
|
||||
|
||||
var headRef *plumbing.Reference // вроде ссылка на гит
|
||||
|
||||
if g.baseRef == nil {
|
||||
g.baseRef, err = repo.Head()
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
refIter, err := repo.Branches() // получение веток
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
|
||||
}
|
||||
for {
|
||||
ref, err := refIter.Next()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if ref.Name().Short() == branch { // todo вот тут возможно нужно переделать
|
||||
headRef = ref
|
||||
break
|
||||
}
|
||||
} // перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
|
||||
refIter.Close()
|
||||
|
||||
if headRef == nil {
|
||||
g.logger.Fatal(ctx, "failed to get repo branch head")
|
||||
return err
|
||||
} // Не получили нужную ветку
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("repo head %s", headRef))
|
||||
|
||||
wtree, err := repo.Worktree() // todo вроде рабочее дерево не нужно
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
|
||||
}
|
||||
defer g.checkout(*wtree, *g.baseRef)
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) {
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR for %s exists %s, call RequestUpdate", path, pull.URL))
|
||||
return g.RequestUpdate(ctx, branch, path, mod)
|
||||
} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
||||
|
||||
sourceBranch := fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version)
|
||||
|
||||
g.logger.Info(ctx, "reset worktree")
|
||||
if err = wtree.Reset(&git.ResetOptions{Commit: headRef.Hash(), Mode: git.HardReset}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to reset repo branch: %v", err))
|
||||
}
|
||||
|
||||
if err = wtree.PullContext(ctx, &git.PullOptions{
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Depth: 1,
|
||||
// RemoteURL :
|
||||
Force: true,
|
||||
RemoteName: "origin",
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to pull repo: %v", err)) // подтягиваем изменения с удаленого репозитория
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("checkout ref %s", headRef))
|
||||
if err = wtree.Checkout(&git.CheckoutOptions{
|
||||
Hash: headRef.Hash(),
|
||||
Branch: plumbing.NewBranchReferenceName(sourceBranch),
|
||||
Create: true,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to checkout tree: %v", err))
|
||||
return err
|
||||
} // создаем новую ветку
|
||||
|
||||
epath, err := exec.LookPath("go")
|
||||
if errors.Is(err, exec.ErrDot) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to find go command: %v", err))
|
||||
} // ищем go файл
|
||||
|
||||
var cmd *exec.Cmd
|
||||
var out []byte
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-droprequire=%s", mod.Module.Path))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to run go mod edit: %s err: %v", out, err))
|
||||
}
|
||||
|
||||
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
|
||||
if out, err = cmd.CombinedOutput(); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to run go mod tidy: %s err: %v", out, err))
|
||||
} // пытаемся выполнить команду go mod tidy пытаемся подтянуть новую версию модуля
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.mod")
|
||||
if _, err = wtree.Add("go.mod"); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, "worktree add go.sum")
|
||||
if _, err = wtree.Add("go.sum"); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to add file: %v", err))
|
||||
}
|
||||
|
||||
g.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 {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to commit: %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 как будто нужно переделать
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("try to push refspec %s", refspec))
|
||||
|
||||
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||
RefSpecs: []gitconfig.RefSpec{refspec},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
g.logger.Fatal(ctx, fmt.Sprintf("failed to push repo branch: %v", err))
|
||||
} // пытаемся за пушить изменения
|
||||
|
||||
body := map[string]string{
|
||||
"id": g.RepositoryId,
|
||||
"source_branch": sourceBranch,
|
||||
"target_branch": branch,
|
||||
"title": wTitle.String(),
|
||||
"description": wBody.String(),
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("raw body: %#+v", body))
|
||||
|
||||
buf, err = json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("marshal body: %s", buf))
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("https://%s/api/v4/projects/%s/merge_requests", g.URL, g.RepositoryId),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+g.Password)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR create for %s-%s", path, mod.Version))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
|
||||
var err error
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
prExist := false
|
||||
var b string // Name of the branch to be deleted
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) {
|
||||
g.logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
|
||||
prExist = true
|
||||
b = pull.Source
|
||||
}
|
||||
}
|
||||
if !prExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("skip %s since pr does not exist", path))
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
req, err := g.DeleteBranch(ctx, g.URL, g.RepositoryId, b, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to create request for delete the branch: %s, err: %s", branch, err))
|
||||
return err
|
||||
}
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to do request for delete the branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
|
||||
return err
|
||||
}
|
||||
|
||||
g.logger.Info(ctx, fmt.Sprintf("Delete branch for %s successful", path))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
|
||||
var err error
|
||||
|
||||
g.RepositoryId, err = g.GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil || g.RepositoryId == "" {
|
||||
return fmt.Errorf("project id is empty")
|
||||
}
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
}
|
||||
|
||||
prExist := false
|
||||
for _, pull := range g.pulls {
|
||||
if strings.Contains(pull.Title, path) {
|
||||
g.logger.Info(ctx, fmt.Sprintf("don't skip %s since pr exist %s", path, pull.URL)) // todo
|
||||
tVersion := getVersions(pull.Source) // Надо взять просто из названия ветки последнюю версию
|
||||
if modules.IsNewerVersion(tVersion, mod.Version, false) {
|
||||
reqDel, err := g.DeleteBranch(ctx, g.URL, g.RepositoryId, pull.Source, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("Error with create request for branch: %s, err: %s", branch, err))
|
||||
return err
|
||||
}
|
||||
rsp, err := http.DefaultClient.Do(reqDel)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("Error with do request for branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
|
||||
return err
|
||||
}
|
||||
g.logger.Info(ctx, fmt.Sprintf("Old pr %s successful delete", pull.Source))
|
||||
} else {
|
||||
g.logger.Debug(ctx, "The existing PR is relevant")
|
||||
return nil
|
||||
}
|
||||
prExist = true
|
||||
}
|
||||
}
|
||||
if !prExist {
|
||||
g.logger.Error(ctx, fmt.Sprintf("skip %s since pr does not exist", path))
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
return g.RequestOpen(ctx, branch, path, mod) // todo это мне не нравится
|
||||
}
|
||||
|
||||
func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]string, error) {
|
||||
g.logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
var err error
|
||||
|
||||
g.RepositoryId, err = g.GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil || g.RepositoryId == "" {
|
||||
return nil, fmt.Errorf("project id is empty")
|
||||
}
|
||||
|
||||
g.pulls, err = g.GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||
if err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var path string
|
||||
rMap := make(map[string]string)
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if !strings.HasPrefix(pull.Title, "Bump ") { // добавляем только реквесты бота по обновлению модулей
|
||||
continue
|
||||
}
|
||||
path = strings.Split(pull.Title, " ")[1] // todo Работет только для дефолтного шаблона
|
||||
rMap[path] = pull.Title
|
||||
}
|
||||
return rMap, nil
|
||||
}
|
||||
|
||||
func getVersions(s string) string {
|
||||
re := regexp.MustCompile("[vV][0-9]+\\.[0-9]+\\.[0-9]+")
|
||||
|
||||
version := re.FindString(s)
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
func (g *Gitlab) DeleteBranch(ctx context.Context, url, projectId, branch, password string) (*http.Request, error) {
|
||||
var buf []byte
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/api/v4/projects/%s/repository/branches/%s", url, projectId, branch), bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func (g *Gitlab) GetPulls(ctx context.Context, url, projectId, branch, password string) ([]*gitlabPull, error) {
|
||||
pulls := make([]*gitlabPull, 0, 10)
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("https://%s/api/v4/projects/%s/merge_requests?state=opened&target_branch=%s", url, projectId, branch),
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} // вроде запроса к репозиторию
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
|
||||
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 {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
|
||||
return nil, err
|
||||
}
|
||||
return pulls, nil
|
||||
case http.StatusNotFound:
|
||||
g.logger.Info(ctx, fmt.Sprintf("pull-request is not exist for %s", projectId))
|
||||
return nil, ErrPRNotExist
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown error: %s", buf)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gitlab) GetRepoID(ctx context.Context, url, owner, repo, password string) (rId string, err error) {
|
||||
var buf []byte
|
||||
projects := make([]*gitlabProject, 0, 10)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/api/v4/users/%s/projects?owned=true", url, owner), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
buf, _ = io.ReadAll(rsp.Body)
|
||||
|
||||
switch rsp.StatusCode {
|
||||
case http.StatusOK:
|
||||
if err = json.Unmarshal(buf, &projects); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
|
||||
}
|
||||
for _, p := range projects {
|
||||
if p.Name == repo {
|
||||
rId = strconv.Itoa(int(p.Id))
|
||||
}
|
||||
}
|
||||
return
|
||||
default:
|
||||
return rId, fmt.Errorf("unknown error: %s", buf)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gitlab) checkout(w git.Worktree, ref plumbing.Reference) {
|
||||
ctx := context.Background()
|
||||
g.logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
|
||||
|
||||
if err := w.Checkout(&git.CheckoutOptions{
|
||||
Branch: ref.Name(),
|
||||
Create: false,
|
||||
Force: true,
|
||||
Keep: false,
|
||||
}); err != nil {
|
||||
g.logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err))
|
||||
}
|
||||
}
|
44
internal/source/gogs/gogs.go
Normal file
44
internal/source/gogs/gogs.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package gogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/configcli"
|
||||
"go.unistack.org/pkgdash/internal/modules"
|
||||
)
|
||||
|
||||
type Gogs struct {
|
||||
logger logger.Logger
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func NewGogs(cfg configcli.Config, log logger.Logger) *Gogs {
|
||||
return &Gogs{
|
||||
logger: log,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gogs) Name() string {
|
||||
return "gogs"
|
||||
}
|
||||
|
||||
func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
return fmt.Errorf("implement me")
|
||||
}
|
||||
|
||||
func (g *Gogs) RequestClose(ctx context.Context, branch string, path string) error {
|
||||
return fmt.Errorf("implement me")
|
||||
}
|
||||
|
||||
func (g *Gogs) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
return fmt.Errorf("implement me")
|
||||
}
|
||||
|
||||
func (g *Gogs) RequestList(ctx context.Context, branch string) (map[string]string, error) {
|
||||
return nil, fmt.Errorf("implement me")
|
||||
}
|
35
internal/source/source.go
Normal file
35
internal/source/source.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/configcli"
|
||||
"go.unistack.org/pkgdash/internal/modules"
|
||||
"go.unistack.org/pkgdash/internal/source/gitea"
|
||||
"go.unistack.org/pkgdash/internal/source/github"
|
||||
"go.unistack.org/pkgdash/internal/source/gitlab"
|
||||
"go.unistack.org/pkgdash/internal/source/gogs"
|
||||
)
|
||||
|
||||
type SourceControl interface {
|
||||
Name() string
|
||||
RequestOpen(ctx context.Context, 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
|
||||
RequestList(ctx context.Context, branch string) (map[string]string, error)
|
||||
}
|
||||
|
||||
func NewSourceControl(cfg configcli.Config, log logger.Logger) SourceControl {
|
||||
switch cfg.Source.TypeGit {
|
||||
case "github":
|
||||
return github.NewGithub(cfg, log)
|
||||
case "gitlab":
|
||||
return gitlab.NewGitlab(cfg, log)
|
||||
case "gitea":
|
||||
return gitea.NewGitea(cfg, log)
|
||||
case "gogs":
|
||||
return gogs.NewGogs(cfg, log)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user