0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-13 17:35:18 +02:00

fix: Sort action run jobs by JobID and Name with matrix examples (#37046)

Fix the sorting of jobs out of a matrix

## Before
<img width="415" height="487" alt="grafik"
src="https://github.com/user-attachments/assets/b628adb9-9158-4106-89f1-d8ecaa98f17d"
/>


## After

<img width="423" height="365" alt="grafik"
src="https://github.com/user-attachments/assets/d26223d5-96da-4bdc-bbfe-389101d28cc8"
/>

---------

Signed-off-by: Nicolas <bircni@icloud.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Nicolas 2026-05-13 09:30:22 +02:00 committed by GitHub
parent 3738809219
commit 187daac598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 0 deletions

View File

@ -5,9 +5,11 @@ package actions
import (
"context"
"slices"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/timeutil"
@ -23,6 +25,22 @@ func (jobs ActionJobList) GetRunIDs() []int64 {
})
}
// SortMatrixGroupsByName natural-sorts each contiguous run of jobs that share a JobID
// so matrix expansions (e.g. "test (1)", "test (2)", "test (10)") appear in human order.
// Input is expected to be in DB id order so JobID groups are contiguous; cross-group order is preserved.
func (jobs ActionJobList) SortMatrixGroupsByName() {
for i := 0; i < len(jobs); {
j := i + 1
for j < len(jobs) && jobs[j].JobID == jobs[i].JobID {
j++
}
slices.SortFunc(jobs[i:j], func(a, b *ActionRunJob) int {
return base.NaturalSortCompare(a.Name, b.Name)
})
i = j
}
}
func (jobs ActionJobList) LoadRepos(ctx context.Context) error {
repoIDs := container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
return j.RepoID, j.RepoID != 0 && j.Repo == nil

View File

@ -0,0 +1,61 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestActionJobList_SortMatrixGroupsByName(t *testing.T) {
mk := func(jobID, name string) *ActionRunJob {
return &ActionRunJob{JobID: jobID, Name: name}
}
names := func(jobs ActionJobList) []string {
out := make([]string, len(jobs))
for i, j := range jobs {
out[i] = j.Name
}
return out
}
t.Run("matrix group sorted naturally", func(t *testing.T) {
jobs := ActionJobList{
mk("build", "build"),
mk("test", "test (10)"),
mk("test", "test (2)"),
mk("test", "test (1)"),
mk("deploy", "deploy"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"build", "test (1)", "test (2)", "test (10)", "deploy"}, names(jobs))
})
t.Run("non-adjacent same JobID stays in input order", func(t *testing.T) {
jobs := ActionJobList{
mk("test", "test (10)"),
mk("build", "build"),
mk("test", "test (1)"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"test (10)", "build", "test (1)"}, names(jobs))
})
t.Run("groups stay in input order", func(t *testing.T) {
jobs := ActionJobList{
mk("z", "z"),
mk("a", "a"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"z", "a"}, names(jobs))
})
t.Run("empty and singleton", func(t *testing.T) {
ActionJobList(nil).SortMatrixGroupsByName()
jobs := ActionJobList{mk("only", "only")}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"only"}, names(jobs))
})
}

View File

@ -1175,6 +1175,7 @@ func getCurrentRepoActionRunJobsByID(ctx *context.APIContext) (*actions_model.Ac
ctx.APIErrorInternal(err)
return nil, nil
}
jobs.SortMatrixGroupsByName()
return run, jobs
}

View File

@ -893,6 +893,7 @@ func getCurrentRunJobsByPathParam(ctx *context_module.Context) (*actions_model.A
ctx.NotFound(nil)
return nil, nil, nil
}
jobs.SortMatrixGroupsByName()
for _, job := range jobs {
job.Run = run