Pipeline started_at should not change
Problem
We set the pipeline's started_at time when the pipeline transitions to a running state. This works fine on linear pipeline executions: the pipeline starts and finishes without any external interactions.
However, if we retry a job or play a manual job or run a scheduled job, the pipeline that had previously completed is back into running state. This causes started_at to be reset. What's worse is that queueing time is broken.
The following pipeline https://gitlab.com/gitlab-org/gitlab/-/pipelines/452086059 shows as queued for 2962 minutes and 3 seconds because the scheduled job review-stop ran 2 days later.
Solution
Set started_at only once (the first time the pipeline transitions to running state). Let finished_at to be moved (as is) when a pipeline is retried.
before_transition [:created, :waiting_for_resource, :preparing, :pending] => :running do |pipeline|
pipeline.started_at ||= Time.current # only set it once
end