0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-04-26 04:33:34 +02:00
gitea/models/migrations/v1_26/v326_test.go
Zettat123 385994295d
Replace index with id in actions routes (#36842)
This PR migrates the web Actions run/job routes from index-based
`runIndex` or `jobIndex` to database IDs.

**⚠️ BREAKING ⚠️**: Existing saved links/bookmarks that use the old
index-based URLs will no longer resolve after this change.

Improvements of this change:
- Previously, `jobIndex` depended on list order, making it hard to
locate a specific job. Using `jobID` provides stable addressing.
- Web routes now align with API, which already use IDs.
- Behavior is closer to GitHub, which exposes run/job IDs in URLs.
- Provides a cleaner base for future features without relying on list
order.
- #36388 this PR improves the support for reusable workflows. If a job
uses a reusable workflow, it may contain multiple child jobs, which
makes relying on job index to locate a job much more complicated

---------

Signed-off-by: Zettat123 <zettat123@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-10 22:14:48 +01:00

105 lines
2.7 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_26
import (
"testing"
"code.gitea.io/gitea/models/migrations/base"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
_ "code.gitea.io/gitea/models/actions"
_ "code.gitea.io/gitea/models/git"
_ "code.gitea.io/gitea/models/repo"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
)
func Test_FixCommitStatusTargetURLToUseRunAndJobID(t *testing.T) {
defer test.MockVariableValue(&setting.AppSubURL, "")()
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerName string
Name string
}
type ActionRun struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index"`
Index int64
}
type ActionRunJob struct {
ID int64 `xorm:"pk autoincr"`
RunID int64 `xorm:"index"`
}
type CommitStatus struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index"`
TargetURL string
}
type CommitStatusSummary struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index"`
SHA string `xorm:"VARCHAR(64) NOT NULL"`
State string `xorm:"VARCHAR(7) NOT NULL"`
TargetURL string
}
x, deferable := base.PrepareTestEnv(t, 0,
new(Repository),
new(ActionRun),
new(ActionRunJob),
new(CommitStatus),
new(CommitStatusSummary),
)
defer deferable()
newURL1 := "/testuser/repo1/actions/runs/106/jobs/530"
newURL2 := "/testuser/repo1/actions/runs/106/jobs/531"
invalidWrongRepo := "/otheruser/badrepo/actions/runs/7/jobs/0"
invalidNonexistentRun := "/testuser/repo1/actions/runs/10/jobs/0"
invalidNonexistentJob := "/testuser/repo1/actions/runs/7/jobs/3"
externalTargetURL := "https://ci.example.com/build/123"
require.NoError(t, FixCommitStatusTargetURLToUseRunAndJobID(x))
cases := []struct {
table string
id int64
want string
}{
{table: "commit_status", id: 10, want: newURL1},
{table: "commit_status", id: 11, want: newURL2},
{table: "commit_status", id: 12, want: invalidWrongRepo},
{table: "commit_status", id: 13, want: invalidNonexistentRun},
{table: "commit_status", id: 14, want: invalidNonexistentJob},
{table: "commit_status", id: 15, want: externalTargetURL},
{table: "commit_status_summary", id: 20, want: newURL1},
{table: "commit_status_summary", id: 21, want: externalTargetURL},
}
for _, tc := range cases {
assertTargetURL(t, x, tc.table, tc.id, tc.want)
}
}
func assertTargetURL(t *testing.T, x *xorm.Engine, table string, id int64, want string) {
t.Helper()
var row struct {
TargetURL string
}
has, err := x.Table(table).Where("id=?", id).Cols("target_url").Get(&row)
require.NoError(t, err)
require.Truef(t, has, "row not found: table=%s id=%d", table, id)
require.Equal(t, want, row.TargetURL)
}