updateEntityCredentials fetches, validates, and assigns credentials to an entity
(ctx context.Context, tx *gorm.DB, entity ForgeEntity, credentialsName string, updates map[string]interface{})
| 1159 | |
| 1160 | // updateEntityCredentials fetches, validates, and assigns credentials to an entity |
| 1161 | func (s *sqlDatabase) updateEntityCredentials(ctx context.Context, tx *gorm.DB, entity ForgeEntity, credentialsName string, updates map[string]interface{}) error { |
| 1162 | if credentialsName == "" { |
| 1163 | return nil |
| 1164 | } |
| 1165 | |
| 1166 | // Entity knows its own endpoint type |
| 1167 | forgeType := entity.GetEndpoint().EndpointType |
| 1168 | endpointName := entity.GetEndpointName() |
| 1169 | |
| 1170 | if endpointName == nil { |
| 1171 | return runnerErrors.NewUnprocessableError("entity has no endpoint") |
| 1172 | } |
| 1173 | |
| 1174 | // Fetch credentials by type |
| 1175 | creds, err := s.fetchCredentialsByType(ctx, tx, credentialsName, forgeType) |
| 1176 | if err != nil { |
| 1177 | return fmt.Errorf("error fetching credentials: %w", err) |
| 1178 | } |
| 1179 | |
| 1180 | // Validate credentials |
| 1181 | if creds.GetEndpointName() == nil { |
| 1182 | return runnerErrors.NewUnprocessableError("credentials have no endpoint") |
| 1183 | } |
| 1184 | if *creds.GetEndpointName() != *endpointName { |
| 1185 | return runnerErrors.NewBadRequestError("endpoint mismatch") |
| 1186 | } |
| 1187 | |
| 1188 | // Add the appropriate credentials field to updates based on endpoint type |
| 1189 | // Only add if the credentials ID has changed |
| 1190 | credID := creds.GetID() |
| 1191 | switch forgeType { |
| 1192 | case params.GithubEndpointType: |
| 1193 | // Check current credentials ID based on entity type |
| 1194 | switch e := entity.(type) { |
| 1195 | case *Repository: |
| 1196 | if e.CredentialsID == nil || *e.CredentialsID != credID { |
| 1197 | updates["credentials_id"] = credID |
| 1198 | } |
| 1199 | case *Organization: |
| 1200 | if e.CredentialsID == nil || *e.CredentialsID != credID { |
| 1201 | updates["credentials_id"] = credID |
| 1202 | } |
| 1203 | case *Enterprise: |
| 1204 | if e.CredentialsID == nil || *e.CredentialsID != credID { |
| 1205 | updates["credentials_id"] = credID |
| 1206 | } |
| 1207 | } |
| 1208 | case params.GiteaEndpointType: |
| 1209 | switch e := entity.(type) { |
| 1210 | case *Repository: |
| 1211 | if e.GiteaCredentialsID == nil || *e.GiteaCredentialsID != credID { |
| 1212 | updates["gitea_credentials_id"] = credID |
| 1213 | } |
| 1214 | case *Organization: |
| 1215 | if e.GiteaCredentialsID == nil || *e.GiteaCredentialsID != credID { |
| 1216 | updates["gitea_credentials_id"] = credID |
| 1217 | } |
| 1218 | } |
no test coverage detected