2024-08-25 14:52:21 +02:00
|
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-06-26 20:06:21 +02:00
|
|
|
|
"forgejo.org/models/db"
|
|
|
|
|
issues_model "forgejo.org/models/issues"
|
|
|
|
|
repo_model "forgejo.org/models/repo"
|
2025-03-27 19:40:14 +00:00
|
|
|
|
unit_model "forgejo.org/models/unit"
|
|
|
|
|
"forgejo.org/models/unittest"
|
|
|
|
|
user_model "forgejo.org/models/user"
|
|
|
|
|
"forgejo.org/modules/git"
|
2025-04-19 04:33:07 +00:00
|
|
|
|
"forgejo.org/modules/indexer/stats"
|
2025-06-26 20:06:21 +02:00
|
|
|
|
"forgejo.org/modules/optional"
|
|
|
|
|
"forgejo.org/modules/timeutil"
|
|
|
|
|
issue_service "forgejo.org/services/issue"
|
2025-03-27 19:40:14 +00:00
|
|
|
|
files_service "forgejo.org/services/repository/files"
|
|
|
|
|
"forgejo.org/tests"
|
2024-08-25 14:52:21 +02:00
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2025-06-26 20:06:21 +02:00
|
|
|
|
"xorm.io/xorm/convert"
|
2024-08-25 14:52:21 +02:00
|
|
|
|
)
|
|
|
|
|
|
2024-09-05 19:21:21 +02:00
|
|
|
|
// first entry represents filename
|
|
|
|
|
// the following entries define the full file content over time
|
2024-11-10 20:09:53 +01:00
|
|
|
|
type FileChanges struct {
|
|
|
|
|
Filename string
|
|
|
|
|
CommitMsg string
|
|
|
|
|
Versions []string
|
|
|
|
|
}
|
2024-08-25 14:52:21 +02:00
|
|
|
|
|
2025-06-26 20:06:21 +02:00
|
|
|
|
// performs additional repo setup as needed
|
|
|
|
|
type SetupRepo func(*user_model.User, *repo_model.Repository)
|
|
|
|
|
|
2024-08-25 14:52:21 +02:00
|
|
|
|
// put your Git repo declarations in here
|
|
|
|
|
// feel free to amend the helper function below or use the raw variant directly
|
|
|
|
|
func DeclareGitRepos(t *testing.T) func() {
|
2025-06-26 20:06:21 +02:00
|
|
|
|
now := timeutil.TimeStampNow()
|
|
|
|
|
postIssue := func(repo *repo_model.Repository, user *user_model.User, age int64, title, content string) {
|
|
|
|
|
issue := &issues_model.Issue{
|
|
|
|
|
RepoID: repo.ID,
|
|
|
|
|
PosterID: user.ID,
|
|
|
|
|
Title: title,
|
|
|
|
|
Content: content,
|
|
|
|
|
CreatedUnix: now.Add(-age),
|
|
|
|
|
}
|
|
|
|
|
require.NoError(t, issue_service.NewIssue(db.DefaultContext, repo, issue, nil, nil, nil))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 19:21:21 +02:00
|
|
|
|
cleanupFunctions := []func(){
|
2025-06-26 20:06:21 +02:00
|
|
|
|
newRepo(t, 2, "diff-test", nil, []FileChanges{{
|
2024-11-10 20:09:53 +01:00
|
|
|
|
Filename: "testfile",
|
|
|
|
|
Versions: []string{"hello", "hallo", "hola", "native", "ubuntu-latest", "- runs-on: ubuntu-latest", "- runs-on: debian-latest"},
|
2025-06-26 20:06:21 +02:00
|
|
|
|
}}, nil),
|
|
|
|
|
newRepo(t, 2, "language-stats-test", nil, []FileChanges{{
|
2025-04-19 04:33:07 +00:00
|
|
|
|
Filename: "main.rs",
|
|
|
|
|
Versions: []string{"fn main() {", "println!(\"Hello World!\");", "}"},
|
2025-06-26 20:06:21 +02:00
|
|
|
|
}}, nil),
|
|
|
|
|
newRepo(t, 2, "mentions-highlighted", nil, []FileChanges{
|
2024-11-10 20:09:53 +01:00
|
|
|
|
{
|
|
|
|
|
Filename: "history1.md",
|
|
|
|
|
Versions: []string{""},
|
|
|
|
|
CommitMsg: "A commit message which mentions @user2 in the title\nand has some additional text which mentions @user1",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Filename: "history2.md",
|
|
|
|
|
Versions: []string{""},
|
|
|
|
|
CommitMsg: "Another commit which mentions @user1 in the title\nand @user2 in the text",
|
|
|
|
|
},
|
2025-06-26 20:06:21 +02:00
|
|
|
|
}, nil),
|
|
|
|
|
newRepo(t, 2, "unicode-escaping", nil, []FileChanges{{
|
2025-05-28 05:16:19 +02:00
|
|
|
|
Filename: "a-file",
|
|
|
|
|
Versions: []string{"{a}{а}"},
|
2025-06-26 20:06:21 +02:00
|
|
|
|
}}, nil),
|
2025-07-14 13:24:45 +02:00
|
|
|
|
newRepo(t, 2, "multiple-combo-boxes", nil, []FileChanges{{
|
|
|
|
|
Filename: ".forgejo/issue_template/multi-combo-boxes.yaml",
|
|
|
|
|
Versions: []string{`
|
|
|
|
|
name: "Multiple combo-boxes"
|
|
|
|
|
description: "To show something"
|
|
|
|
|
body:
|
|
|
|
|
- type: textarea
|
|
|
|
|
id: textarea-one
|
|
|
|
|
attributes:
|
|
|
|
|
label: one
|
|
|
|
|
- type: textarea
|
|
|
|
|
id: textarea-two
|
|
|
|
|
attributes:
|
|
|
|
|
label: two
|
|
|
|
|
`},
|
|
|
|
|
}}, nil),
|
2025-06-26 20:06:21 +02:00
|
|
|
|
newRepo(t, 11, "dependency-test", &tests.DeclarativeRepoOptions{
|
|
|
|
|
UnitConfig: optional.Some(map[unit_model.Type]convert.Conversion{
|
|
|
|
|
unit_model.TypeIssues: &repo_model.IssuesConfig{
|
|
|
|
|
EnableDependencies: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}, []FileChanges{}, func(user *user_model.User, repo *repo_model.Repository) {
|
|
|
|
|
postIssue(repo, user, 500, "first issue here", "an issue created earlier")
|
|
|
|
|
postIssue(repo, user, 400, "second issue here (not 1)", "not the right issue, but in the right repo")
|
|
|
|
|
postIssue(repo, user, 300, "third issue here", "depends on things")
|
|
|
|
|
postIssue(repo, user, 200, "unrelated issue", "shrug emoji")
|
|
|
|
|
postIssue(repo, user, 100, "newest issue", "very new")
|
|
|
|
|
}),
|
|
|
|
|
newRepo(t, 11, "dependency-test-2", &tests.DeclarativeRepoOptions{
|
|
|
|
|
UnitConfig: optional.Some(map[unit_model.Type]convert.Conversion{
|
|
|
|
|
unit_model.TypeIssues: &repo_model.IssuesConfig{
|
|
|
|
|
EnableDependencies: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}, []FileChanges{}, func(user *user_model.User, repo *repo_model.Repository) {
|
|
|
|
|
postIssue(repo, user, 450, "right issue", "an issue containing word right")
|
|
|
|
|
postIssue(repo, user, 150, "left issue", "an issue containing word left")
|
|
|
|
|
}),
|
2024-09-05 19:21:21 +02:00
|
|
|
|
// add your repo declarations here
|
|
|
|
|
}
|
2024-08-25 14:52:21 +02:00
|
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
|
for _, cleanup := range cleanupFunctions {
|
|
|
|
|
cleanup()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 20:06:21 +02:00
|
|
|
|
func newRepo(t *testing.T, userID int64, repoName string, initOpts *tests.DeclarativeRepoOptions, fileChanges []FileChanges, setup SetupRepo) func() {
|
2024-08-25 14:52:21 +02:00
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
|
2025-06-26 20:06:21 +02:00
|
|
|
|
|
|
|
|
|
opts := tests.DeclarativeRepoOptions{}
|
|
|
|
|
if initOpts != nil {
|
|
|
|
|
opts = *initOpts
|
|
|
|
|
}
|
|
|
|
|
opts.Name = optional.Some(repoName)
|
|
|
|
|
if !opts.EnabledUnits.Has() {
|
|
|
|
|
opts.EnabledUnits = optional.Some([]unit_model.Type{unit_model.TypeCode, unit_model.TypeIssues})
|
|
|
|
|
}
|
|
|
|
|
somerepo, _, cleanupFunc := tests.CreateDeclarativeRepoWithOptions(t, user, opts)
|
2024-08-25 14:52:21 +02:00
|
|
|
|
|
2025-06-12 00:13:39 +02:00
|
|
|
|
var lastCommitID string
|
2024-08-25 14:52:21 +02:00
|
|
|
|
for _, file := range fileChanges {
|
2024-11-10 20:09:53 +01:00
|
|
|
|
for i, version := range file.Versions {
|
|
|
|
|
operation := "update"
|
|
|
|
|
if i == 0 {
|
|
|
|
|
operation = "create"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// default to unique commit messages
|
|
|
|
|
commitMsg := file.CommitMsg
|
|
|
|
|
if commitMsg == "" {
|
|
|
|
|
commitMsg = fmt.Sprintf("Patch: %s-%d", file.Filename, i+1)
|
2024-08-25 14:52:21 +02:00
|
|
|
|
}
|
2024-11-10 20:09:53 +01:00
|
|
|
|
|
2024-08-25 14:52:21 +02:00
|
|
|
|
resp, err := files_service.ChangeRepoFiles(git.DefaultContext, somerepo, user, &files_service.ChangeRepoFilesOptions{
|
|
|
|
|
Files: []*files_service.ChangeRepoFile{{
|
|
|
|
|
Operation: operation,
|
2024-11-10 20:09:53 +01:00
|
|
|
|
TreePath: file.Filename,
|
|
|
|
|
ContentReader: strings.NewReader(version),
|
2024-08-25 14:52:21 +02:00
|
|
|
|
}},
|
2024-11-10 20:09:53 +01:00
|
|
|
|
Message: commitMsg,
|
2024-08-25 14:52:21 +02:00
|
|
|
|
OldBranch: "main",
|
|
|
|
|
NewBranch: "main",
|
|
|
|
|
Author: &files_service.IdentityOptions{
|
|
|
|
|
Name: user.Name,
|
|
|
|
|
Email: user.Email,
|
|
|
|
|
},
|
|
|
|
|
Committer: &files_service.IdentityOptions{
|
|
|
|
|
Name: user.Name,
|
|
|
|
|
Email: user.Email,
|
|
|
|
|
},
|
|
|
|
|
Dates: &files_service.CommitDateOptions{
|
|
|
|
|
Author: time.Now(),
|
|
|
|
|
Committer: time.Now(),
|
|
|
|
|
},
|
2025-06-12 00:13:39 +02:00
|
|
|
|
LastCommitID: lastCommitID,
|
2024-08-25 14:52:21 +02:00
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, resp)
|
2025-06-12 00:13:39 +02:00
|
|
|
|
|
|
|
|
|
lastCommitID = resp.Commit.SHA
|
2024-08-25 14:52:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 20:06:21 +02:00
|
|
|
|
if setup != nil {
|
|
|
|
|
setup(user, somerepo)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 04:33:07 +00:00
|
|
|
|
err := stats.UpdateRepoIndexer(somerepo)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2024-08-25 14:52:21 +02:00
|
|
|
|
return cleanupFunc
|
|
|
|
|
}
|