From db2c0de4c3540ee631549953c4f0e353b4be7fa1 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Sun, 21 Apr 2024 16:43:27 +0300 Subject: [PATCH] #8 fix checkout. --- internal/source/gitlab/gitlab.go | 58 +++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 88fc2be..7ed6696 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -10,6 +10,7 @@ import ( "net/http" "os/exec" "regexp" + "strconv" "strings" "text/template" "time" @@ -59,6 +60,11 @@ 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" } @@ -147,7 +153,7 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo } defer checkout(*wtree, *g.baseRef) - g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) + g.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 @@ -298,7 +304,7 @@ func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) e logger.Debug(ctx, fmt.Sprintf("RequestClose start, mod title: %s", path)) var err error - g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) + g.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 @@ -337,7 +343,12 @@ 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.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) + 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 { logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) return err @@ -379,7 +390,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.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) + 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 { logger.Error(ctx, fmt.Sprintf("GetPulls error: %s", err)) return nil, err @@ -455,6 +471,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) { ctx := context.Background() logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short())) -- 2.45.2