fixup for 2fa auth in gitea
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -28,7 +28,8 @@ var ErrPRNotExist = errors.New("pull request does not exist")
|
||||
|
||||
type Gitea struct {
|
||||
URL string
|
||||
Token string
|
||||
Username string
|
||||
Password string
|
||||
PRTitle string
|
||||
PRBody string
|
||||
Repository string
|
||||
@@ -39,7 +40,8 @@ type Gitea struct {
|
||||
func NewGitea(cfg configcli.Config) *Gitea {
|
||||
return &Gitea{
|
||||
URL: cfg.Source.APIURL,
|
||||
Token: cfg.Source.Token,
|
||||
Username: cfg.Source.Username,
|
||||
Password: cfg.Source.Password,
|
||||
PRTitle: cfg.PullRequestTitle,
|
||||
PRBody: cfg.PullRequestBody,
|
||||
Repository: cfg.Source.Repository,
|
||||
@@ -64,7 +66,7 @@ func (g *Gitea) Name() string {
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
logger.Debugf(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
|
||||
|
||||
var buf []byte
|
||||
var err error
|
||||
@@ -103,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.Token, Password: g.Token},
|
||||
// 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 // вроде ссылка на гит
|
||||
@@ -139,7 +141,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
|
||||
}
|
||||
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
if err != nil && err != ErrPRNotExist {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -160,7 +162,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
} //вроде меняем ветку todo вроде можно удалить
|
||||
|
||||
if err = wtree.PullContext(ctx, &git.PullOptions{
|
||||
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Depth: 1,
|
||||
// RemoteURL :
|
||||
Force: true,
|
||||
@@ -235,7 +237,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
|
||||
|
||||
if err = repo.PushContext(ctx, &git.PushOptions{
|
||||
RefSpecs: []gitconfig.RefSpec{refspec},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Token, Password: g.Token},
|
||||
Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
logger.Fatal(ctx, fmt.Sprintf("failed to push repo branch: %v", err))
|
||||
@@ -259,7 +261,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?token=%s", g.URL, g.Owner, g.Repository, g.Token),
|
||||
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls", g.URL, g.Owner, g.Repository, g.Password),
|
||||
bytes.NewReader(buf),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -267,6 +269,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)
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -288,9 +291,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.Debugf(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path))
|
||||
|
||||
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
|
||||
pulls, err := GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -310,7 +313,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.Token)
|
||||
req, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, b, g.Username, 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
|
||||
@@ -326,11 +329,11 @@ func (g *Gitea) RequestClose(ctx context.Context, branch string, path string) er
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
|
||||
logger.Debugf(ctx, fmt.Sprintf("RequestUpdate start, mod title: %s", path))
|
||||
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.Token)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||
return err
|
||||
@@ -343,7 +346,7 @@ 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.Token)
|
||||
reqDel, err := DeleteBranch(ctx, g.URL, g.Owner, g.Repository, pull.Head.Ref, g.Username, g.Password)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("Error with create request for branch: %s, err: %s", branch, err))
|
||||
continue
|
||||
@@ -355,7 +358,7 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
}
|
||||
logger.Info(ctx, fmt.Sprintf("Old pr %s successful delete", pull.Head.Ref))
|
||||
} else {
|
||||
logger.Debugf(ctx, "The existing PR is relevant")
|
||||
logger.Debug(ctx, "The existing PR is relevant")
|
||||
return nil
|
||||
}
|
||||
prExist = true
|
||||
@@ -372,10 +375,10 @@ func (g *Gitea) RequestUpdate(ctx context.Context, branch string, path string, m
|
||||
}
|
||||
|
||||
func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]string, error) {
|
||||
logger.Debugf(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
logger.Debug(ctx, fmt.Sprintf("RequestList for %s", branch))
|
||||
var err error
|
||||
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Token)
|
||||
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Username, g.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -401,18 +404,19 @@ func getVersions(s string) string {
|
||||
return version
|
||||
}
|
||||
|
||||
func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) {
|
||||
func DeleteBranch(ctx context.Context, url, owner, repo, branch, username, 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?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", password)
|
||||
return req, err
|
||||
}
|
||||
|
||||
func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull, error) {
|
||||
func GetPulls(ctx context.Context, url, owner, repo, username, password string) ([]*giteaPull, error) {
|
||||
var pullsAll []*giteaPull
|
||||
page := 1
|
||||
|
||||
@@ -421,7 +425,7 @@ func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("https://%s/api/v1//repos/%s/%s/pulls?state=open&page=%v&token=%s", url, owner, repo, page, token),
|
||||
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
|
||||
@@ -429,6 +433,7 @@ func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull
|
||||
|
||||
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 {
|
||||
|
Reference in New Issue
Block a user