#8 Checkout, auth. #16

Closed
kgorbunov wants to merge 104 commits from kgorbunov:dev into master
4 changed files with 99 additions and 38 deletions
Showing only changes of commit 8fc16a2e3b - Show all commits

View File

@ -3,6 +3,7 @@ branches:
source: source:
type: gitea type: gitea
apiurl: git.unistack.org apiurl: git.unistack.org
repository: pkgdash
update_opt: update_opt:
pre: false pre: false
major: false major: false

View File

@ -35,6 +35,7 @@ type Gitea struct {
Repository string Repository string
Owner string Owner string
pulls []*giteaPull pulls []*giteaPull
baseRef *plumbing.Reference
} }
func NewGitea(cfg configcli.Config) *Gitea { func NewGitea(cfg configcli.Config) *Gitea {
@ -105,13 +106,21 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
} }
//извлекаем ссылки с объектами из удаленного объекта?? //извлекаем ссылки с объектами из удаленного объекта??
if err = repo.FetchContext(ctx, &git.FetchOptions{ 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, Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate { }); 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 // вроде ссылка на гит var headRef *plumbing.Reference // вроде ссылка на гит
if g.baseRef == nil {
g.baseRef, err = repo.Head()
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
}
}
refIter, err := repo.Branches() //получение веток refIter, err := repo.Branches() //получение веток
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
@ -140,10 +149,10 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
} }
defer checkout(wtree, *headRef) defer checkout(wtree, *g.baseRef)
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil && err != ErrPRNotExist { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
} }
@ -270,7 +279,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", g.Password) req.Header.Add("Authorization", "Bearer "+g.Password)
rsp, err := http.DefaultClient.Do(req) rsp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -293,8 +302,9 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error { func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) error {
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path)) logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
var err error
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
@ -302,7 +312,7 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
prExist := false prExist := false
var b string // Name of the branch to be deleted var b string // Name of the branch to be deleted
for _, pull := range pulls { for _, pull := range g.pulls {
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch { if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL)) logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
prExist = true prExist = true
@ -333,13 +343,11 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path)) logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
var err error var err error
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
} }
}
prExist := false prExist := false
for _, pull := range g.pulls { for _, pull := range g.pulls {
@ -379,6 +387,7 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return nil, err return nil, err
} }
@ -411,7 +420,7 @@ func DeleteBranch(ctx context.Context, url, owner, repo, branch, password string
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password) req.Header.Add("Authorization", "Bearer "+password)
return req, err return req, err
} }
@ -432,7 +441,7 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaP
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password) req.Header.Add("Authorization", "Bearer "+password)
rsp, err := http.DefaultClient.Do(req) // выполнение запроса rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil { if err != nil {
@ -466,7 +475,9 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaP
func checkout(w *git.Worktree, ref plumbing.Reference) { func checkout(w *git.Worktree, ref plumbing.Reference) {
ctx := context.Background() ctx := context.Background()
if err := w.Reset(&git.ResetOptions{Commit: ref.Hash(), Mode: git.HardReset}); err != nil { logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
if err := w.Checkout(&git.CheckoutOptions{Hash: ref.Hash()}); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err))
} }
} }

View File

@ -34,6 +34,7 @@ type Github struct {
Repository string Repository string
Owner string Owner string
pulls []*githubPull pulls []*githubPull
baseRef *plumbing.Reference
} }
func NewGithub(cfg configcli.Config) *Github { func NewGithub(cfg configcli.Config) *Github {
@ -111,6 +112,14 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
} //обновляем репозиторий } //обновляем репозиторий
var headRef *plumbing.Reference // вроде ссылка на гит var headRef *plumbing.Reference // вроде ссылка на гит
if g.baseRef == nil {
g.baseRef, err = repo.Head()
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
}
}
refIter, err := repo.Branches() //получение веток refIter, err := repo.Branches() //получение веток
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
@ -139,9 +148,10 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
} }
defer checkout(wtree, *g.baseRef)
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil && err != ErrPRNotExist { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
} }
@ -268,7 +278,7 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", g.Password) req.Header.Add("Authorization", "Bearer "+g.Password)
rsp, err := http.DefaultClient.Do(req) rsp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -295,7 +305,26 @@ func (g *Github) RequestUpdate(ctx context.Context, branch string, path string,
return fmt.Errorf("implement me") return fmt.Errorf("implement me")
} }
func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) { func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, fmt.Errorf("implement me") logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
var err error
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 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 GetPulls(ctx context.Context, url, owner, repo, password string) ([]*githubPull, error) { func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*githubPull, error) {
@ -315,7 +344,7 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password) req.Header.Add("Authorization", "Bearer "+password)
rsp, err := http.DefaultClient.Do(req) // выполнение запроса rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil { if err != nil {
@ -346,3 +375,12 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github
return pullsAll, nil return pullsAll, nil
} }
func checkout(w *git.Worktree, ref plumbing.Reference) {
ctx := context.Background()
logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
if err := w.Checkout(&git.CheckoutOptions{Hash: ref.Hash()}); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err))
}
}

View File

@ -36,6 +36,7 @@ type Gitlab struct {
RepositoryId string RepositoryId string
Owner string Owner string
pulls []*gitlabPull pulls []*gitlabPull
baseRef *plumbing.Reference
} }
func NewGitlab(cfg configcli.Config) *Gitlab { func NewGitlab(cfg configcli.Config) *Gitlab {
@ -110,10 +111,16 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
var headRef *plumbing.Reference // вроде ссылка на гит var headRef *plumbing.Reference // вроде ссылка на гит
if g.baseRef == nil {
g.baseRef, err = repo.Head()
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
}
}
refIter, err := repo.Branches() //получение веток refIter, err := repo.Branches() //получение веток
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
return err
} }
for { for {
ref, err := refIter.Next() ref, err := refIter.Next()
@ -138,10 +145,10 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
if err != nil { if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err)) logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
} }
defer checkout(wtree, *headRef) defer checkout(wtree, *g.baseRef)
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil && err != ErrPRNotExist { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
} }
@ -271,7 +278,7 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", g.Password) req.Header.Add("Authorization", "Bearer "+g.Password)
rsp, err := http.DefaultClient.Do(req) rsp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -289,8 +296,9 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error { func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path)) logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
var err error
pulls, err := GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
@ -298,7 +306,7 @@ func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) e
prExist := false prExist := false
var b string // Name of the branch to be deleted var b string // Name of the branch to be deleted
for _, pull := range pulls { for _, pull := range g.pulls {
if strings.Contains(pull.Title, path) { if strings.Contains(pull.Title, path) {
logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL)) logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
prExist = true prExist = true
@ -329,13 +337,11 @@ func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string,
logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path)) logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
var err error var err error
if len(g.pulls) == 0 { g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err return err
} }
}
prExist := false prExist := false
for _, pull := range g.pulls { for _, pull := range g.pulls {
@ -373,8 +379,9 @@ func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]str
logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch)) logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
var err error var err error
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil { if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return nil, err return nil, err
} }
@ -407,7 +414,7 @@ func DeleteBranch(ctx context.Context, url, projectId, branch, password string)
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password) req.Header.Add("Authorization", "Bearer "+password)
return req, err return req, err
} }
@ -424,7 +431,7 @@ func GetPulls(ctx context.Context, url, projectId, branch, password string) ([]*
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password) req.Header.Add("Authorization", "Bearer "+password)
rsp, err := http.DefaultClient.Do(req) // выполнение запроса rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil { if err != nil {
@ -450,7 +457,11 @@ func GetPulls(ctx context.Context, url, projectId, branch, password string) ([]*
func checkout(w *git.Worktree, ref plumbing.Reference) { func checkout(w *git.Worktree, ref plumbing.Reference) {
ctx := context.Background() ctx := context.Background()
if err := w.Reset(&git.ResetOptions{Commit: ref.Hash(), Mode: git.HardReset}); err != nil { logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err))
if err := w.Checkout(&git.CheckoutOptions{
Branch: ref.Name(),
}); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err))
} }
} }