Skip to content

Commit 40f0781

Browse files
authored
Merge branch 'main' into fix-redirect-link
2 parents eda6aa4 + f88dbf8 commit 40f0781

33 files changed

+929
-306
lines changed

models/repo/transfer.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type RepoTransfer struct { //nolint
6868
RecipientID int64
6969
Recipient *user_model.User `xorm:"-"`
7070
RepoID int64
71+
Repo *Repository `xorm:"-"`
7172
TeamIDs []int64
7273
Teams []*organization.Team `xorm:"-"`
7374

@@ -79,48 +80,65 @@ func init() {
7980
db.RegisterModel(new(RepoTransfer))
8081
}
8182

82-
// LoadAttributes fetches the transfer recipient from the database
83-
func (r *RepoTransfer) LoadAttributes(ctx context.Context) error {
83+
func (r *RepoTransfer) LoadRecipient(ctx context.Context) error {
8484
if r.Recipient == nil {
8585
u, err := user_model.GetUserByID(ctx, r.RecipientID)
8686
if err != nil {
8787
return err
8888
}
89-
9089
r.Recipient = u
9190
}
9291

93-
if r.Recipient.IsOrganization() && len(r.TeamIDs) != len(r.Teams) {
94-
for _, v := range r.TeamIDs {
95-
team, err := organization.GetTeamByID(ctx, v)
96-
if err != nil {
97-
return err
98-
}
92+
return nil
93+
}
9994

100-
if team.OrgID != r.Recipient.ID {
101-
return fmt.Errorf("team %d belongs not to org %d", v, r.Recipient.ID)
102-
}
95+
func (r *RepoTransfer) LoadRepo(ctx context.Context) error {
96+
if r.Repo == nil {
97+
repo, err := GetRepositoryByID(ctx, r.RepoID)
98+
if err != nil {
99+
return err
100+
}
101+
r.Repo = repo
102+
}
103+
104+
return nil
105+
}
106+
107+
// LoadAttributes fetches the transfer recipient from the database
108+
func (r *RepoTransfer) LoadAttributes(ctx context.Context) error {
109+
if err := r.LoadRecipient(ctx); err != nil {
110+
return err
111+
}
103112

113+
if r.Recipient.IsOrganization() && r.Teams == nil {
114+
teamsMap, err := organization.GetTeamsByIDs(ctx, r.TeamIDs)
115+
if err != nil {
116+
return err
117+
}
118+
for _, team := range teamsMap {
104119
r.Teams = append(r.Teams, team)
105120
}
106121
}
107122

123+
if err := r.LoadRepo(ctx); err != nil {
124+
return err
125+
}
126+
108127
if r.Doer == nil {
109128
u, err := user_model.GetUserByID(ctx, r.DoerID)
110129
if err != nil {
111130
return err
112131
}
113-
114132
r.Doer = u
115133
}
116134

117135
return nil
118136
}
119137

120-
// CanUserAcceptTransfer checks if the user has the rights to accept/decline a repo transfer.
138+
// CanUserAcceptOrRejectTransfer checks if the user has the rights to accept/decline a repo transfer.
121139
// For user, it checks if it's himself
122140
// For organizations, it checks if the user is able to create repos
123-
func (r *RepoTransfer) CanUserAcceptTransfer(ctx context.Context, u *user_model.User) bool {
141+
func (r *RepoTransfer) CanUserAcceptOrRejectTransfer(ctx context.Context, u *user_model.User) bool {
124142
if err := r.LoadAttributes(ctx); err != nil {
125143
log.Error("LoadAttributes: %v", err)
126144
return false
@@ -166,6 +184,10 @@ func GetPendingRepositoryTransfers(ctx context.Context, opts *PendingRepositoryT
166184
Find(&transfers)
167185
}
168186

187+
func IsRepositoryTransferExist(ctx context.Context, repoID int64) (bool, error) {
188+
return db.GetEngine(ctx).Where("repo_id = ?", repoID).Exist(new(RepoTransfer))
189+
}
190+
169191
// GetPendingRepositoryTransfer fetches the most recent and ongoing transfer
170192
// process for the repository
171193
func GetPendingRepositoryTransfer(ctx context.Context, repo *Repository) (*RepoTransfer, error) {
@@ -206,11 +228,26 @@ func CreatePendingRepositoryTransfer(ctx context.Context, doer, newOwner *user_m
206228
return err
207229
}
208230

231+
if _, err := user_model.GetUserByID(ctx, newOwner.ID); err != nil {
232+
return err
233+
}
234+
209235
// Make sure repo is ready to transfer
210236
if err := TestRepositoryReadyForTransfer(repo.Status); err != nil {
211237
return err
212238
}
213239

240+
exist, err := IsRepositoryTransferExist(ctx, repo.ID)
241+
if err != nil {
242+
return err
243+
}
244+
if exist {
245+
return ErrRepoTransferInProgress{
246+
Uname: repo.Owner.LowerName,
247+
Name: repo.Name,
248+
}
249+
}
250+
214251
repo.Status = RepositoryPendingTransfer
215252
if err := UpdateRepositoryCols(ctx, repo, "status"); err != nil {
216253
return err

modules/structs/hook.go

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,7 @@ var (
116116
_ Payloader = &PackagePayload{}
117117
)
118118

119-
// _________ __
120-
// \_ ___ \_______ ____ _____ _/ |_ ____
121-
// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
122-
// \ \____| | \/\ ___/ / __ \| | \ ___/
123-
// \______ /|__| \___ >____ /__| \___ >
124-
// \/ \/ \/ \/
125-
126-
// CreatePayload FIXME
119+
// CreatePayload represents a payload information of create event.
127120
type CreatePayload struct {
128121
Sha string `json:"sha"`
129122
Ref string `json:"ref"`
@@ -157,13 +150,6 @@ func ParseCreateHook(raw []byte) (*CreatePayload, error) {
157150
return hook, nil
158151
}
159152

160-
// ________ .__ __
161-
// \______ \ ____ | | _____/ |_ ____
162-
// | | \_/ __ \| | _/ __ \ __\/ __ \
163-
// | ` \ ___/| |_\ ___/| | \ ___/
164-
// /_______ /\___ >____/\___ >__| \___ >
165-
// \/ \/ \/ \/
166-
167153
// PusherType define the type to push
168154
type PusherType string
169155

@@ -186,13 +172,6 @@ func (p *DeletePayload) JSONPayload() ([]byte, error) {
186172
return json.MarshalIndent(p, "", " ")
187173
}
188174

189-
// ___________ __
190-
// \_ _____/__________| | __
191-
// | __)/ _ \_ __ \ |/ /
192-
// | \( <_> ) | \/ <
193-
// \___ / \____/|__| |__|_ \
194-
// \/ \/
195-
196175
// ForkPayload represents fork payload
197176
type ForkPayload struct {
198177
Forkee *Repository `json:"forkee"`
@@ -232,13 +211,6 @@ func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
232211
return json.MarshalIndent(p, "", " ")
233212
}
234213

235-
// __________ .__
236-
// \______ \ ____ | | ____ _____ ______ ____
237-
// | _// __ \| | _/ __ \\__ \ / ___// __ \
238-
// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
239-
// |____|_ /\___ >____/\___ >____ /____ >\___ >
240-
// \/ \/ \/ \/ \/ \/
241-
242214
// HookReleaseAction defines hook release action type
243215
type HookReleaseAction string
244216

@@ -302,13 +274,6 @@ func (p *PushPayload) Branch() string {
302274
return strings.ReplaceAll(p.Ref, "refs/heads/", "")
303275
}
304276

305-
// .___
306-
// | | ______ ________ __ ____
307-
// | |/ ___// ___/ | \_/ __ \
308-
// | |\___ \ \___ \| | /\ ___/
309-
// |___/____ >____ >____/ \___ >
310-
// \/ \/ \/
311-
312277
// HookIssueAction FIXME
313278
type HookIssueAction string
314279

@@ -371,13 +336,6 @@ type ChangesPayload struct {
371336
Ref *ChangesFromPayload `json:"ref,omitempty"`
372337
}
373338

374-
// __________ .__ .__ __________ __
375-
// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
376-
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
377-
// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
378-
// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
379-
// \/ \/ |__| \/ \/
380-
381339
// PullRequestPayload represents a payload information of pull request event.
382340
type PullRequestPayload struct {
383341
Action HookIssueAction `json:"action"`
@@ -402,13 +360,6 @@ type ReviewPayload struct {
402360
Content string `json:"content"`
403361
}
404362

405-
// __ __.__ __ .__
406-
// / \ / \__| | _|__|
407-
// \ \/\/ / | |/ / |
408-
// \ /| | <| |
409-
// \__/\ / |__|__|_ \__|
410-
// \/ \/
411-
412363
// HookWikiAction an action that happens to a wiki page
413364
type HookWikiAction string
414365

@@ -435,13 +386,6 @@ func (p *WikiPayload) JSONPayload() ([]byte, error) {
435386
return json.MarshalIndent(p, "", " ")
436387
}
437388

438-
//__________ .__ __
439-
//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
440-
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
441-
// | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
442-
// |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
443-
// \/ \/|__| \/ \/
444-
445389
// HookRepoAction an action that happens to a repo
446390
type HookRepoAction string
447391

@@ -480,7 +424,7 @@ type PackagePayload struct {
480424
Action HookPackageAction `json:"action"`
481425
Repository *Repository `json:"repository"`
482426
Package *Package `json:"package"`
483-
Organization *User `json:"organization"`
427+
Organization *Organization `json:"organization"`
484428
Sender *User `json:"sender"`
485429
}
486430

routers/api/v1/repo/transfer.go

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/log"
1717
api "code.gitea.io/gitea/modules/structs"
18+
"code.gitea.io/gitea/modules/util"
1819
"code.gitea.io/gitea/modules/web"
1920
"code.gitea.io/gitea/services/context"
2021
"code.gitea.io/gitea/services/convert"
@@ -161,12 +162,16 @@ func AcceptTransfer(ctx *context.APIContext) {
161162
// "404":
162163
// "$ref": "#/responses/notFound"
163164

164-
err := acceptOrRejectRepoTransfer(ctx, true)
165-
if ctx.Written() {
166-
return
167-
}
165+
err := repo_service.AcceptTransferOwnership(ctx, ctx.Repo.Repository, ctx.Doer)
168166
if err != nil {
169-
ctx.Error(http.StatusInternalServerError, "acceptOrRejectRepoTransfer", err)
167+
switch {
168+
case repo_model.IsErrNoPendingTransfer(err):
169+
ctx.Error(http.StatusNotFound, "AcceptTransferOwnership", err)
170+
case errors.Is(err, util.ErrPermissionDenied):
171+
ctx.Error(http.StatusForbidden, "AcceptTransferOwnership", err)
172+
default:
173+
ctx.Error(http.StatusInternalServerError, "AcceptTransferOwnership", err)
174+
}
170175
return
171176
}
172177

@@ -199,40 +204,18 @@ func RejectTransfer(ctx *context.APIContext) {
199204
// "404":
200205
// "$ref": "#/responses/notFound"
201206

202-
err := acceptOrRejectRepoTransfer(ctx, false)
203-
if ctx.Written() {
204-
return
205-
}
207+
err := repo_service.RejectRepositoryTransfer(ctx, ctx.Repo.Repository, ctx.Doer)
206208
if err != nil {
207-
ctx.Error(http.StatusInternalServerError, "acceptOrRejectRepoTransfer", err)
209+
switch {
210+
case repo_model.IsErrNoPendingTransfer(err):
211+
ctx.Error(http.StatusNotFound, "RejectRepositoryTransfer", err)
212+
case errors.Is(err, util.ErrPermissionDenied):
213+
ctx.Error(http.StatusForbidden, "RejectRepositoryTransfer", err)
214+
default:
215+
ctx.Error(http.StatusInternalServerError, "RejectRepositoryTransfer", err)
216+
}
208217
return
209218
}
210219

211220
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.Permission))
212221
}
213-
214-
func acceptOrRejectRepoTransfer(ctx *context.APIContext, accept bool) error {
215-
repoTransfer, err := repo_model.GetPendingRepositoryTransfer(ctx, ctx.Repo.Repository)
216-
if err != nil {
217-
if repo_model.IsErrNoPendingTransfer(err) {
218-
ctx.NotFound()
219-
return nil
220-
}
221-
return err
222-
}
223-
224-
if err := repoTransfer.LoadAttributes(ctx); err != nil {
225-
return err
226-
}
227-
228-
if !repoTransfer.CanUserAcceptTransfer(ctx, ctx.Doer) {
229-
ctx.Error(http.StatusForbidden, "CanUserAcceptTransfer", nil)
230-
return fmt.Errorf("user does not have permissions to do this")
231-
}
232-
233-
if accept {
234-
return repo_service.TransferOwnership(ctx, repoTransfer.Doer, repoTransfer.Recipient, ctx.Repo.Repository, repoTransfer.Teams)
235-
}
236-
237-
return repo_service.CancelRepositoryTransfer(ctx, ctx.Repo.Repository)
238-
}

routers/api/v1/utils/hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
205205
webhook_module.HookEventWiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
206206
webhook_module.HookEventRepository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
207207
webhook_module.HookEventRelease: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
208+
webhook_module.HookEventPackage: util.SliceContainsString(form.Events, string(webhook_module.HookEventPackage), true),
208209
webhook_module.HookEventStatus: util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true),
209210
},
210211
BranchFilter: form.BranchFilter,
@@ -384,6 +385,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
384385
w.HookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
385386
w.HookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
386387
w.HookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
388+
w.HookEvents[webhook_module.HookEventPullRequestComment] = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment))
387389
w.HookEvents[webhook_module.HookEventPullRequestReview] = pullHook(form.Events, "pull_request_review")
388390
w.HookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest))
389391
w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))

0 commit comments

Comments
 (0)