feat(daemon): execute and verify organisation repository connections
This commit is contained in:
1 parent
32642af4a9
commit
05987f04e2
45 files changed
+1600
-284
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
@@ -195,32 +196,55 @@ func (s *Server) workRequestRepository(ctx context.Context, mode task.Repository
|
||||
if !ok || owner == "" || repo == "" || strings.Contains(repo, "/") {
|
||||
return "", "", http.StatusBadRequest, errors.New("repository must be owner/name")
|
||||
}
|
||||
view, found, err := s.configSource()(ctx)
|
||||
if err != nil || !found {
|
||||
return "", "", http.StatusServiceUnavailable, errors.New("configured repositories unavailable")
|
||||
repos, err := s.configuredRepositories(ctx)
|
||||
if err != nil {
|
||||
return "", "", http.StatusServiceUnavailable, err
|
||||
}
|
||||
if !viewHasRepo(view, owner, repo) {
|
||||
found := false
|
||||
for _, r := range repos {
|
||||
if strings.EqualFold(r.Owner, owner) && strings.EqualFold(r.Name, repo) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", "", http.StatusBadRequest, fmt.Errorf("repository %s/%s is not configured", owner, repo)
|
||||
}
|
||||
return owner, repo, 0, nil
|
||||
}
|
||||
|
||||
// viewHasRepo reports whether the default identity or any named identity
|
||||
// manages owner/repo.
|
||||
func viewHasRepo(view ConfigView, owner, repo string) bool {
|
||||
for _, r := range view.Repositories {
|
||||
if strings.EqualFold(r.Owner, owner) && strings.EqualFold(r.Name, repo) {
|
||||
return true
|
||||
}
|
||||
// handleRepositories lists configured names without requiring permission to
|
||||
// read or edit the organisation's full settings document.
|
||||
func (s *Server) handleRepositories(w http.ResponseWriter, r *http.Request) {
|
||||
repos, err := s.configuredRepositories(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, "Configured repositories are unavailable.", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
for _, identity := range view.Identities {
|
||||
for _, r := range identity.Repos {
|
||||
if strings.EqualFold(r.Owner, owner) && strings.EqualFold(r.Name, repo) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
type repositoryName struct {
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
return false
|
||||
names := make([]repositoryName, 0, len(repos))
|
||||
for _, repo := range repos {
|
||||
names = append(names, repositoryName{Owner: repo.Owner, Name: repo.Name})
|
||||
}
|
||||
writeJSON(w, names)
|
||||
}
|
||||
|
||||
func (s *Server) configuredRepositories(ctx context.Context) ([]config.Repo, error) {
|
||||
if s.ControlPlane == nil {
|
||||
return nil, errors.New("configured repositories unavailable")
|
||||
}
|
||||
response, err := s.ControlPlane.Query(ctx, &controlpb.QueryRequest{Kind: "repository-policies"})
|
||||
if err != nil || response.Resource == nil {
|
||||
return nil, errors.New("configured repositories unavailable")
|
||||
}
|
||||
var repos []config.Repo
|
||||
if err := json.Unmarshal(response.Resource.ValueJson, &repos); err != nil {
|
||||
return nil, errors.New("configured repositories unavailable")
|
||||
}
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
// enabledWorkflowInterface returns a workflow's declared interface and whether
|
||||
|
||||
Reference in new issue
Block a user