93 lines
2.9 KiB
Go
93 lines
2.9 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"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/daemonrpc"
|
|
)
|
|
|
|
type RepositoryChecker interface {
|
|
CheckRepository(context.Context, *pb.CheckRepositoryRequest) (*pb.CheckRepositoryResponse, error)
|
|
}
|
|
|
|
type IssueLister interface {
|
|
ListIssues(context.Context, *daemonrpc.ListIssuesRequest) (*daemonrpc.ListIssuesResponse, error)
|
|
}
|
|
|
|
type issueView struct {
|
|
Owner string `json:"owner"`
|
|
Repository string `json:"repository"`
|
|
Number int32 `json:"number"`
|
|
Title string `json:"title"`
|
|
Labels []string `json:"labels,omitempty"`
|
|
}
|
|
|
|
func (s *Server) handleIssues(w http.ResponseWriter, r *http.Request) {
|
|
if s.Issues == nil {
|
|
writeJSON(w, []issueView{})
|
|
return
|
|
}
|
|
response, err := s.Issues.ListIssues(r.Context(), &daemonrpc.ListIssuesRequest{})
|
|
if err != nil {
|
|
code, message := http.StatusServiceUnavailable, "Repository issues are unavailable."
|
|
switch status.Code(err) {
|
|
case codes.PermissionDenied:
|
|
code, message = http.StatusForbidden, "You do not have permission to list issues."
|
|
case codes.Unauthenticated:
|
|
code, message = http.StatusUnauthorized, "Sign in before listing issues."
|
|
}
|
|
http.Error(w, message, code)
|
|
return
|
|
}
|
|
out := make([]issueView, 0, len(response.Issues))
|
|
for _, issue := range response.Issues {
|
|
out = append(out, issueView{
|
|
Owner: issue.Owner,
|
|
Repository: issue.Repository,
|
|
Number: issue.Number,
|
|
Title: issue.Title,
|
|
Labels: issue.Labels,
|
|
})
|
|
}
|
|
writeJSON(w, out)
|
|
}
|
|
|
|
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)
|
|
}
|