Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"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:42:06 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIIssueTemplateList(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
|
|
|
|
t.Run("no templates", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var issueTemplates []*api.IssueTemplate
|
|
|
|
DecodeJSON(t, resp, &issueTemplates)
|
|
|
|
assert.Empty(t, issueTemplates)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("existing template", func(t *testing.T) {
|
2024-02-05 10:33:40 -05:00
|
|
|
templateCandidates := []string{
|
|
|
|
".forgejo/ISSUE_TEMPLATE/test.md",
|
|
|
|
".forgejo/issue_template/test.md",
|
|
|
|
".gitea/ISSUE_TEMPLATE/test.md",
|
|
|
|
".gitea/issue_template/test.md",
|
|
|
|
".github/ISSUE_TEMPLATE/test.md",
|
|
|
|
".github/issue_template/test.md",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, template := range templateCandidates {
|
|
|
|
t.Run(template, func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
defer func() {
|
|
|
|
deleteFileInBranch(user, repo, template, repo.DefaultBranch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := createOrReplaceFileInBranch(user, repo, template, repo.DefaultBranch,
|
|
|
|
`---
|
|
|
|
name: 'Template Name'
|
|
|
|
about: 'This template is for testing!'
|
|
|
|
title: '[TEST] '
|
|
|
|
ref: 'main'
|
|
|
|
---
|
|
|
|
|
|
|
|
This is the template!`)
|
2024-07-30 15:42:06 -04:00
|
|
|
require.NoError(t, err)
|
2024-02-05 10:33:40 -05:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var issueTemplates []*api.IssueTemplate
|
|
|
|
DecodeJSON(t, resp, &issueTemplates)
|
|
|
|
assert.Len(t, issueTemplates, 1)
|
|
|
|
assert.Equal(t, "Template Name", issueTemplates[0].Name)
|
|
|
|
assert.Equal(t, "This template is for testing!", issueTemplates[0].About)
|
|
|
|
assert.Equal(t, "refs/heads/main", issueTemplates[0].Ref)
|
|
|
|
assert.Equal(t, template, issueTemplates[0].FileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("multiple templates", func(t *testing.T) {
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-02-05 10:33:40 -05:00
|
|
|
templatePriority := []string{
|
|
|
|
".forgejo/issue_template/test.md",
|
|
|
|
".gitea/issue_template/test.md",
|
|
|
|
".github/issue_template/test.md",
|
|
|
|
}
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
defer func() {
|
2024-02-05 10:33:40 -05:00
|
|
|
for _, template := range templatePriority {
|
|
|
|
deleteFileInBranch(user, repo, template, repo.DefaultBranch)
|
|
|
|
}
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
}()
|
|
|
|
|
2024-02-05 10:33:40 -05:00
|
|
|
for _, template := range templatePriority {
|
|
|
|
err := createOrReplaceFileInBranch(user, repo, template, repo.DefaultBranch,
|
|
|
|
`---
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
name: 'Template Name'
|
|
|
|
about: 'This template is for testing!'
|
|
|
|
title: '[TEST] '
|
|
|
|
ref: 'main'
|
|
|
|
---
|
|
|
|
|
|
|
|
This is the template!`)
|
2024-07-30 15:42:06 -04:00
|
|
|
require.NoError(t, err)
|
2024-02-05 10:33:40 -05:00
|
|
|
}
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var issueTemplates []*api.IssueTemplate
|
|
|
|
DecodeJSON(t, resp, &issueTemplates)
|
2024-02-05 10:33:40 -05:00
|
|
|
|
|
|
|
// If templates have the same filename and content, but in different
|
|
|
|
// directories, they count as different templates, and all are
|
|
|
|
// considered.
|
|
|
|
assert.Len(t, issueTemplates, 3)
|
Fix `/api/v1/{owner}/{repo}/issue_templates`
When issue templates were moved into services in
def4956122ea2364f247712b13856383ee496add, the code was also refactored
and simplified. Unfortunately, that simplification broke the
`/api/v1/{owner}/{repo}/issue_templates` route, because it was
previously using a helper function that ignored invalid templates, and
after the refactor, the function it called *always* returned non-nil as
the second return value. This, in turn, results in the aforementioned
end point always returning an internal server error.
This change restores the previous behaviour of ignoring invalid files
returned by `issue.GetTemplatesFromDefaultBranch`, and adds a few test
cases to exercise the endpoint.
Other users of `GetTemplatesFromDefaultBranch` already ignore the second
return value, or handle it correctly, so no changes are necessary there.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-02-05 06:42:52 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|