397 lines
13 KiB
Go
397 lines
13 KiB
Go
|
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))
|
|||
|
}
|
|||
|
}
|