Skip to content

Commit 64bf223

Browse files
committed
fix nits
1 parent e66a51e commit 64bf223

File tree

6 files changed

+58
-52
lines changed

6 files changed

+58
-52
lines changed

models/actions/run_job.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,5 @@ func IsErrUnevaluatedConcurrency(err error) bool {
293293
}
294294

295295
func (err ErrUnevaluatedConcurrency) Error() string {
296-
return fmt.Sprintf("the raw concurrency [group=%s, cancel-in-progress=%s] has not been evaluated", err.Group, err.CancelInProgress)
296+
return fmt.Sprintf("the raw concurrency [group=%s, cancel-in-progress=%s] is not evaluated", err.Group, err.CancelInProgress)
297297
}

models/actions/run_job_list.go

-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) err
4646
return jobs.LoadRuns(ctx, withRepo)
4747
}
4848

49-
func GetRunsByIDs(ctx context.Context, runIDs []int64) (RunList, error) {
50-
runList := make(RunList, 0, len(runIDs))
51-
err := db.GetEngine(ctx).In("id", runIDs).Find(&runList)
52-
return runList, err
53-
}
54-
5549
type FindRunJobOptions struct {
5650
db.ListOptions
5751
RunID int64

services/actions/concurrency.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 The Gitea Authors. All rights reserved.
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package actions

services/actions/job_emitter.go

+35-26
Original file line numberDiff line numberDiff line change
@@ -100,43 +100,53 @@ func checkJobsByRunID(ctx context.Context, runID int64) error {
100100
}
101101

102102
// check job concurrency
103-
concurrentJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
103+
runJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
104104
if err != nil {
105105
return err
106106
}
107-
for _, job := range concurrentJobs {
107+
for _, job := range runJobs {
108108
if job.Status.IsDone() && job.ConcurrencyGroup != "" {
109-
concurrentJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
109+
waitingConcurrentJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
110110
RepoID: job.RepoID,
111111
ConcurrencyGroup: job.ConcurrencyGroup,
112-
Statuses: []actions_model.Status{actions_model.StatusBlocked},
112+
Statuses: []actions_model.Status{actions_model.StatusWaiting},
113113
})
114114
if err != nil {
115115
return err
116116
}
117-
for _, concurrentJob := range concurrentJobs {
118-
if concurrentRunIDs.Contains(concurrentJob.RunID) {
119-
continue
120-
}
121-
concurrentRunIDs.Add(concurrentJob.RunID)
122-
concurrentRun, err := actions_model.GetRunByID(ctx, concurrentJob.RunID)
123-
if err != nil {
124-
return err
125-
}
126-
if concurrentRun.NeedApproval {
127-
continue
128-
}
129-
if jobs, err := checkJobsOfRun(ctx, concurrentRun); err != nil {
130-
return err
131-
} else {
132-
jobsToCreateCommitStatus = append(jobsToCreateCommitStatus, jobs...)
133-
}
134-
updatedJob, err := actions_model.GetRunJobByID(ctx, concurrentJob.ID)
117+
if len(waitingConcurrentJobs) == 0 {
118+
blockedConcurrentJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
119+
RepoID: job.RepoID,
120+
ConcurrencyGroup: job.ConcurrencyGroup,
121+
Statuses: []actions_model.Status{actions_model.StatusBlocked},
122+
})
135123
if err != nil {
136124
return err
137125
}
138-
if updatedJob.Status == actions_model.StatusWaiting {
139-
break
126+
for _, concurrentJob := range blockedConcurrentJobs {
127+
if concurrentRunIDs.Contains(concurrentJob.RunID) {
128+
continue
129+
}
130+
concurrentRunIDs.Add(concurrentJob.RunID)
131+
concurrentRun, err := actions_model.GetRunByID(ctx, concurrentJob.RunID)
132+
if err != nil {
133+
return err
134+
}
135+
if concurrentRun.NeedApproval {
136+
continue
137+
}
138+
if jobs, err := checkJobsOfRun(ctx, concurrentRun); err != nil {
139+
return err
140+
} else {
141+
jobsToCreateCommitStatus = append(jobsToCreateCommitStatus, jobs...)
142+
}
143+
updatedJob, err := actions_model.GetRunJobByID(ctx, concurrentJob.ID)
144+
if err != nil {
145+
return err
146+
}
147+
if updatedJob.Status == actions_model.StatusWaiting {
148+
break
149+
}
140150
}
141151
}
142152
}
@@ -287,15 +297,14 @@ func (r *jobStatusResolver) resolve(ctx context.Context) map[int64]actions_model
287297
}
288298

289299
func checkConcurrencyForJobWithNeeds(ctx context.Context, actionRunJob *actions_model.ActionRunJob, vars map[string]string) (bool, error) {
290-
if len(actionRunJob.RawConcurrencyGroup) == 0 {
300+
if actionRunJob.RawConcurrencyGroup == "" {
291301
return false, nil
292302
}
293303
if err := actionRunJob.LoadAttributes(ctx); err != nil {
294304
return false, err
295305
}
296306

297307
if !actionRunJob.IsConcurrencyEvaluated {
298-
// empty concurrency group means the raw concurrency has not been evaluated
299308
taskNeeds, err := FindTaskNeeds(ctx, actionRunJob)
300309
if err != nil {
301310
return false, fmt.Errorf("find task needs: %w", err)

services/actions/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 The Gitea Authors. All rights reserved.
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package actions

tests/integration/actions_concurrency_test.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
14
package integration
25

36
import (
@@ -33,6 +36,9 @@ func TestWorkflowConcurrency(t *testing.T) {
3336

3437
apiRepo := createActionsTestRepo(t, token, "actions-concurrency", false)
3538
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
39+
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
40+
defer doAPIDeleteRepository(httpContext)(t)
41+
3642
runner := newMockRunner()
3743
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
3844

@@ -124,13 +130,10 @@ jobs:
124130
runner.execTask(t, task, &mockTaskOutcome{
125131
result: runnerv1.Result_RESULT_SUCCESS,
126132
})
127-
128-
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
129-
doAPIDeleteRepository(httpContext)(t)
130133
})
131134
}
132135

133-
func TestWorkflowConcurrency_WithPullRequest(t *testing.T) {
136+
func TestPullRequestWorkflowConcurrency(t *testing.T) {
134137
onGiteaRun(t, func(t *testing.T, u *url.URL) {
135138
// user2 is the owner of the base repo
136139
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
@@ -143,6 +146,7 @@ func TestWorkflowConcurrency_WithPullRequest(t *testing.T) {
143146
apiBaseRepo := createActionsTestRepo(t, user2Token, "actions-concurrency", false)
144147
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiBaseRepo.ID})
145148
user2APICtx := NewAPITestContext(t, baseRepo.OwnerName, baseRepo.Name, auth_model.AccessTokenScopeWriteRepository)
149+
defer doAPIDeleteRepository(user2APICtx)(t)
146150

147151
runner := newMockRunner()
148152
runner.registerAsRepoRunner(t, baseRepo.OwnerName, baseRepo.Name, "mock-runner", []string{"ubuntu-latest"})
@@ -199,6 +203,7 @@ jobs:
199203
DecodeJSON(t, resp, &apiForkRepo)
200204
forkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiForkRepo.ID})
201205
user4APICtx := NewAPITestContext(t, user4.Name, forkRepo.Name, auth_model.AccessTokenScopeWriteRepository)
206+
defer doAPIDeleteRepository(user4APICtx)(t)
202207

203208
// user4 creates a pull request from branch "bugfix/bbb"
204209
doAPICreateFile(user4APICtx, "user4-fix.txt", &api.CreateFileOptions{
@@ -274,9 +279,6 @@ jobs:
274279
assert.Equal(t, "pull-request-test", pr3Run1.ConcurrencyGroup)
275280
assert.False(t, pr3Run1.ConcurrencyCancel)
276281
assert.Equal(t, actions_model.StatusRunning, pr3Run1.Status)
277-
278-
doAPIDeleteRepository(user4APICtx)(t)
279-
doAPIDeleteRepository(user2APICtx)(t)
280282
})
281283
}
282284

@@ -288,6 +290,9 @@ func TestJobConcurrency(t *testing.T) {
288290

289291
apiRepo := createActionsTestRepo(t, token, "actions-concurrency", false)
290292
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
293+
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
294+
defer doAPIDeleteRepository(httpContext)(t)
295+
291296
runner1 := newMockRunner()
292297
runner1.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner-1", []string{"runner1"})
293298
runner2 := newMockRunner()
@@ -394,8 +399,6 @@ jobs:
394399
_, wf2Job2ActionJob, _ = getTaskAndJobAndRunByTaskID(t, wf2Job2Task.Id)
395400
assert.Equal(t, actions_model.StatusCancelled, wf2Job2ActionJob.Status)
396401

397-
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
398-
doAPIDeleteRepository(httpContext)(t)
399402
})
400403
}
401404

@@ -407,6 +410,9 @@ func TestWorkflowDispatchConcurrency(t *testing.T) {
407410

408411
apiRepo := createActionsTestRepo(t, token, "actions-concurrency", false)
409412
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
413+
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
414+
defer doAPIDeleteRepository(httpContext)(t)
415+
410416
runner := newMockRunner()
411417
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
412418

@@ -487,9 +493,6 @@ jobs:
487493
assert.Equal(t, "workflow-dispatch-v1.22", run4.ConcurrencyGroup)
488494
_, _, run2 = getTaskAndJobAndRunByTaskID(t, task2.Id)
489495
assert.Equal(t, actions_model.StatusCancelled, run2.Status)
490-
491-
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
492-
doAPIDeleteRepository(httpContext)(t)
493496
})
494497
}
495498

@@ -503,6 +506,7 @@ func TestScheduleConcurrency(t *testing.T) {
503506
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
504507
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
505508
defer doAPIDeleteRepository(httpContext)(t)
509+
506510
runner := newMockRunner()
507511
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
508512

@@ -601,12 +605,14 @@ func TestWorkflowAndJobConcurrency(t *testing.T) {
601605

602606
apiRepo := createActionsTestRepo(t, token, "actions-concurrency", false)
603607
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
608+
609+
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
610+
defer doAPIDeleteRepository(httpContext)(t)
611+
604612
runner1 := newMockRunner()
605613
runner1.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner-1", []string{"runner1"})
606614
runner2 := newMockRunner()
607615
runner2.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner-2", []string{"runner2"})
608-
runner3 := newMockRunner()
609-
runner3.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner-3", []string{"runner3"})
610616

611617
wf1TreePath := ".gitea/workflows/concurrent-workflow-1.yml"
612618
wf1FileContent := `name: concurrent-workflow-1
@@ -759,9 +765,6 @@ jobs:
759765
assert.Equal(t, "job-group-2", w4j1Job.ConcurrencyGroup)
760766
assert.Equal(t, "workflow-group-2", w4Run.ConcurrencyGroup)
761767
assert.Equal(t, "concurrent-workflow-4.yml", w4Run.WorkflowID)
762-
763-
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
764-
doAPIDeleteRepository(httpContext)(t)
765768
})
766769
}
767770

0 commit comments

Comments
 (0)