(ctx context.Context, repoID string, param params.UpdateEntityParams)
| 154 | } |
| 155 | |
| 156 | func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (newParams params.Repository, err error) { |
| 157 | var rowsAffected int64 |
| 158 | defer func() { |
| 159 | if err == nil && rowsAffected > 0 { |
| 160 | s.sendNotify(common.RepositoryEntityType, common.UpdateOperation, newParams) |
| 161 | } |
| 162 | }() |
| 163 | var repo Repository |
| 164 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 165 | var err error |
| 166 | repo, err = s.getRepoByID(ctx, tx, repoID, "Endpoint") |
| 167 | if err != nil { |
| 168 | return fmt.Errorf("error fetching repo: %w", err) |
| 169 | } |
| 170 | if repo.EndpointName == nil { |
| 171 | return runnerErrors.NewUnprocessableError("repository has no endpoint") |
| 172 | } |
| 173 | |
| 174 | updates := make(map[string]interface{}) |
| 175 | |
| 176 | if err := s.updateEntityCredentials(ctx, tx, &repo, param.CredentialsName, updates); err != nil { |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | if param.WebhookSecret != "" { |
| 181 | // Decrypt existing secret to compare |
| 182 | existingSecret, err := util.Unseal(repo.WebhookSecret, []byte(s.cfg.Passphrase)) |
| 183 | if err != nil { |
| 184 | return fmt.Errorf("failed to decrypt existing webhook secret: %w", err) |
| 185 | } |
| 186 | // Only update if the webhook secret has changed |
| 187 | if string(existingSecret) != param.WebhookSecret { |
| 188 | secret, err := util.Seal([]byte(param.WebhookSecret), []byte(s.cfg.Passphrase)) |
| 189 | if err != nil { |
| 190 | return fmt.Errorf("saving repo: failed to encrypt string: %w", err) |
| 191 | } |
| 192 | updates["webhook_secret"] = secret |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if param.PoolManagerStatus != nil { |
| 197 | if param.PoolManagerStatus.IsRunning != repo.PoolManagerRunning { |
| 198 | updates["pool_manager_running"] = param.PoolManagerStatus.IsRunning |
| 199 | } |
| 200 | if param.PoolManagerStatus.FailureReason != repo.PoolManagerFailureReason { |
| 201 | updates["pool_manager_failure_reason"] = param.PoolManagerStatus.FailureReason |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if param.PoolBalancerType != "" && param.PoolBalancerType != repo.PoolBalancerType { |
| 206 | updates["pool_balancer_type"] = param.PoolBalancerType |
| 207 | } |
| 208 | if param.AgentMode != nil && *param.AgentMode != repo.AgentMode { |
| 209 | updates["agent_mode"] = *param.AgentMode |
| 210 | } |
| 211 | |
| 212 | if len(updates) > 0 { |
| 213 | q := tx.Model(&repo).Omit("Endpoint").Updates(updates) |
no test coverage detected