patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update.
(ctx context.Context, txn *sql.Tx, patch *TaskRunStatusPatch)
| 417 | |
| 418 | // patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update. |
| 419 | func (*Store) patchTaskRunStatusImpl(ctx context.Context, txn *sql.Tx, patch *TaskRunStatusPatch) (*TaskRunMessage, error) { |
| 420 | set := qb.Q() |
| 421 | |
| 422 | set.Comma("updated_at = ?, status = ?", time.Now(), patch.Status.String()) |
| 423 | |
| 424 | if v := patch.ResultProto; v != nil { |
| 425 | resultBytes, err := protojson.Marshal(v) |
| 426 | if err != nil { |
| 427 | return nil, errors.Wrapf(err, "failed to marshal task run result") |
| 428 | } |
| 429 | result := string(resultBytes) |
| 430 | if result == "" { |
| 431 | result = "{}" |
| 432 | } |
| 433 | set.Comma("result = ?", result) |
| 434 | } |
| 435 | |
| 436 | q := qb.Q().Space("UPDATE task_run SET ?", set). |
| 437 | Space("WHERE id = ? AND project = ?", patch.ID, patch.ProjectID) |
| 438 | |
| 439 | if len(patch.AllowedStatuses) > 0 { |
| 440 | var statusStrings []string |
| 441 | for _, status := range patch.AllowedStatuses { |
| 442 | statusStrings = append(statusStrings, status.String()) |
| 443 | } |
| 444 | q.Space("AND status = ANY(?)", statusStrings) |
| 445 | } |
| 446 | |
| 447 | q.Space("RETURNING id, creator, created_at, updated_at, task_id, status, result") |
| 448 | |
| 449 | query, args, err := q.ToSQL() |
| 450 | if err != nil { |
| 451 | return nil, errors.Wrapf(err, "failed to build sql") |
| 452 | } |
| 453 | |
| 454 | var taskRun TaskRunMessage |
| 455 | var creatorEmail sql.NullString |
| 456 | var statusString string |
| 457 | var resultJSON string |
| 458 | if err := txn.QueryRowContext(ctx, query, args...).Scan( |
| 459 | &taskRun.ID, |
| 460 | &creatorEmail, |
| 461 | &taskRun.CreatedAt, |
| 462 | &taskRun.UpdatedAt, |
| 463 | &taskRun.TaskUID, |
| 464 | &statusString, |
| 465 | &resultJSON, |
| 466 | ); err != nil { |
| 467 | if err == sql.ErrNoRows { |
| 468 | if len(patch.AllowedStatuses) > 0 { |
| 469 | return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("task run %d status changed", patch.ID)} |
| 470 | } |
| 471 | return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("project ID not found: %d", patch.ID)} |
| 472 | } |
| 473 | return nil, err |
| 474 | } |
| 475 | if creatorEmail.Valid { |
| 476 | taskRun.CreatorEmail = creatorEmail.String |