Co-authored-by: Vasiliy Tolstov <v.tolstov@unistack.org> Co-authored-by: Gorbunov Kirill Andreevich <kgorbunov@mtsbank.ru> Reviewed-on: #15 Co-authored-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru> Co-committed-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
This commit is contained in:
@@ -28,8 +28,8 @@ var ErrPRNotExist = errors.New("pull request does not exist")
|
||||
|
||||
type Gitea struct {
|
||||
URL string
|
||||
Username string
|
||||
Password string
|
||||
Username string
|
||||
Password string
|
||||
PRTitle string
|
||||
PRBody string
|
||||
Repository string
|
||||
@@ -40,8 +40,8 @@ type Gitea struct {
|
||||
func NewGitea(cfg configcli.Config) *Gitea {
|
||||
return &Gitea{
|
||||
URL: cfg.Source.APIURL,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
PRTitle: cfg.PullRequestTitle,
|
||||
PRBody: cfg.PullRequestBody,
|
||||
Repository: cfg.Source.Repository,
|
||||
@@ -105,10 +105,10 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
}
|
||||
//извлекаем ссылки с объектами из удаленного объекта??
|
||||
if err = repo.FetchContext(ctx, &git.FetchOptions{
|
||||
// Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
// Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v",err))
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err))
|
||||
} //обновляем репозиторий
|
||||
|
||||
var headRef *plumbing.Reference // вроде ссылка на гит
|
||||
@@ -140,8 +140,9 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
if err != nil {
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
|
||||
}
|
||||
defer checkout(wtree, *headRef)
|
||||
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -157,7 +158,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
logger.Info(ctx, fmt.Sprintf("update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
||||
|
||||
logger.Info(ctx, "reset worktree")
|
||||
if err = wtree.Reset(&git.ResetOptions{Commit: headRef.Hash(), Mode: git.HardReset}); err != nil {
|
||||
if err = wtree.Reset(&git.ResetOptions{Commit: headRef.Hash(), Mode: git.HardReset}); err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("failed to reset repo branch: %v", err))
|
||||
} //вроде меняем ветку todo вроде можно удалить
|
||||
|
||||
@@ -261,7 +262,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls", g.URL, g.Owner, g.Repository, g.Password),
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls", g.URL, g.Owner, g.Repository),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -269,7 +270,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", g.Password)
|
||||
req.Header.Add("Authorization", g.Password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -293,7 +294,7 @@ req.Header.Add("Authorization", g.Password)
|
||||
func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error {
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
|
||||
|
||||
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -313,7 +314,7 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
req, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Username, g.Password)
|
||||
req, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("failed to create request for delete the branch: %s, err: %s", branch, err))
|
||||
return err
|
||||
@@ -333,7 +334,7 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
var err error
|
||||
|
||||
if len(g.pulls) == 0 {
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -346,15 +347,15 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
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) {
|
||||
reqDel, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, pull.Head.Ref, g.Username, g.Password)
|
||||
reqDel, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, pull.Head.Ref, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("Error with create request for branch: %s, err: %s", branch, err))
|
||||
continue
|
||||
return err
|
||||
}
|
||||
rsp, err := http.DefaultClient.Do(reqDel)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("Error with do request for branch: %s, err: %s, code: %v", branch, err, rsp.StatusCode))
|
||||
continue //думаю что если не можем удалить ветку не стоит заканчивать работу, а перейти к следующей итерации
|
||||
return err
|
||||
}
|
||||
logger.Info(ctx, fmt.Sprintf("Old pr %s successful delete", pull.Head.Ref))
|
||||
} else {
|
||||
@@ -369,8 +370,6 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
return ErrPRNotExist
|
||||
}
|
||||
|
||||
logger.Info(ctx, fmt.Sprintf("update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
||||
|
||||
return g.RequestOpen(ctx, branch, path, mod)
|
||||
}
|
||||
|
||||
@@ -378,7 +377,7 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
var err error
|
||||
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -387,7 +386,7 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
|
||||
rMap := make(map[string]string)
|
||||
|
||||
for _, pull := range g.pulls {
|
||||
if !strings.HasPrefix(pull.Title, "Bump ") { //добавляем только реквесты бота по обновлению модулей
|
||||
if !strings.HasPrefix(pull.Title, "Bump ") || pull.Base.Ref != branch { //добавляем только реквесты бота по обновлению модулей
|
||||
continue
|
||||
}
|
||||
path = strings.Split(pull.Title, " ")[1] //todo Работет только для дефолтного шаблона
|
||||
@@ -404,7 +403,7 @@ func getVersions(s string) string {
|
||||
return version
|
||||
}
|
||||
|
||||
func DeleteBranch(ctx context.Context, url, owner, repo, branch, username, password string) (*http.Request, error) {
|
||||
func 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 {
|
||||
@@ -416,7 +415,7 @@ func DeleteBranch(ctx context.Context, url, owner, repo, branch, username, passw
|
||||
return req, err
|
||||
}
|
||||
|
||||
func GetPulls(ctx context.Context, url, owner, repo, username, password string) ([]*giteaPull, error) {
|
||||
func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaPull, error) {
|
||||
var pullsAll []*giteaPull
|
||||
page := 1
|
||||
|
||||
@@ -425,7 +424,7 @@ func GetPulls(ctx context.Context, url, owner, repo, username, password string)
|
||||
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),
|
||||
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
|
||||
@@ -433,7 +432,7 @@ func GetPulls(ctx context.Context, url, owner, repo, username, password string)
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", password)
|
||||
req.Header.Add("Authorization", password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req) // выполнение запроса
|
||||
if err != nil {
|
||||
@@ -464,3 +463,10 @@ req.Header.Add("Authorization", password)
|
||||
|
||||
return pullsAll, nil
|
||||
}
|
||||
|
||||
func checkout(w *git.Worktree, ref plumbing.Reference) {
|
||||
ctx := context.Background()
|
||||
if err := w.Reset(&git.ResetOptions{Commit: ref.Hash(), Mode: git.HardReset}); err != nil {
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user