Skip to content

Commit bc83fb2

Browse files
GiteaBotlunnywxiaoguang
authored
Use project's redirect url instead of composing url (#33058) (#33064)
Backport #33058 by lunny Fix #32992 Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 68736ec commit bc83fb2

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

models/project/project.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ func (p *Project) LoadRepo(ctx context.Context) (err error) {
126126
return err
127127
}
128128

129+
func ProjectLinkForOrg(org *user_model.User, projectID int64) string { //nolint
130+
return fmt.Sprintf("%s/-/projects/%d", org.HomeLink(), projectID)
131+
}
132+
133+
func ProjectLinkForRepo(repo *repo_model.Repository, projectID int64) string { //nolint
134+
return fmt.Sprintf("%s/projects/%d", repo.Link(), projectID)
135+
}
136+
129137
// Link returns the project's relative URL.
130138
func (p *Project) Link(ctx context.Context) string {
131139
if p.OwnerID > 0 {
@@ -134,15 +142,15 @@ func (p *Project) Link(ctx context.Context) string {
134142
log.Error("LoadOwner: %v", err)
135143
return ""
136144
}
137-
return fmt.Sprintf("%s/-/projects/%d", p.Owner.HomeLink(), p.ID)
145+
return ProjectLinkForOrg(p.Owner, p.ID)
138146
}
139147
if p.RepoID > 0 {
140148
err := p.LoadRepo(ctx)
141149
if err != nil {
142150
log.Error("LoadRepo: %v", err)
143151
return ""
144152
}
145-
return fmt.Sprintf("%s/projects/%d", p.Repo.Link(), p.ID)
153+
return ProjectLinkForRepo(p.Repo, p.ID)
146154
}
147155
return ""
148156
}

routers/web/org/projects.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func ChangeProjectStatus(ctx *context.Context) {
212212
ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err)
213213
return
214214
}
215-
ctx.JSONRedirect(fmt.Sprintf("%s/-/projects/%d", ctx.ContextUser.HomeLink(), id))
215+
ctx.JSONRedirect(project_model.ProjectLinkForOrg(ctx.ContextUser, id))
216216
}
217217

218218
// DeleteProject delete a project
@@ -262,7 +262,7 @@ func RenderEditProject(ctx *context.Context) {
262262
ctx.Data["redirect"] = ctx.FormString("redirect")
263263
ctx.Data["HomeLink"] = ctx.ContextUser.HomeLink()
264264
ctx.Data["card_type"] = p.CardType
265-
ctx.Data["CancelLink"] = fmt.Sprintf("%s/-/projects/%d", ctx.ContextUser.HomeLink(), p.ID)
265+
ctx.Data["CancelLink"] = project_model.ProjectLinkForOrg(ctx.ContextUser, p.ID)
266266

267267
ctx.HTML(http.StatusOK, tplProjectsNew)
268268
}
@@ -276,7 +276,7 @@ func EditProjectPost(ctx *context.Context) {
276276
ctx.Data["PageIsViewProjects"] = true
277277
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)
278278
ctx.Data["CardTypes"] = project_model.GetCardConfig()
279-
ctx.Data["CancelLink"] = fmt.Sprintf("%s/-/projects/%d", ctx.ContextUser.HomeLink(), projectID)
279+
ctx.Data["CancelLink"] = project_model.ProjectLinkForOrg(ctx.ContextUser, projectID)
280280

281281
shared_user.RenderUserHeader(ctx)
282282

routers/web/repo/issue_new.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,15 @@ func NewIssuePost(ctx *context.Context) {
396396

397397
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
398398
if ctx.FormString("redirect_after_creation") == "project" && projectID > 0 {
399-
ctx.JSONRedirect(ctx.Repo.RepoLink + "/projects/" + strconv.FormatInt(projectID, 10))
400-
} else {
401-
ctx.JSONRedirect(issue.Link())
399+
project, err := project_model.GetProjectByID(ctx, projectID)
400+
if err == nil {
401+
if project.Type == project_model.TypeOrganization {
402+
ctx.JSONRedirect(project_model.ProjectLinkForOrg(ctx.Repo.Owner, project.ID))
403+
} else {
404+
ctx.JSONRedirect(project_model.ProjectLinkForRepo(repo, project.ID))
405+
}
406+
return
407+
}
402408
}
409+
ctx.JSONRedirect(issue.Link())
403410
}

routers/web/repo/projects.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func ChangeProjectStatus(ctx *context.Context) {
181181
ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err)
182182
return
183183
}
184-
ctx.JSONRedirect(fmt.Sprintf("%s/projects/%d", ctx.Repo.RepoLink, id))
184+
ctx.JSONRedirect(project_model.ProjectLinkForRepo(ctx.Repo.Repository, id))
185185
}
186186

187187
// DeleteProject delete a project
@@ -235,7 +235,7 @@ func RenderEditProject(ctx *context.Context) {
235235
ctx.Data["content"] = p.Description
236236
ctx.Data["card_type"] = p.CardType
237237
ctx.Data["redirect"] = ctx.FormString("redirect")
238-
ctx.Data["CancelLink"] = fmt.Sprintf("%s/projects/%d", ctx.Repo.Repository.Link(), p.ID)
238+
ctx.Data["CancelLink"] = project_model.ProjectLinkForRepo(ctx.Repo.Repository, p.ID)
239239

240240
ctx.HTML(http.StatusOK, tplProjectsNew)
241241
}
@@ -249,7 +249,7 @@ func EditProjectPost(ctx *context.Context) {
249249
ctx.Data["PageIsEditProjects"] = true
250250
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
251251
ctx.Data["CardTypes"] = project_model.GetCardConfig()
252-
ctx.Data["CancelLink"] = fmt.Sprintf("%s/projects/%d", ctx.Repo.Repository.Link(), projectID)
252+
ctx.Data["CancelLink"] = project_model.ProjectLinkForRepo(ctx.Repo.Repository, projectID)
253253

254254
if ctx.HasError() {
255255
ctx.HTML(http.StatusOK, tplProjectsNew)

0 commit comments

Comments
 (0)