#8 fix checkout.

This commit is contained in:
Gorbunov Kirill Andreevich
2024-04-21 16:26:11 +03:00
parent 700ba16470
commit c3ad2e5bba
4 changed files with 113 additions and 101 deletions

View File

@@ -34,6 +34,7 @@ type Github struct {
Repository string
Owner string
pulls []*githubPull
baseRef *plumbing.Reference
}
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 // вроде ссылка на гит
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))
@@ -139,9 +148,10 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
if err != nil {
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)
if err != nil && err != ErrPRNotExist {
if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", 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("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 {
@@ -295,7 +305,26 @@ func (g *Github) RequestUpdate(ctx context.Context, branch string, path string,
return fmt.Errorf("implement me")
}
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) {
@@ -315,7 +344,7 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github
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 {
@@ -346,3 +375,17 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github
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{
Branch: ref.Name(),
Create: false,
Force: true,
Keep: false,
}); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err))
}
}