refactor: move rerun logic to services (#12141)

Move the logic for handling reruns of Forgejo Action workflows and individual jobs to services. That is a prerequisite for adding the corresponding HTTP API endpoints.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

(can be removed for JavaScript changes)

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

### Tests for JavaScript changes

(can be removed for Go changes)

- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

*The decision if the pull request will be shown in the release notes is up to the mergers / release team.*

The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12141
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
Co-committed-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
This commit is contained in:
Andreas Ahlenstorf 2026-04-19 22:08:00 +02:00 committed by Mathieu Fenniak
parent 178a0a25f8
commit 6cd3f0263d
12 changed files with 1122 additions and 140 deletions

View file

@ -255,19 +255,33 @@ func (run *ActionRun) IsDispatchedRun() bool {
return run.TriggerEvent == "workflow_dispatch"
}
// IsRunnable indicates whether this ActionRun can generally be run.
func (run *ActionRun) IsRunnable() bool {
// IsValid indicates whether this ActionRun is valid and can be run.
func (run *ActionRun) IsValid() bool {
return run.PreExecutionErrorCode == 0 && run.PreExecutionError == ""
}
// CanBeRerun indicates whether this ActionRun can be rerun.
func (run *ActionRun) CanBeRerun() bool {
if !run.IsRunnable() {
if !run.IsValid() {
return false
}
return run.Status.IsDone()
}
func (run *ActionRun) PrepareNextAttempt() error {
if run.Status != StatusUnknown && !run.Status.IsDone() {
return fmt.Errorf("cannot prepare next attempt because run %d is active: %s", run.ID, run.Status.String())
}
run.PreviousDuration = run.Duration()
run.Status = StatusWaiting
run.Started = 0
run.Stopped = 0
return nil
}
func actionsCountOpenCacheKey(repoID int64) string {
return fmt.Sprintf("Actions:CountOpenActionRuns:%d", repoID)
}

View file

@ -141,12 +141,16 @@ func (job *ActionRunJob) PrepareNextAttempt(initialStatus Status) error {
}
// CanBeRerun answers whether this ActionRunJob can be rerun. Returns true if it is done and the Run it belongs to
// is runnable. Returns false in all other cases, including when Run is nil.
func (job *ActionRunJob) CanBeRerun() bool {
if job.Run == nil || !job.Run.IsRunnable() {
return false
// is valid. Returns false in all other cases.
func (job *ActionRunJob) CanBeRerun(ctx context.Context) (bool, error) {
if err := job.LoadRun(ctx); err != nil {
return false, fmt.Errorf("cannot load run %d of job %d: %w", job.RunID, job.ID, err)
}
return job.Status.IsDone()
if !job.Run.IsValid() {
return false, nil
}
return job.Status.IsDone(), nil
}
func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {

View file

@ -428,9 +428,10 @@ func TestAllNeedsExist(t *testing.T) {
func TestActionRunJob_CanBeRerun(t *testing.T) {
testCases := []struct {
name string
job ActionRunJob
canBeRerun bool
name string
job ActionRunJob
canBeRerun bool
expectedError string
}{
{
name: "job with unknown status",
@ -468,9 +469,9 @@ func TestActionRunJob_CanBeRerun(t *testing.T) {
canBeRerun: false,
},
{
name: "ActionRun is nil",
job: ActionRunJob{Run: nil, Status: StatusSuccess},
canBeRerun: false,
name: "ActionRun is nil",
job: ActionRunJob{ID: 12, Run: nil, Status: StatusSuccess},
expectedError: "cannot load run 0 of job 12",
},
{
name: "with busy run but completed job",
@ -489,7 +490,15 @@ func TestActionRunJob_CanBeRerun(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.canBeRerun, testCase.job.CanBeRerun())
result, err := testCase.job.CanBeRerun(t.Context())
if testCase.expectedError == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, testCase.expectedError)
}
assert.Equal(t, testCase.canBeRerun, result)
})
}
}

View file

@ -96,27 +96,27 @@ func TestIsManualRun(t *testing.T) {
assert.False(t, pushRun.IsDispatchedRun())
}
func TestActionRun_IsRunnable(t *testing.T) {
func TestActionRun_IsValid(t *testing.T) {
testCases := []struct {
name string
run ActionRun
isRunnable bool
name string
run ActionRun
isValid bool
}{
{
name: "valid run",
run: ActionRun{},
isRunnable: true,
name: "valid run",
run: ActionRun{},
isValid: true,
},
{
name: "with pre-execution error",
run: ActionRun{PreExecutionErrorCode: ErrorCodeIncompleteRunsOnMissingOutput},
isRunnable: false,
name: "with pre-execution error",
run: ActionRun{PreExecutionErrorCode: ErrorCodeIncompleteRunsOnMissingOutput},
isValid: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.isRunnable, testCase.run.IsRunnable())
assert.Equal(t, testCase.isValid, testCase.run.IsValid())
})
}
}