#8 Checkout, auth. #16

Closed
kgorbunov wants to merge 104 commits from kgorbunov:dev into master
4 changed files with 98 additions and 107 deletions
Showing only changes of commit de2eafaac5 - Show all commits

View File

@ -3,8 +3,9 @@ branches:
source:
type: gitea
apiurl: git.unistack.org
repository: pkgdash
update_opt:
pre: false
major: false
up_major: false
cached: true
cached: true

View File

@ -105,13 +105,18 @@ 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 // вроде ссылка на гит
var headRef, baseRef *plumbing.Reference // вроде ссылка на гит
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,12 +145,14 @@ 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, *baseRef)
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
if len(g.pulls) == 0 {
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
}
}
for _, pull := range g.pulls {
@ -270,7 +277,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,16 +300,19 @@ 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)
if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
if len(g.pulls) == 0 {
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
}
}
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
@ -377,9 +387,12 @@ 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.Password)
if err != nil {
return nil, err
if len(g.pulls) == 0 {
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 nil, err
}
}
var path string
@ -411,7 +424,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 +445,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 {
@ -466,7 +479,9 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaP
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.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

@ -140,10 +140,12 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
}
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
if len(g.pulls) == 0 {
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
}
}
for _, pull := range g.pulls {
@ -268,7 +270,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 +297,28 @@ 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
if len(g.pulls) == 0 {
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 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 +338,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 {

View File

@ -10,7 +10,6 @@ import (
"net/http"
"os/exec"
"regexp"
"strconv"
"strings"
"text/template"
"time"
@ -59,11 +58,6 @@ type gitlabPull struct {
ID int64 `json:"id"`
}
type gitlabProject struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
func (g *Gitlab) Name() string {
return "gitlab"
}
@ -114,12 +108,16 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err))
} //обновляем репозиторий
var headRef *plumbing.Reference // вроде ссылка на гит
var headRef, baseRef *plumbing.Reference // вроде ссылка на гит
baseRef, err = repo.Head()
if err != nil {
logger.Error(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))
return err
}
for {
ref, err := refIter.Next()
@ -144,17 +142,14 @@ func (g *Gitlab) 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, *headRef)
defer checkout(wtree, *baseRef)
g.RepositoryId, err = GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil || g.RepositoryId == "" {
return fmt.Errorf("project id is empty")
}
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil && err != ErrPRNotExist {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil && err != ErrPRNotExist {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
}
}
for _, pull := range g.pulls {
@ -282,7 +277,7 @@ func (g *Gitlab) 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 {
@ -300,23 +295,19 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
var err error
g.RepositoryId, err = GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil || g.RepositoryId == "" {
return fmt.Errorf("project id is empty")
}
pulls, err := GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil && err != ErrPRNotExist {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return err
}
}
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) {
logger.Info(ctx, fmt.Sprintf("PR for %s exists: %s", path, pull.URL))
prExist = true
@ -347,11 +338,6 @@ func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string,
logger.Debug(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
var err error
g.RepositoryId, err = GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil || g.RepositoryId == "" {
return fmt.Errorf("project id is empty")
}
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil {
@ -396,14 +382,12 @@ func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]str
logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
var err error
g.RepositoryId, err = GetRepoID(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil || g.RepositoryId == "" {
return nil, fmt.Errorf("project id is empty")
}
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil {
return nil, err
if len(g.pulls) == 0 {
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
if err != nil && err != ErrPRNotExist {
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
return nil, err
}
}
var path string
@ -435,7 +419,7 @@ func DeleteBranch(ctx context.Context, url, projectId, 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
}
@ -452,7 +436,7 @@ func GetPulls(ctx context.Context, url, projectId, 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)
rsp, err := http.DefaultClient.Do(req) // выполнение запроса
if err != nil {
@ -476,43 +460,11 @@ func GetPulls(ctx context.Context, url, projectId, branch, password string) ([]*
}
}
func GetRepoID(ctx context.Context, url, owner, repo, password string) (rId string, err error) {
var buf []byte
projects := make([]*gitlabProject, 0, 10)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/api/v4/users/%s/projects?owned=true", url, owner), nil)
if err != nil {
return
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", password)
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
buf, _ = io.ReadAll(rsp.Body)
switch rsp.StatusCode {
case http.StatusOK:
if err = json.Unmarshal(buf, &projects); err != nil {
logger.Error(ctx, fmt.Sprintf("failed to decode response %s err: %v", buf, err))
}
for _, p := range projects {
if p.Name == repo {
rId = strconv.Itoa(int(p.Id))
}
}
return
default:
return rId, fmt.Errorf("unknown error: %s", buf)
}
}
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.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))
}
}