2017-12-07 21:22:02 -08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-07 21:22:02 -08:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-01-15 10:19:25 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-25 10:31:57 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-09-28 21:21:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2025-01-13 17:27:35 -08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/contexttest"
|
2021-04-06 20:44:05 +01:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2023-05-10 05:43:55 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-01-13 17:27:35 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-12-07 21:22:02 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewReleasePost(t *testing.T) {
|
2025-01-13 17:27:35 -08:00
|
|
|
unittest.PrepareTestEnv(t)
|
2017-12-07 21:22:02 -08:00
|
|
|
|
2025-01-13 17:27:35 -08:00
|
|
|
get := func(t *testing.T, tagName string) *context.Context {
|
|
|
|
ctx, _ := contexttest.MockContext(t, "user2/repo1/releases/new?tag="+tagName)
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
NewRelease(ctx)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NewReleasePage", func(t *testing.T) {
|
|
|
|
ctx := get(t, "v1.1")
|
|
|
|
assert.Empty(t, ctx.Data["ShowCreateTagOnlyButton"])
|
|
|
|
ctx = get(t, "new-tag-name")
|
|
|
|
assert.NotEmpty(t, ctx.Data["ShowCreateTagOnlyButton"])
|
|
|
|
})
|
|
|
|
|
|
|
|
post := func(t *testing.T, form forms.NewReleaseForm) *context.Context {
|
2023-09-01 19:26:07 +08:00
|
|
|
ctx, _ := contexttest.MockContext(t, "user2/repo1/releases/new")
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
2025-01-13 17:27:35 -08:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
web.SetForm(ctx, &form)
|
2021-01-26 23:36:53 +08:00
|
|
|
NewReleasePost(ctx)
|
2025-01-13 17:27:35 -08:00
|
|
|
return ctx
|
2017-12-07 21:22:02 -08:00
|
|
|
}
|
2025-01-13 17:27:35 -08:00
|
|
|
|
|
|
|
loadRelease := func(t *testing.T, tagName string) *repo_model.Release {
|
|
|
|
return unittest.GetBean(t, &repo_model.Release{}, unittest.Cond("repo_id=1 AND tag_name=?", tagName))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NewTagRelease", func(t *testing.T) {
|
|
|
|
post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "newtag",
|
|
|
|
Target: "master",
|
|
|
|
Title: "title",
|
|
|
|
Content: "content",
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "newtag")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.False(t, rel.IsTag)
|
|
|
|
assert.Equal(t, "master", rel.Target)
|
|
|
|
assert.Equal(t, "title", rel.Title)
|
|
|
|
assert.Equal(t, "content", rel.Note)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ReleaseExistsDoUpdate(non-tag)", func(t *testing.T) {
|
|
|
|
ctx := post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "v1.1",
|
|
|
|
Target: "master",
|
|
|
|
Title: "updated-title",
|
|
|
|
Content: "updated-content",
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "v1.1")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.False(t, rel.IsTag)
|
|
|
|
assert.Equal(t, "testing-release", rel.Title)
|
|
|
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ReleaseExistsDoUpdate(tag-only)", func(t *testing.T) {
|
|
|
|
ctx := post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "delete-tag", // a strange name, but it is the only "is_tag=true" fixture
|
|
|
|
Target: "master",
|
|
|
|
Title: "updated-title",
|
|
|
|
Content: "updated-content",
|
|
|
|
TagOnly: true,
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "delete-tag")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.True(t, rel.IsTag) // the record should not be updated because the request is "tag-only". TODO: need to improve the logic?
|
|
|
|
assert.Equal(t, "delete-tag", rel.Title)
|
|
|
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
|
|
|
assert.NotEmpty(t, ctx.Data["ShowCreateTagOnlyButton"]) // still show the "tag-only" button
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ReleaseExistsDoUpdate(tag-release)", func(t *testing.T) {
|
|
|
|
ctx := post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "delete-tag", // a strange name, but it is the only "is_tag=true" fixture
|
|
|
|
Target: "master",
|
|
|
|
Title: "updated-title",
|
|
|
|
Content: "updated-content",
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "delete-tag")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.False(t, rel.IsTag) // the tag has been "updated" to be a real "release"
|
|
|
|
assert.Equal(t, "updated-title", rel.Title)
|
|
|
|
assert.Empty(t, ctx.Flash.ErrorMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TagOnly", func(t *testing.T) {
|
|
|
|
ctx := post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "new-tag-only",
|
|
|
|
Target: "master",
|
|
|
|
Title: "title",
|
|
|
|
Content: "content",
|
|
|
|
TagOnly: true,
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "new-tag-only")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.True(t, rel.IsTag)
|
|
|
|
assert.Empty(t, ctx.Flash.ErrorMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TagOnlyConflict", func(t *testing.T) {
|
|
|
|
ctx := post(t, forms.NewReleaseForm{
|
|
|
|
TagName: "v1.1",
|
|
|
|
Target: "master",
|
|
|
|
Title: "title",
|
|
|
|
Content: "content",
|
|
|
|
TagOnly: true,
|
|
|
|
})
|
|
|
|
rel := loadRelease(t, "v1.1")
|
|
|
|
require.NotNil(t, rel)
|
|
|
|
assert.False(t, rel.IsTag)
|
|
|
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
|
|
|
})
|
2017-12-07 21:22:02 -08:00
|
|
|
}
|
2023-05-10 05:43:55 +02:00
|
|
|
|
2023-09-28 21:21:47 +08:00
|
|
|
func TestCalReleaseNumCommitsBehind(t *testing.T) {
|
2023-05-10 05:43:55 +02:00
|
|
|
unittest.PrepareTestEnv(t)
|
2023-09-01 19:26:07 +08:00
|
|
|
ctx, _ := contexttest.MockContext(t, "user2/repo-release/releases")
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 57)
|
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
2023-05-10 05:43:55 +02:00
|
|
|
t.Cleanup(func() { ctx.Repo.GitRepo.Close() })
|
|
|
|
|
2024-01-15 10:19:25 +08:00
|
|
|
releases, err := db.Find[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
2023-09-28 21:21:47 +08:00
|
|
|
IncludeDrafts: ctx.Repo.CanWrite(unit.TypeReleases),
|
2024-01-15 10:19:25 +08:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2023-09-28 21:21:47 +08:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
countCache := make(map[string]int64)
|
|
|
|
for _, release := range releases {
|
|
|
|
err := calReleaseNumCommitsBehind(ctx.Repo, release, countCache)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:43:55 +02:00
|
|
|
type computedFields struct {
|
|
|
|
NumCommitsBehind int64
|
|
|
|
TargetBehind string
|
|
|
|
}
|
|
|
|
expectedComputation := map[string]computedFields{
|
|
|
|
"v1.0": {
|
|
|
|
NumCommitsBehind: 3,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"v1.1": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"v2.0": {
|
|
|
|
NumCommitsBehind: 0,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"non-existing-target-branch": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"empty-target-branch": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, r := range releases {
|
|
|
|
actual := computedFields{
|
|
|
|
NumCommitsBehind: r.NumCommitsBehind,
|
|
|
|
TargetBehind: r.TargetBehind,
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedComputation[r.TagName], actual, "wrong computed fields for %s: %#v", r.TagName, r)
|
|
|
|
}
|
|
|
|
}
|