mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-13 04:58:46 +00:00
chore: re-enable nilnil lint for models/actions/task.go (#12768)
I added sentinel error values for jobs not being found and no jobs being updated by the `CreateTaskForRunner` function. This avoids the nilnil antipattern and allows the nilnil lint to be enabled for the `models/actions/task.go` file again. The handling of these new errors was added to the `PickTask` function in `services/actions/task.go`. Related issue: https://codeberg.org/forgejo/forgejo/issues/11261 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12768 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org> Reviewed-by: elle <0xllx0@noreply.codeberg.org>
This commit is contained in:
parent
2394f1a4e4
commit
044f3d95b6
7 changed files with 141 additions and 21 deletions
|
|
@ -6,6 +6,7 @@ package actions
|
|||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
|
@ -341,10 +342,15 @@ func GetAvailableJobsForRunner(e db.Engine, runner *ActionRunner) ([]*ActionRunJ
|
|||
return jobs, nil
|
||||
}
|
||||
|
||||
func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey, handle *string) (*ActionTask, bool, error) {
|
||||
var (
|
||||
ErrNoMatchingJobFound = errors.New("no matching job found")
|
||||
ErrNoJobUpdated = errors.New("no job updated")
|
||||
)
|
||||
|
||||
func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey, handle *string) (*ActionTask, error) {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
|
|
@ -352,7 +358,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey,
|
|||
|
||||
jobs, err := GetAvailableJobsForRunner(e, runner)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: a more efficient way to filter labels
|
||||
|
|
@ -365,10 +371,10 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey,
|
|||
}
|
||||
}
|
||||
if job == nil {
|
||||
return nil, false, nil
|
||||
return nil, ErrNoMatchingJobFound
|
||||
}
|
||||
if err := job.LoadAttributes(ctx); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := timeutil.TimeStampNow()
|
||||
|
|
@ -393,20 +399,20 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey,
|
|||
|
||||
var workflowJob *jobparser.Job
|
||||
if gots, err := jobparser.Parse(job.WorkflowPayload, false); err != nil {
|
||||
return nil, false, fmt.Errorf("parse workflow of job %d: %w", job.ID, err)
|
||||
return nil, fmt.Errorf("parse workflow of job %d: %w", job.ID, err)
|
||||
} else if len(gots) != 1 {
|
||||
return nil, false, fmt.Errorf("workflow of job %d: not single workflow", job.ID)
|
||||
return nil, fmt.Errorf("workflow of job %d: not single workflow", job.ID)
|
||||
} else { //nolint:revive
|
||||
_, workflowJob = gots[0].Job()
|
||||
}
|
||||
|
||||
if _, err := e.Insert(task); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
task.LogFilename = logFileName(job.Run.Repo.FullName(), task.ID)
|
||||
if err := UpdateTask(ctx, task, "log_filename"); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(workflowJob.Steps) > 0 {
|
||||
|
|
@ -422,7 +428,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey,
|
|||
}
|
||||
}
|
||||
if _, err := e.Insert(steps); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
task.Steps = steps
|
||||
}
|
||||
|
|
@ -430,18 +436,18 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner, requestKey,
|
|||
job.TaskID = task.ID
|
||||
// We never have to send a notification here because the job is started with a not done status.
|
||||
if n, err := UpdateRunJobWithoutNotification(ctx, job, builder.Eq{"task_id": 0}); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
} else if n != 1 {
|
||||
return nil, false, nil
|
||||
return nil, ErrNoJobUpdated
|
||||
}
|
||||
|
||||
task.Job = job
|
||||
|
||||
if err := committer.Commit(); err != nil {
|
||||
return nil, false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return task, true, nil
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// Placeholder tasks are created when the status/content of an [ActionRunJob] is resolved by Forgejo without dispatch to
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue