Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Mar 25, 2022
1 parent f10724f commit d133972
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 85 deletions.
21 changes: 1 addition & 20 deletions integrations/api_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,7 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
if ctx.ExpectedCode != 0 {
expected = ctx.ExpectedCode
}
resp := ctx.Session.MakeRequest(t, req, expected)

decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
err := decoder.Decode(&pr)
return pr, err
}
}

func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
return func(t *testing.T) (api.PullRequest, error) {
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s",
owner, repo, index, ctx.Token)
req := NewRequest(t, http.MethodGet, urlStr)

expected := http.StatusOK
if ctx.ExpectedCode != 0 {
expected = ctx.ExpectedCode
}
resp := MakeRequest(t, req, expected)
resp := ctx.MakeRequest(t, req, expected)

decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
Expand Down
2 changes: 1 addition & 1 deletion integrations/api_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAPIListReleases(t *testing.T) {

// test filter
testFilterByLen := func(auth bool, query url.Values, expectedLength int, msgAndArgs ...string) {
var comment = link.String()
comment := link.String()
if len(msgAndArgs) > 0 {
comment = msgAndArgs[0]
}
Expand Down
1 change: 1 addition & 0 deletions integrations/api_ui_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/queue"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/forms"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 2 additions & 1 deletion integrations/api_ui_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func TestAPIUISearchIssues(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.EqualValues(t, "15", resp.Header().Get("X-Total-Count"))
assert.Len(t, apiIssues, 10) //there are more but 10 is page item limit
assert.Len(t, apiIssues, 10) // there are more but 10 is page item limit

query.Add("limit", "20")
link.RawQuery = query.Encode()
Expand Down
6 changes: 3 additions & 3 deletions integrations/api_ui_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNotification(t *testing.T) {

// -- GET /notifications --
// test filter
since := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801
since := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?since=%s&token=%s", since, token))
resp := MakeRequest(t, req, http.StatusOK)
var apiNL []api.NotificationThread
Expand All @@ -40,7 +40,7 @@ func TestNotification(t *testing.T) {
assert.EqualValues(t, 5, apiNL[0].ID)

// test filter
before := "2000-01-01T01%3A06%3A59%2B00%3A00" //946688819
before := "2000-01-01T01%3A06%3A59%2B00%3A00" // 946688819

req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=%s&before=%s&token=%s", "true", before, token))
resp = MakeRequest(t, req, http.StatusOK)
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestNotification(t *testing.T) {
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 2)

lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" //946687801 <- only Notification 4 is in this filter ...
lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801 <- only Notification 4 is in this filter ...
req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
MakeRequest(t, req, http.StatusResetContent)

Expand Down
106 changes: 65 additions & 41 deletions integrations/api_ui_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -56,76 +57,99 @@ func TestAPIUISearchRepo(t *testing.T) {
name, requestURL string
expectedResults
}{
{name: "RepositoriesMax50", requestURL: "/api/ui/repos/search?limit=50&private=false", expectedResults: expectedResults{
nil: {count: 30},
user: {count: 30},
user2: {count: 30}},
{
name: "RepositoriesMax50", requestURL: "/api/ui/repos/search?limit=50&private=false", expectedResults: expectedResults{
nil: {count: 30},
user: {count: 30},
user2: {count: 30},
},
},
{name: "RepositoriesMax10", requestURL: "/api/ui/repos/search?limit=10&private=false", expectedResults: expectedResults{
nil: {count: 10},
user: {count: 10},
user2: {count: 10}},
{
name: "RepositoriesMax10", requestURL: "/api/ui/repos/search?limit=10&private=false", expectedResults: expectedResults{
nil: {count: 10},
user: {count: 10},
user2: {count: 10},
},
},
{name: "RepositoriesDefault", requestURL: "/api/ui/repos/search?default&private=false", expectedResults: expectedResults{
nil: {count: 10},
user: {count: 10},
user2: {count: 10}},
{
name: "RepositoriesDefault", requestURL: "/api/ui/repos/search?default&private=false", expectedResults: expectedResults{
nil: {count: 10},
user: {count: 10},
user2: {count: 10},
},
},
{name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/ui/repos/search?q=%s&private=false", "big_test_"), expectedResults: expectedResults{
nil: {count: 7, repoName: "big_test_"},
user: {count: 7, repoName: "big_test_"},
user2: {count: 7, repoName: "big_test_"}},
{
name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/ui/repos/search?q=%s&private=false", "big_test_"), expectedResults: expectedResults{
nil: {count: 7, repoName: "big_test_"},
user: {count: 7, repoName: "big_test_"},
user2: {count: 7, repoName: "big_test_"},
},
},
{name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
nil: {count: 5},
user: {count: 9, includesPrivate: true},
user2: {count: 6, includesPrivate: true}},
{
name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
nil: {count: 5},
user: {count: 9, includesPrivate: true},
user2: {count: 6, includesPrivate: true},
},
},
{name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 2, includesPrivate: true},
user2: {count: 2, includesPrivate: true},
user4: {count: 1}},
{
name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 2, includesPrivate: true},
user2: {count: 2, includesPrivate: true},
user4: {count: 1},
},
},
{name: "RepositoriesAccessibleAndRelatedToUser3", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user3.ID), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 4, includesPrivate: true},
user2: {count: 3, includesPrivate: true},
user3: {count: 4, includesPrivate: true}},
{
name: "RepositoriesAccessibleAndRelatedToUser3", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user3.ID), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 4, includesPrivate: true},
user2: {count: 3, includesPrivate: true},
user3: {count: 4, includesPrivate: true},
},
},
{name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
nil: {count: 1, repoOwnerID: orgUser.ID},
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
user2: {count: 1, repoOwnerID: orgUser.ID}},
{
name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
nil: {count: 1, repoOwnerID: orgUser.ID},
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
user2: {count: 1, repoOwnerID: orgUser.ID},
},
},
{name: "RepositoriesAccessibleAndRelatedToUser4", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user4.ID), expectedResults: expectedResults{
nil: {count: 3},
user: {count: 4, includesPrivate: true},
user4: {count: 7, includesPrivate: true}}},
user4: {count: 7, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeSource", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "source"), expectedResults: expectedResults{
nil: {count: 0},
user: {count: 1, includesPrivate: true},
user4: {count: 1, includesPrivate: true}}},
user4: {count: 1, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "fork"), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 1},
user4: {count: 2, includesPrivate: true}}},
user4: {count: 2, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork/Exclusive", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "fork"), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 1},
user4: {count: 2, includesPrivate: true}}},
user4: {count: 2, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "mirror"), expectedResults: expectedResults{
nil: {count: 2},
user: {count: 2},
user4: {count: 4, includesPrivate: true}}},
user4: {count: 4, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror/Exclusive", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "mirror"), expectedResults: expectedResults{
nil: {count: 1},
user: {count: 1},
user4: {count: 2, includesPrivate: true}}},
user4: {count: 2, includesPrivate: true},
}},
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeCollaborative", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "collaborative"), expectedResults: expectedResults{
nil: {count: 0},
user: {count: 1, includesPrivate: true},
user4: {count: 1, includesPrivate: true}}},
user4: {count: 1, includesPrivate: true},
}},
}

for _, testCase := range testCases {
Expand Down
1 change: 1 addition & 0 deletions integrations/api_ui_repo_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion integrations/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func doBranchProtectPRMerge(baseCtx *TestContext, dstPath string) func(t *testin
}
}

func doProtectBranch(ctx TestContext, branch string, userToWhitelist string, unprotectedFilePatterns string) func(t *testing.T) {
func doProtectBranch(ctx TestContext, branch, userToWhitelist, unprotectedFilePatterns string) func(t *testing.T) {
// We are going to just use the owner to set the protection.
return func(t *testing.T) {
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings/branches", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
Expand Down
2 changes: 1 addition & 1 deletion integrations/mirror_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func doCreatePushMirror(ctx TestContext, address, username, password string) fun
}
}

func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) {
func doRemovePushMirror(ctx TestContext, address, username, password string, pushMirrorID int) func(t *testing.T) {
return func(t *testing.T) {
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))

Expand Down
2 changes: 0 additions & 2 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"
auth_service "code.gitea.io/gitea/services/auth"

"gitea.com/go-chi/session"
)

// APIContext is a specific context for API service
Expand Down
6 changes: 3 additions & 3 deletions modules/context/api_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

// APIUIContexter returns apicontext as middleware
func APIUIContexter() func(http.Handler) http.Handler {
var csrfOpts = getCsrfOpts()
csrfOpts := getCsrfOpts()

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var locale = middleware.Locale(w, req)
var ctx = APIContext{
locale := middleware.Locale(w, req)
ctx := APIContext{
Context: &Context{
Resp: NewResponse(w),
Data: map[string]interface{}{},
Expand Down
6 changes: 6 additions & 0 deletions modules/session/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ type Store interface {
Set(interface{}, interface{}) error
Delete(interface{}) error
}

// RegenerateSession regenerates the underlying session and returns the new store
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
s, err := session.RegenerateSession(resp, req)
return s, err
}
22 changes: 11 additions & 11 deletions routers/api/ui/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func repoAssignment() func(ctx *context.APIContext) {
)

// Check if the user is the same as the repository owner.
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
owner = ctx.User
if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) {
owner = ctx.Doer
} else {
owner, err = user_model.GetUserByName(userName)
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func repoAssignment() func(ctx *context.APIContext) {
repo.Owner = owner
ctx.Repo.Repository = repo

ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.User)
ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.Doer)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
return
Expand Down Expand Up @@ -136,7 +136,7 @@ func reqOrgOwnership() func(ctx *context.APIContext) {
return
}

isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
isOwner, err := models.IsOrganizationOwner(orgID, ctx.Doer.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err)
return
Expand Down Expand Up @@ -168,7 +168,7 @@ func reqOrgMembership() func(ctx *context.APIContext) {
return
}

if isMember, err := models.IsOrganizationMember(orgID, ctx.User.ID); err != nil {
if isMember, err := models.IsOrganizationMember(orgID, ctx.Doer.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err)
return
} else if !isMember {
Expand Down Expand Up @@ -237,7 +237,7 @@ func mustEnableIssuesOrPulls(ctx *context.APIContext) {
if ctx.IsSigned {
log.Trace("Permission Denied: User %-v cannot read %-v and %-v in Repo %-v\n"+
"User in Repo has Permissions: %-+v",
ctx.User,
ctx.Doer,
unit.TypeIssues,
unit.TypePullRequests,
ctx.Repo.Repository,
Expand Down Expand Up @@ -265,12 +265,12 @@ func mustNotBeArchived(ctx *context.APIContext) {

// bind binding an obj to a func(ctx *context.APIContext)
func bind(obj interface{}) http.HandlerFunc {
var tp = reflect.TypeOf(obj)
tp := reflect.TypeOf(obj)
for tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
return web.Wrap(func(ctx *context.APIContext) {
var theObj = reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
errs := binding.Bind(ctx.Req, theObj)
if len(errs) > 0 {
ctx.Error(http.StatusUnprocessableEntity, "validationError", errs[0].Error())
Expand All @@ -282,15 +282,15 @@ func bind(obj interface{}) http.HandlerFunc {

// Routes registers all v1 APIs routes to web application.
func Routes(sess func(next http.Handler) http.Handler) *web.Route {
var m = web.NewRoute()
m := web.NewRoute()

m.Use(sess)
m.Use(common.SecurityHeaders())
if setting.CORSConfig.Enabled {
m.Use(cors.Handler(cors.Options{
//Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
// Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
AllowedOrigins: setting.CORSConfig.AllowDomain,
//setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
// setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
AllowedMethods: setting.CORSConfig.Methods,
AllowCredentials: setting.CORSConfig.AllowCredentials,
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),
Expand Down
1 change: 0 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/routing"
"code.gitea.io/gitea/routers/admin"
"code.gitea.io/gitea/routers/api/v1/misc"
"code.gitea.io/gitea/routers/web/admin"
"code.gitea.io/gitea/routers/web/auth"
Expand Down

0 comments on commit d133972

Please sign in to comment.