2023-03-28 14:22:07 -04:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
2024-02-05 10:33:40 -05:00
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
2023-03-28 14:22:07 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-28 08:16:40 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-03-28 14:22:07 -04:00
|
|
|
)
|
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
func createIssueConfigInDirectory(t *testing.T, user *user_model.User, repo *repo_model.Repository, dir string, issueConfig map[string]any) {
|
2023-09-28 08:16:40 -04:00
|
|
|
config, err := yaml.Marshal(issueConfig)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-03-28 14:22:07 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
err = createOrReplaceFileInBranch(user, repo, fmt.Sprintf("%s/ISSUE_TEMPLATE/config.yaml", dir), repo.DefaultBranch, string(config))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-09-28 08:16:40 -04:00
|
|
|
}
|
2023-03-28 14:22:07 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
func createIssueConfig(t *testing.T, user *user_model.User, repo *repo_model.Repository, issueConfig map[string]any) {
|
|
|
|
createIssueConfigInDirectory(t, user, repo, ".gitea", issueConfig)
|
|
|
|
}
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
func getIssueConfig(t *testing.T, owner, repo string) api.IssueConfig {
|
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issue_config", owner, repo)
|
2023-03-28 14:22:07 -04:00
|
|
|
req := NewRequest(t, "GET", urlStr)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var issueConfig api.IssueConfig
|
|
|
|
DecodeJSON(t, resp, &issueConfig)
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
return issueConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIRepoGetIssueConfig(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 49})
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
|
|
|
|
t.Run("Default", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
issueConfig := getIssueConfig(t, owner.Name, repo.Name)
|
|
|
|
|
|
|
|
assert.True(t, issueConfig.BlankIssuesEnabled)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Empty(t, issueConfig.ContactLinks)
|
2023-09-28 08:16:40 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DisableBlankIssues", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
config := make(map[string]any)
|
|
|
|
config["blank_issues_enabled"] = false
|
|
|
|
|
|
|
|
createIssueConfig(t, owner, repo, config)
|
|
|
|
|
|
|
|
issueConfig := getIssueConfig(t, owner.Name, repo.Name)
|
|
|
|
|
|
|
|
assert.False(t, issueConfig.BlankIssuesEnabled)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Empty(t, issueConfig.ContactLinks)
|
2023-09-28 08:16:40 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ContactLinks", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
contactLink := make(map[string]string)
|
|
|
|
contactLink["name"] = "TestName"
|
|
|
|
contactLink["url"] = "https://example.com"
|
|
|
|
contactLink["about"] = "TestAbout"
|
|
|
|
|
|
|
|
config := make(map[string]any)
|
|
|
|
config["contact_links"] = []map[string]string{contactLink}
|
|
|
|
|
|
|
|
createIssueConfig(t, owner, repo, config)
|
|
|
|
|
|
|
|
issueConfig := getIssueConfig(t, owner.Name, repo.Name)
|
|
|
|
|
|
|
|
assert.True(t, issueConfig.BlankIssuesEnabled)
|
|
|
|
assert.Len(t, issueConfig.ContactLinks, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, "TestName", issueConfig.ContactLinks[0].Name)
|
|
|
|
assert.Equal(t, "https://example.com", issueConfig.ContactLinks[0].URL)
|
|
|
|
assert.Equal(t, "TestAbout", issueConfig.ContactLinks[0].About)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Full", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
contactLink := make(map[string]string)
|
|
|
|
contactLink["name"] = "TestName"
|
|
|
|
contactLink["url"] = "https://example.com"
|
|
|
|
contactLink["about"] = "TestAbout"
|
|
|
|
|
|
|
|
config := make(map[string]any)
|
|
|
|
config["blank_issues_enabled"] = false
|
|
|
|
config["contact_links"] = []map[string]string{contactLink}
|
|
|
|
|
|
|
|
createIssueConfig(t, owner, repo, config)
|
|
|
|
|
|
|
|
issueConfig := getIssueConfig(t, owner.Name, repo.Name)
|
|
|
|
|
|
|
|
assert.False(t, issueConfig.BlankIssuesEnabled)
|
|
|
|
assert.Len(t, issueConfig.ContactLinks, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, "TestName", issueConfig.ContactLinks[0].Name)
|
|
|
|
assert.Equal(t, "https://example.com", issueConfig.ContactLinks[0].URL)
|
|
|
|
assert.Equal(t, "TestAbout", issueConfig.ContactLinks[0].About)
|
|
|
|
})
|
2023-03-28 14:22:07 -04:00
|
|
|
}
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
func TestAPIRepoIssueConfigPaths(t *testing.T) {
|
2023-03-28 14:22:07 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 49})
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
|
|
|
|
templateConfigCandidates := []string{
|
2024-02-05 10:33:40 -05:00
|
|
|
".forgejo/ISSUE_TEMPLATE/config",
|
|
|
|
".forgejo/issue_template/config",
|
2023-09-28 08:16:40 -04:00
|
|
|
".gitea/ISSUE_TEMPLATE/config",
|
|
|
|
".gitea/issue_template/config",
|
|
|
|
".github/ISSUE_TEMPLATE/config",
|
|
|
|
".github/issue_template/config",
|
|
|
|
}
|
|
|
|
|
2024-05-09 09:49:37 -04:00
|
|
|
for _, candidate := range templateConfigCandidates {
|
2023-09-28 08:16:40 -04:00
|
|
|
for _, extension := range []string{".yaml", ".yml"} {
|
2024-05-09 09:49:37 -04:00
|
|
|
fullPath := candidate + extension
|
2023-09-28 08:16:40 -04:00
|
|
|
t.Run(fullPath, func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
configMap := make(map[string]any)
|
|
|
|
configMap["blank_issues_enabled"] = false
|
|
|
|
|
|
|
|
configData, err := yaml.Marshal(configMap)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-09-28 08:16:40 -04:00
|
|
|
|
|
|
|
_, err = createFileInBranch(owner, repo, fullPath, repo.DefaultBranch, string(configData))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-09-28 08:16:40 -04:00
|
|
|
|
|
|
|
issueConfig := getIssueConfig(t, owner.Name, repo.Name)
|
|
|
|
|
|
|
|
assert.False(t, issueConfig.BlankIssuesEnabled)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Empty(t, issueConfig.ContactLinks)
|
2023-09-28 08:16:40 -04:00
|
|
|
|
|
|
|
_, err = deleteFileInBranch(owner, repo, fullPath, repo.DefaultBranch)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-09-28 08:16:40 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIRepoValidateIssueConfig(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 49})
|
2023-03-28 14:22:07 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
|
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issue_config/validate", owner.Name, repo.Name)
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
t.Run("Valid", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2023-09-28 08:16:40 -04:00
|
|
|
req := NewRequest(t, "GET", urlStr)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var issueConfigValidation api.IssueConfigValidation
|
|
|
|
DecodeJSON(t, resp, &issueConfigValidation)
|
|
|
|
|
|
|
|
assert.True(t, issueConfigValidation.Valid)
|
|
|
|
assert.Empty(t, issueConfigValidation.Message)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
dirs := []string{".gitea", ".forgejo"}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
t.Run(dir, func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
defer func() {
|
|
|
|
deleteFileInBranch(owner, repo, fmt.Sprintf("%s/ISSUE_TEMPLATE/config.yaml", dir), repo.DefaultBranch)
|
|
|
|
}()
|
2023-09-28 08:16:40 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
config := make(map[string]any)
|
|
|
|
config["blank_issues_enabled"] = "Test"
|
2023-09-28 08:16:40 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
createIssueConfigInDirectory(t, owner, repo, dir, config)
|
2023-09-28 08:16:40 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
req := NewRequest(t, "GET", urlStr)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var issueConfigValidation api.IssueConfigValidation
|
|
|
|
DecodeJSON(t, resp, &issueConfigValidation)
|
2023-03-28 14:22:07 -04:00
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
assert.False(t, issueConfigValidation.Valid)
|
|
|
|
assert.NotEmpty(t, issueConfigValidation.Message)
|
|
|
|
})
|
|
|
|
}
|
2023-09-28 08:16:40 -04:00
|
|
|
})
|
2023-03-28 14:22:07 -04:00
|
|
|
}
|