|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controller |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "math" |
| 22 | + |
| 23 | + "github.com/uber-go/tally" |
| 24 | + "github.com/uber/submitqueue/platform/errs" |
| 25 | + "github.com/uber/submitqueue/platform/metrics" |
| 26 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 27 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 28 | + "go.uber.org/zap" |
| 29 | +) |
| 30 | + |
| 31 | +const maxProjectStatusPageSize = 200 |
| 32 | + |
| 33 | +// ProjectStatusNotFoundError indicates that no validation request matches a lookup selector. |
| 34 | +type ProjectStatusNotFoundError struct { |
| 35 | + // Queue is the queue in the selector. |
| 36 | + Queue string |
| 37 | + // ChangeURI is the commit URI in the selector. |
| 38 | + ChangeURI string |
| 39 | +} |
| 40 | + |
| 41 | +func (e *ProjectStatusNotFoundError) Error() string { |
| 42 | + return fmt.Sprintf("project status not found for queue %q and change URI %q", e.Queue, e.ChangeURI) |
| 43 | +} |
| 44 | + |
| 45 | +// IsProjectStatusNotFound reports whether err represents an unknown validation request. |
| 46 | +func IsProjectStatusNotFound(err error) bool { |
| 47 | + var target *ProjectStatusNotFoundError |
| 48 | + return errors.As(err, &target) |
| 49 | +} |
| 50 | + |
| 51 | +// ProjectStatusConsistencyError indicates that persisted records disagree about a request. |
| 52 | +type ProjectStatusConsistencyError struct { |
| 53 | + message string |
| 54 | +} |
| 55 | + |
| 56 | +func (e *ProjectStatusConsistencyError) Error() string { return e.message } |
| 57 | + |
| 58 | +// IsProjectStatusConsistency reports whether err represents inconsistent persisted state. |
| 59 | +func IsProjectStatusConsistency(err error) bool { |
| 60 | + var target *ProjectStatusConsistencyError |
| 61 | + return errors.As(err, &target) |
| 62 | +} |
| 63 | + |
| 64 | +// GetProjectStatusByURIController reads the durable repository-level validation projection. |
| 65 | +// Project result reads are added when planned-project storage exists. |
| 66 | +type GetProjectStatusByURIController struct { |
| 67 | + logger *zap.SugaredLogger |
| 68 | + metricsScope tally.Scope |
| 69 | + stores storage.Factory |
| 70 | +} |
| 71 | + |
| 72 | +// NewGetProjectStatusByURIController creates a controller for validation-status lookups. |
| 73 | +func NewGetProjectStatusByURIController(logger *zap.SugaredLogger, scope tally.Scope, stores storage.Factory) *GetProjectStatusByURIController { |
| 74 | + return &GetProjectStatusByURIController{ |
| 75 | + logger: logger, |
| 76 | + metricsScope: scope.SubScope("get_project_status_by_uri_controller"), |
| 77 | + stores: stores, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// GetProjectStatusByURI returns the selected request and its repository validation fact, if recorded. |
| 82 | +func (c *GetProjectStatusByURIController) GetProjectStatusByURI(ctx context.Context, req entity.GetProjectStatusByURIRequest) (result entity.GetProjectStatusByURIResult, retErr error) { |
| 83 | + op := metrics.Begin(c.metricsScope, "get_project_status_by_uri", metrics.StorageLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 84 | + defer func() { op.Complete(retErr) }() |
| 85 | + |
| 86 | + if err := validateProjectStatusRequest(req); err != nil { |
| 87 | + return entity.GetProjectStatusByURIResult{}, err |
| 88 | + } |
| 89 | + store, err := c.stores.For(storage.Config{QueueName: req.Queue}) |
| 90 | + if err != nil { |
| 91 | + return entity.GetProjectStatusByURIResult{}, fmt.Errorf("GetProjectStatusByURI failed to resolve storage for queue %q: %w", req.Queue, err) |
| 92 | + } |
| 93 | + |
| 94 | + requestID, err := store.GetRequestURIStore().GetIDByURI(ctx, req.ChangeURI) |
| 95 | + if err != nil { |
| 96 | + if storage.IsNotFound(err) { |
| 97 | + return entity.GetProjectStatusByURIResult{}, errs.NewUserError(&ProjectStatusNotFoundError{Queue: req.Queue, ChangeURI: req.ChangeURI}) |
| 98 | + } |
| 99 | + return entity.GetProjectStatusByURIResult{}, fmt.Errorf("GetProjectStatusByURI failed to resolve request for URI %q: %w", req.ChangeURI, err) |
| 100 | + } |
| 101 | + |
| 102 | + request, err := store.GetRequestStore().Get(ctx, requestID) |
| 103 | + if err != nil { |
| 104 | + if storage.IsNotFound(err) { |
| 105 | + // The URI mapping is created before the request, so this gap is retryable. |
| 106 | + return entity.GetProjectStatusByURIResult{}, errs.NewRetryableError(fmt.Errorf("GetProjectStatusByURI request %q is not visible yet", requestID)) |
| 107 | + } |
| 108 | + return entity.GetProjectStatusByURIResult{}, fmt.Errorf("GetProjectStatusByURI failed to load request %q: %w", requestID, err) |
| 109 | + } |
| 110 | + if request.ID != requestID || request.Queue != req.Queue || request.URI != req.ChangeURI { |
| 111 | + return entity.GetProjectStatusByURIResult{}, &ProjectStatusConsistencyError{message: "request URI mapping disagrees with stored request"} |
| 112 | + } |
| 113 | + |
| 114 | + result.Request = request |
| 115 | + logs, err := store.GetRequestLogStore().List(ctx, request.ID) |
| 116 | + if err != nil && !storage.IsNotFound(err) { |
| 117 | + return entity.GetProjectStatusByURIResult{}, fmt.Errorf("GetProjectStatusByURI failed to load request history for %q: %w", request.ID, err) |
| 118 | + } |
| 119 | + for _, log := range logs { |
| 120 | + if log.TimestampMs > result.UpdatedAtMs { |
| 121 | + result.UpdatedAtMs = log.TimestampMs |
| 122 | + } |
| 123 | + } |
| 124 | + fact, err := store.GetValidationFactStore().Get(ctx, request.URI, "") |
| 125 | + if err != nil { |
| 126 | + if storage.IsNotFound(err) { |
| 127 | + return result, nil |
| 128 | + } |
| 129 | + return entity.GetProjectStatusByURIResult{}, fmt.Errorf("GetProjectStatusByURI failed to load repository fact for request %q: %w", request.ID, err) |
| 130 | + } |
| 131 | + if err := validateRepositoryFact(fact, request); err != nil { |
| 132 | + return entity.GetProjectStatusByURIResult{}, err |
| 133 | + } |
| 134 | + result.RepositoryValidationFact = fact |
| 135 | + result.HasRepositoryValidationFact = true |
| 136 | + if fact.CreatedAt > result.UpdatedAtMs { |
| 137 | + result.UpdatedAtMs = fact.CreatedAt |
| 138 | + } |
| 139 | + |
| 140 | + c.logger.Debugw("project status retrieved", "request_id", request.ID, "queue", request.Queue, "change_uri", request.URI, "has_repository_result", true) |
| 141 | + return result, nil |
| 142 | +} |
| 143 | + |
| 144 | +func validateProjectStatusRequest(req entity.GetProjectStatusByURIRequest) error { |
| 145 | + if err := validateHistoryIdentifier("queue", req.Queue); err != nil { |
| 146 | + return fmt.Errorf("GetProjectStatusByURI invalid queue=%q: %w", req.Queue, err) |
| 147 | + } |
| 148 | + if err := validateHistoryIdentifier("change URI", req.ChangeURI); err != nil { |
| 149 | + return fmt.Errorf("GetProjectStatusByURI invalid change_uri=%q: %w", req.ChangeURI, err) |
| 150 | + } |
| 151 | + if req.HasProject && req.Project == "" { |
| 152 | + return fmt.Errorf("GetProjectStatusByURI project must be non-empty when present: %w", ErrInvalidRequest) |
| 153 | + } |
| 154 | + if req.HasProject && len(req.Project) > maxHistoryIdentifierBytes { |
| 155 | + return fmt.Errorf("GetProjectStatusByURI project exceeds %d bytes: %w", maxHistoryIdentifierBytes, ErrInvalidRequest) |
| 156 | + } |
| 157 | + if req.HasProject && (req.PageSize != 0 || req.PageToken != "") { |
| 158 | + return fmt.Errorf("GetProjectStatusByURI page fields are invalid with project: %w", ErrInvalidRequest) |
| 159 | + } |
| 160 | + if req.PageSize < 0 || req.PageSize > maxProjectStatusPageSize { |
| 161 | + return fmt.Errorf("GetProjectStatusByURI page_size must be between 0 and %d: %w", maxProjectStatusPageSize, ErrInvalidRequest) |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func validateRepositoryFact(fact entity.ValidationFact, request entity.Request) error { |
| 167 | + if fact.URI != request.URI || fact.Project != "" || fact.RequestID != request.ID { |
| 168 | + return &ProjectStatusConsistencyError{message: "repository validation fact disagrees with stored request"} |
| 169 | + } |
| 170 | + if math.IsNaN(fact.Degree) || fact.Degree < entity.DegreeGreen || fact.Degree > entity.DegreeBroken { |
| 171 | + return &ProjectStatusConsistencyError{message: "repository validation fact has an invalid degree"} |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
0 commit comments