agola/internal/services/gateway/api/remoterepo.go
Simone Gotti c1da3ab566 *: Improve error handling
* Create an APIError that should only be used for api returned errors.
  It'll wrap an error and can have different Kinds and optional code and
  message.
* The http handlers will use the first APIError available in the
  error chain and generate a json response body containing the code and
  the user message. The wrapped error is internal and is not sent in the
  response.
  If no api error is available in the chain a generic internal
  server error will be returned.
* Add a RemoteError type that will be created from remote services calls
  (runservice, configstore). It's similar to the APIError but a
  different type to not propagate to the caller response and it'll not
  contain any wrapped error.
* Gateway: when we call a remote service, by default, we'll create a
  APIError using the RemoteError Kind (omitting the code and the
  message that usually must not be propagated).
  This is done for all the remote service calls as a starting point, in
  future, if this default behavior is not the right one for a specific
  remote service call, a new api error with a different kind and/or
  augmented with the calling service error codes and user messages could
  be created.
* datamanager: Use a dedicated ErrNotExist (and converting objectstorage
  ErrNotExist).
2022-02-25 16:11:19 +01:00

112 lines
3.2 KiB
Go

// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"net/http"
gitsource "agola.io/agola/internal/gitsources"
"agola.io/agola/internal/services/gateway/action"
"agola.io/agola/internal/services/gateway/common"
"agola.io/agola/internal/util"
csclient "agola.io/agola/services/configstore/client"
cstypes "agola.io/agola/services/configstore/types"
gwapitypes "agola.io/agola/services/gateway/api/types"
"github.com/gorilla/mux"
"go.uber.org/zap"
errors "golang.org/x/xerrors"
)
func createRemoteRepoResponse(r *gitsource.RepoInfo) *gwapitypes.RemoteRepoResponse {
rr := &gwapitypes.RemoteRepoResponse{
ID: r.ID,
Path: r.Path,
}
return rr
}
type UserRemoteReposHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
configstoreClient *csclient.Client
}
func NewUserRemoteReposHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csclient.Client) *UserRemoteReposHandler {
return &UserRemoteReposHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient}
}
func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
remoteSourceRef := vars["remotesourceref"]
userID := common.CurrentUserID(ctx)
if userID == "" {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
return
}
user, _, err := h.configstoreClient.GetUser(ctx, userID)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceRef)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
var la *cstypes.LinkedAccount
for _, v := range user.LinkedAccounts {
if v.RemoteSourceID == rs.ID {
la = v
break
}
}
if la == nil {
err := util.NewAPIError(util.ErrBadRequest, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name))
util.HTTPError(w, err)
h.log.Errorf("err: %+v", err)
return
}
gitsource, err := h.ah.GetGitSource(ctx, rs, user.Name, la)
if err != nil {
util.HTTPError(w, err)
h.log.Errorf("err: %+v", err)
return
}
remoteRepos, err := gitsource.ListUserRepos()
if err != nil {
err := util.NewAPIError(util.ErrBadRequest, errors.Errorf("failed to get user repositories from git source: %w", err))
util.HTTPError(w, err)
h.log.Errorf("err: %+v", err)
return
}
repos := make([]*gwapitypes.RemoteRepoResponse, len(remoteRepos))
for i, r := range remoteRepos {
repos[i] = createRemoteRepoResponse(r)
}
if err := util.HTTPResponse(w, http.StatusOK, repos); err != nil {
h.log.Errorf("err: %+v", err)
}
}