#8 fix checkout.
This commit is contained in:
@@ -35,6 +35,7 @@ type Gitea struct {
|
||||
Repository string
|
||||
Owner string
|
||||
pulls []*giteaPull
|
||||
baseRef *plumbing.Reference
|
||||
}
|
||||
|
||||
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{
|
||||
// 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))
|
||||
} //обновляем репозиторий
|
||||
|
||||
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() //получение веток
|
||||
if err != nil {
|
||||
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 {
|
||||
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)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", 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("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", g.Password)
|
||||
req.Header.Add("Authorization", "Bearer "+g.Password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
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 {
|
||||
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 {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -302,7 +312,7 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
|
||||
|
||||
prExist := false
|
||||
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 {
|
||||
logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
|
||||
prExist = true
|
||||
@@ -333,12 +343,10 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
|
||||
var err error
|
||||
|
||||
if len(g.pulls) == 0 {
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
prExist := false
|
||||
@@ -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)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", 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("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", password)
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
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("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", password)
|
||||
req.Header.Add("Authorization", "Bearer "+password)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req) // выполнение запроса
|
||||
if err != nil {
|
||||
@@ -464,9 +473,16 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaP
|
||||
return pullsAll, nil
|
||||
}
|
||||
|
||||
func checkout(w *git.Worktree, ref plumbing.Reference) {
|
||||
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))
|
||||
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 {
|
||||
logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user