Merge pull request 'fix: template config path (#2836)' (#6335) from Snoweuph/forgejo:fix/template-file into forgejo
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6335
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-12-22 14:53:16 +00:00
commit 54255688f5
2 changed files with 94 additions and 16 deletions

View file

@ -126,24 +126,32 @@ func (gt *GiteaTemplate) Globs() []glob.Glob {
} }
func checkGiteaTemplate(tmpDir string) (*GiteaTemplate, error) { func checkGiteaTemplate(tmpDir string) (*GiteaTemplate, error) {
gtPath := filepath.Join(tmpDir, ".gitea", "template") configDirs := []string{".forgejo", ".gitea"}
if _, err := os.Stat(gtPath); os.IsNotExist(err) { var templateFilePath string
return nil, nil
} else if err != nil { for _, dir := range configDirs {
candidatePath := filepath.Join(tmpDir, dir, "template")
if _, err := os.Stat(candidatePath); err == nil {
templateFilePath = candidatePath
break
} else if !os.IsNotExist(err) {
return nil, err return nil, err
} }
}
content, err := os.ReadFile(gtPath) if templateFilePath == "" {
return nil, nil
}
content, err := os.ReadFile(templateFilePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
gt := &GiteaTemplate{ return &GiteaTemplate{
Path: gtPath, Path: templateFilePath,
Content: content, Content: content,
} }, nil
return gt, nil
} }
func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error { func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error {

View file

@ -7,6 +7,7 @@ package integration
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -14,9 +15,11 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/translation"
files_service "code.gitea.io/gitea/services/repository/files"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -76,10 +79,14 @@ func testRepoGenerate(t *testing.T, session *TestSession, templateID, templateOw
// Step4: check the existence of the generated repo // Step4: check the existence of the generated repo
req = NewRequestf(t, "GET", "/%s/%s", generateOwner.Name, generateRepoName) req = NewRequestf(t, "GET", "/%s/%s", generateOwner.Name, generateRepoName)
session.MakeRequest(t, req, http.StatusOK) session.MakeRequest(t, req, http.StatusOK)
}
// Step5: check substituted values in Readme func testRepoGenerateWithFixture(t *testing.T, session *TestSession, templateID, templateOwnerName, templateRepoName string, user, generateOwner *user_model.User, generateRepoName string) {
req = NewRequestf(t, "GET", "/%s/%s/raw/branch/master/README.md", generateOwner.Name, generateRepoName) testRepoGenerate(t, session, templateID, templateOwnerName, templateRepoName, user, generateOwner, generateRepoName)
resp = session.MakeRequest(t, req, http.StatusOK)
// check substituted values in Readme
req := NewRequestf(t, "GET", "/%s/%s/raw/branch/master/README.md", generateOwner.Name, generateRepoName)
resp := session.MakeRequest(t, req, http.StatusOK)
body := fmt.Sprintf(`# %s Readme body := fmt.Sprintf(`# %s Readme
Owner: %s Owner: %s
Link: /%s/%s Link: /%s/%s
@ -125,7 +132,7 @@ func TestRepoGenerate(t *testing.T) {
session := loginUser(t, userName) session := loginUser(t, userName)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: userName}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: userName})
testRepoGenerate(t, session, "44", "user27", "template1", user, user, "generated1") testRepoGenerateWithFixture(t, session, "44", "user27", "template1", user, user, "generated1")
} }
func TestRepoGenerateToOrg(t *testing.T) { func TestRepoGenerateToOrg(t *testing.T) {
@ -135,7 +142,7 @@ func TestRepoGenerateToOrg(t *testing.T) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: userName}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: userName})
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"}) org := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
testRepoGenerate(t, session, "44", "user27", "template1", user, org, "generated2") testRepoGenerateWithFixture(t, session, "44", "user27", "template1", user, org, "generated2")
} }
func TestRepoCreateFormTrimSpace(t *testing.T) { func TestRepoCreateFormTrimSpace(t *testing.T) {
@ -153,3 +160,66 @@ func TestRepoCreateFormTrimSpace(t *testing.T) {
assert.EqualValues(t, "/user2/spaced-name", test.RedirectURL(resp)) assert.EqualValues(t, "/user2/spaced-name", test.RedirectURL(resp))
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 2, Name: "spaced-name"}) unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 2, Name: "spaced-name"})
} }
func TestRepoGenerateTemplating(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
input := `# $REPO_NAME
This is a Repo By $REPO_OWNER
ThisIsThe${REPO_NAME}InAnInlineWay`
expected := `# %s
This is a Repo By %s
ThisIsThe%sInAnInlineWay`
templateName := "my_template"
generatedName := "my_generated"
userName := "user1"
session := loginUser(t, userName)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: userName})
template, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
Name: optional.Some(templateName),
IsTemplate: optional.Some(true),
Files: optional.Some([]*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: ".forgejo/template",
ContentReader: strings.NewReader("Readme.md"),
},
{
Operation: "create",
TreePath: "Readme.md",
ContentReader: strings.NewReader(input),
},
}),
})
defer f()
// The repo.TemplateID field is not initalized. Luckly the ID field holds the expected value
templateID := strconv.FormatInt(template.ID, 10)
testRepoGenerate(
t,
session,
templateID,
user.Name,
templateName,
user,
user,
generatedName,
)
req := NewRequestf(
t,
"GET", "/%s/%s/raw/branch/%s/Readme.md",
user.Name,
generatedName,
template.DefaultBranch,
)
resp := session.MakeRequest(t, req, http.StatusOK)
body := fmt.Sprintf(expected,
generatedName,
user.Name,
generatedName)
assert.Equal(t, body, resp.Body.String())
})
}