#8 Получение project id для gitlab #15
@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
@ -58,6 +59,11 @@ type gitlabPull struct {
|
|||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type gitlabProject struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Gitlab) Name() string {
|
func (g *Gitlab) Name() string {
|
||||||
return "gitlab"
|
return "gitlab"
|
||||||
}
|
}
|
||||||
@ -140,6 +146,11 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo
|
|||||||
}
|
}
|
||||||
defer checkout(wtree, *headRef)
|
defer checkout(wtree, *headRef)
|
||||||
|
|
||||||
|
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)
|
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||||
if err != nil && err != ErrPRNotExist {
|
if err != nil && err != ErrPRNotExist {
|
||||||
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err))
|
||||||
@ -290,6 +301,13 @@ 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
|
||||||
|
|
||||||
|
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)
|
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))
|
||||||
@ -329,6 +347,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
|
||||||
|
|
||||||
|
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 {
|
if len(g.pulls) == 0 {
|
||||||
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -373,6 +396,11 @@ 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.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)
|
g.pulls, err = GetPulls(ctx, g.URL, g.RepositoryId, branch, g.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -448,6 +476,40 @@ 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) {
|
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 {
|
if err := w.Reset(&git.ResetOptions{Commit: ref.Hash(), Mode: git.HardReset}); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user