Files
archie/internal/webui/api_repository_check.go

50 lines
1.6 KiB
Go

package webui
import (
"context"
"encoding/json"
"net/http"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1"
)
type RepositoryChecker interface {
CheckRepository(context.Context, *pb.CheckRepositoryRequest) (*pb.CheckRepositoryResponse, error)
}
func (s *Server) handleRepositoryCheck(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.RepositoryChecks == nil {
http.Error(w, "Repository checks are unavailable. See Instance → Status for service availability.", http.StatusServiceUnavailable)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 8<<10)
var request pb.CheckRepositoryRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&request); err != nil {
http.Error(w, "Choose a repository and agent.", http.StatusBadRequest)
return
}
response, err := s.RepositoryChecks.CheckRepository(r.Context(), &request)
if err != nil {
code, message := http.StatusServiceUnavailable, "Repository access could not be checked. See Instance → Status, then try again."
switch status.Code(err) {
case codes.PermissionDenied:
code, message = http.StatusForbidden, "You do not have permission to check repository access."
case codes.Unauthenticated:
code, message = http.StatusUnauthorized, "Sign in before checking repository access."
case codes.InvalidArgument:
code, message = http.StatusBadRequest, "Choose a repository and agent."
}
http.Error(w, message, code)
return
}
writeJSON(w, response)
}