Skip to content

Commit d133972

Browse files
committed
Rebase
1 parent f10724f commit d133972

14 files changed

+96
-85
lines changed

integrations/api_helper_for_declarative_test.go

+1-20
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,7 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
231231
if ctx.ExpectedCode != 0 {
232232
expected = ctx.ExpectedCode
233233
}
234-
resp := ctx.Session.MakeRequest(t, req, expected)
235-
236-
decoder := json.NewDecoder(resp.Body)
237-
pr := api.PullRequest{}
238-
err := decoder.Decode(&pr)
239-
return pr, err
240-
}
241-
}
242-
243-
func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
244-
return func(t *testing.T) (api.PullRequest, error) {
245-
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s",
246-
owner, repo, index, ctx.Token)
247-
req := NewRequest(t, http.MethodGet, urlStr)
248-
249-
expected := http.StatusOK
250-
if ctx.ExpectedCode != 0 {
251-
expected = ctx.ExpectedCode
252-
}
253-
resp := MakeRequest(t, req, expected)
234+
resp := ctx.MakeRequest(t, req, expected)
254235

255236
decoder := json.NewDecoder(resp.Body)
256237
pr := api.PullRequest{}

integrations/api_releases_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestAPIListReleases(t *testing.T) {
5252

5353
// test filter
5454
testFilterByLen := func(auth bool, query url.Values, expectedLength int, msgAndArgs ...string) {
55-
var comment = link.String()
55+
comment := link.String()
5656
if len(msgAndArgs) > 0 {
5757
comment = msgAndArgs[0]
5858
}

integrations/api_ui_helper_for_declarative_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/queue"
1616
api "code.gitea.io/gitea/modules/structs"
1717
"code.gitea.io/gitea/services/forms"
18+
1819
"github.com/stretchr/testify/assert"
1920
)
2021

integrations/api_ui_issue_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
api "code.gitea.io/gitea/modules/structs"
14+
1415
"github.com/stretchr/testify/assert"
1516
)
1617

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

6364
query.Add("limit", "20")
6465
link.RawQuery = query.Encode()

integrations/api_ui_notification_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestNotification(t *testing.T) {
3030

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

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

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

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

integrations/api_ui_repo_test.go

+65-41
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/gitea/modules/structs"
17+
1718
"github.com/stretchr/testify/assert"
1819
)
1920

@@ -56,76 +57,99 @@ func TestAPIUISearchRepo(t *testing.T) {
5657
name, requestURL string
5758
expectedResults
5859
}{
59-
{name: "RepositoriesMax50", requestURL: "/api/ui/repos/search?limit=50&private=false", expectedResults: expectedResults{
60-
nil: {count: 30},
61-
user: {count: 30},
62-
user2: {count: 30}},
60+
{
61+
name: "RepositoriesMax50", requestURL: "/api/ui/repos/search?limit=50&private=false", expectedResults: expectedResults{
62+
nil: {count: 30},
63+
user: {count: 30},
64+
user2: {count: 30},
65+
},
6366
},
64-
{name: "RepositoriesMax10", requestURL: "/api/ui/repos/search?limit=10&private=false", expectedResults: expectedResults{
65-
nil: {count: 10},
66-
user: {count: 10},
67-
user2: {count: 10}},
67+
{
68+
name: "RepositoriesMax10", requestURL: "/api/ui/repos/search?limit=10&private=false", expectedResults: expectedResults{
69+
nil: {count: 10},
70+
user: {count: 10},
71+
user2: {count: 10},
72+
},
6873
},
69-
{name: "RepositoriesDefault", requestURL: "/api/ui/repos/search?default&private=false", expectedResults: expectedResults{
70-
nil: {count: 10},
71-
user: {count: 10},
72-
user2: {count: 10}},
74+
{
75+
name: "RepositoriesDefault", requestURL: "/api/ui/repos/search?default&private=false", expectedResults: expectedResults{
76+
nil: {count: 10},
77+
user: {count: 10},
78+
user2: {count: 10},
79+
},
7380
},
74-
{name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/ui/repos/search?q=%s&private=false", "big_test_"), expectedResults: expectedResults{
75-
nil: {count: 7, repoName: "big_test_"},
76-
user: {count: 7, repoName: "big_test_"},
77-
user2: {count: 7, repoName: "big_test_"}},
81+
{
82+
name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/ui/repos/search?q=%s&private=false", "big_test_"), expectedResults: expectedResults{
83+
nil: {count: 7, repoName: "big_test_"},
84+
user: {count: 7, repoName: "big_test_"},
85+
user2: {count: 7, repoName: "big_test_"},
86+
},
7887
},
79-
{name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
80-
nil: {count: 5},
81-
user: {count: 9, includesPrivate: true},
82-
user2: {count: 6, includesPrivate: true}},
88+
{
89+
name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
90+
nil: {count: 5},
91+
user: {count: 9, includesPrivate: true},
92+
user2: {count: 6, includesPrivate: true},
93+
},
8394
},
84-
{name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
85-
nil: {count: 1},
86-
user: {count: 2, includesPrivate: true},
87-
user2: {count: 2, includesPrivate: true},
88-
user4: {count: 1}},
95+
{
96+
name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
97+
nil: {count: 1},
98+
user: {count: 2, includesPrivate: true},
99+
user2: {count: 2, includesPrivate: true},
100+
user4: {count: 1},
101+
},
89102
},
90-
{name: "RepositoriesAccessibleAndRelatedToUser3", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user3.ID), expectedResults: expectedResults{
91-
nil: {count: 1},
92-
user: {count: 4, includesPrivate: true},
93-
user2: {count: 3, includesPrivate: true},
94-
user3: {count: 4, includesPrivate: true}},
103+
{
104+
name: "RepositoriesAccessibleAndRelatedToUser3", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user3.ID), expectedResults: expectedResults{
105+
nil: {count: 1},
106+
user: {count: 4, includesPrivate: true},
107+
user2: {count: 3, includesPrivate: true},
108+
user3: {count: 4, includesPrivate: true},
109+
},
95110
},
96-
{name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
97-
nil: {count: 1, repoOwnerID: orgUser.ID},
98-
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
99-
user2: {count: 1, repoOwnerID: orgUser.ID}},
111+
{
112+
name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
113+
nil: {count: 1, repoOwnerID: orgUser.ID},
114+
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
115+
user2: {count: 1, repoOwnerID: orgUser.ID},
116+
},
100117
},
101118
{name: "RepositoriesAccessibleAndRelatedToUser4", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d", user4.ID), expectedResults: expectedResults{
102119
nil: {count: 3},
103120
user: {count: 4, includesPrivate: true},
104-
user4: {count: 7, includesPrivate: true}}},
121+
user4: {count: 7, includesPrivate: true},
122+
}},
105123
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeSource", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "source"), expectedResults: expectedResults{
106124
nil: {count: 0},
107125
user: {count: 1, includesPrivate: true},
108-
user4: {count: 1, includesPrivate: true}}},
126+
user4: {count: 1, includesPrivate: true},
127+
}},
109128
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "fork"), expectedResults: expectedResults{
110129
nil: {count: 1},
111130
user: {count: 1},
112-
user4: {count: 2, includesPrivate: true}}},
131+
user4: {count: 2, includesPrivate: true},
132+
}},
113133
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork/Exclusive", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "fork"), expectedResults: expectedResults{
114134
nil: {count: 1},
115135
user: {count: 1},
116-
user4: {count: 2, includesPrivate: true}}},
136+
user4: {count: 2, includesPrivate: true},
137+
}},
117138
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "mirror"), expectedResults: expectedResults{
118139
nil: {count: 2},
119140
user: {count: 2},
120-
user4: {count: 4, includesPrivate: true}}},
141+
user4: {count: 4, includesPrivate: true},
142+
}},
121143
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror/Exclusive", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "mirror"), expectedResults: expectedResults{
122144
nil: {count: 1},
123145
user: {count: 1},
124-
user4: {count: 2, includesPrivate: true}}},
146+
user4: {count: 2, includesPrivate: true},
147+
}},
125148
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeCollaborative", requestURL: fmt.Sprintf("/api/ui/repos/search?uid=%d&mode=%s", user4.ID, "collaborative"), expectedResults: expectedResults{
126149
nil: {count: 0},
127150
user: {count: 1, includesPrivate: true},
128-
user4: {count: 1, includesPrivate: true}}},
151+
user4: {count: 1, includesPrivate: true},
152+
}},
129153
}
130154

131155
for _, testCase := range testCases {

integrations/api_ui_repo_topic_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
api "code.gitea.io/gitea/modules/structs"
13+
1314
"github.com/stretchr/testify/assert"
1415
)
1516

integrations/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func doBranchProtectPRMerge(baseCtx *TestContext, dstPath string) func(t *testin
428428
}
429429
}
430430

431-
func doProtectBranch(ctx TestContext, branch string, userToWhitelist string, unprotectedFilePatterns string) func(t *testing.T) {
431+
func doProtectBranch(ctx TestContext, branch, userToWhitelist, unprotectedFilePatterns string) func(t *testing.T) {
432432
// We are going to just use the owner to set the protection.
433433
return func(t *testing.T) {
434434
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings/branches", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))

integrations/mirror_push_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func doCreatePushMirror(ctx TestContext, address, username, password string) fun
9797
}
9898
}
9999

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

modules/context/api.go

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"code.gitea.io/gitea/modules/setting"
2020
"code.gitea.io/gitea/modules/web/middleware"
2121
auth_service "code.gitea.io/gitea/services/auth"
22-
23-
"gitea.com/go-chi/session"
2422
)
2523

2624
// APIContext is a specific context for API service

modules/context/api_ui.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
// APIUIContexter returns apicontext as middleware
1919
func APIUIContexter() func(http.Handler) http.Handler {
20-
var csrfOpts = getCsrfOpts()
20+
csrfOpts := getCsrfOpts()
2121

2222
return func(next http.Handler) http.Handler {
2323
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
24-
var locale = middleware.Locale(w, req)
25-
var ctx = APIContext{
24+
locale := middleware.Locale(w, req)
25+
ctx := APIContext{
2626
Context: &Context{
2727
Resp: NewResponse(w),
2828
Data: map[string]interface{}{},

modules/session/store.go

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ type Store interface {
1616
Set(interface{}, interface{}) error
1717
Delete(interface{}) error
1818
}
19+
20+
// RegenerateSession regenerates the underlying session and returns the new store
21+
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
22+
s, err := session.RegenerateSession(resp, req)
23+
return s, err
24+
}

routers/api/ui/api.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func repoAssignment() func(ctx *context.APIContext) {
4141
)
4242

4343
// Check if the user is the same as the repository owner.
44-
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
45-
owner = ctx.User
44+
if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) {
45+
owner = ctx.Doer
4646
} else {
4747
owner, err = user_model.GetUserByName(userName)
4848
if err != nil {
@@ -83,7 +83,7 @@ func repoAssignment() func(ctx *context.APIContext) {
8383
repo.Owner = owner
8484
ctx.Repo.Repository = repo
8585

86-
ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.User)
86+
ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.Doer)
8787
if err != nil {
8888
ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
8989
return
@@ -136,7 +136,7 @@ func reqOrgOwnership() func(ctx *context.APIContext) {
136136
return
137137
}
138138

139-
isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
139+
isOwner, err := models.IsOrganizationOwner(orgID, ctx.Doer.ID)
140140
if err != nil {
141141
ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err)
142142
return
@@ -168,7 +168,7 @@ func reqOrgMembership() func(ctx *context.APIContext) {
168168
return
169169
}
170170

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

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

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

287287
m.Use(sess)
288288
m.Use(common.SecurityHeaders())
289289
if setting.CORSConfig.Enabled {
290290
m.Use(cors.Handler(cors.Options{
291-
//Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
291+
// Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
292292
AllowedOrigins: setting.CORSConfig.AllowDomain,
293-
//setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
293+
// setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
294294
AllowedMethods: setting.CORSConfig.Methods,
295295
AllowCredentials: setting.CORSConfig.AllowCredentials,
296296
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),

routers/web/web.go

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"code.gitea.io/gitea/modules/validation"
2424
"code.gitea.io/gitea/modules/web"
2525
"code.gitea.io/gitea/modules/web/routing"
26-
"code.gitea.io/gitea/routers/admin"
2726
"code.gitea.io/gitea/routers/api/v1/misc"
2827
"code.gitea.io/gitea/routers/web/admin"
2928
"code.gitea.io/gitea/routers/web/auth"

0 commit comments

Comments
 (0)