Skip to content

Commit 8e45fcb

Browse files
yp05327delvhlunny6543
authored
Do not store user projects as organization projects (#23353)
A part of #22865 At first, I think we do not need 3 ProjectTypes, as we can check user type, but it seems that it is not database friendly. --------- Co-authored-by: delvh <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 8120c0c commit 8e45fcb

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ var migrations = []Migration{
471471
NewMigration("Rename Webhook org_id to owner_id", v1_20.RenameWebhookOrgToOwner),
472472
// v246 -> v247
473473
NewMigration("Add missed column owner_id for project table", v1_20.AddNewColumnForProject),
474+
// v247 -> v248
475+
NewMigration("Fix incorrect project type", v1_20.FixIncorrectProjectType),
474476
}
475477

476478
// GetCurrentDBVersion returns the current db version

models/migrations/v1_20/v247.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_20 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/log"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
// FixIncorrectProjectType: set individual project's type from 3(TypeOrganization) to 1(TypeIndividual)
13+
func FixIncorrectProjectType(x *xorm.Engine) error {
14+
type User struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
Type int
17+
}
18+
19+
const (
20+
UserTypeIndividual int = 0
21+
22+
TypeIndividual uint8 = 1
23+
TypeOrganization uint8 = 3
24+
)
25+
26+
type Project struct {
27+
OwnerID int64 `xorm:"INDEX"`
28+
Type uint8
29+
Owner *User `xorm:"extends"`
30+
}
31+
32+
sess := x.NewSession()
33+
defer sess.Close()
34+
35+
if err := sess.Begin(); err != nil {
36+
return err
37+
}
38+
39+
count, err := sess.Table("project").
40+
Where("type = ? AND owner_id IN (SELECT id FROM `user` WHERE type = ?)", TypeOrganization, UserTypeIndividual).
41+
Update(&Project{
42+
Type: TypeIndividual,
43+
})
44+
if err != nil {
45+
return err
46+
}
47+
log.Debug("Updated %d projects to belong to a user instead of an organization", count)
48+
49+
return sess.Commit()
50+
}

models/project/project.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func GetCardConfig() []CardConfig {
172172
// IsTypeValid checks if a project type is valid
173173
func IsTypeValid(p Type) bool {
174174
switch p {
175-
case TypeRepository, TypeOrganization:
175+
case TypeIndividual, TypeRepository, TypeOrganization:
176176
return true
177177
default:
178178
return false

models/project/project_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestIsProjectTypeValid(t *testing.T) {
2020
typ Type
2121
valid bool
2222
}{
23-
{TypeIndividual, false},
23+
{TypeIndividual, true},
2424
{TypeRepository, true},
2525
{TypeOrganization, true},
2626
{UnknownType, false},

routers/web/org/projects.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ func Projects(ctx *context.Context) {
5151
page = 1
5252
}
5353

54+
var projectType project_model.Type
55+
if ctx.ContextUser.IsOrganization() {
56+
projectType = project_model.TypeOrganization
57+
} else {
58+
projectType = project_model.TypeIndividual
59+
}
5460
projects, total, err := project_model.FindProjects(ctx, project_model.SearchOptions{
5561
OwnerID: ctx.ContextUser.ID,
5662
Page: page,
5763
IsClosed: util.OptionalBoolOf(isShowClosed),
5864
SortType: sortType,
59-
Type: project_model.TypeOrganization,
65+
Type: projectType,
6066
})
6167
if err != nil {
6268
ctx.ServerError("FindProjects", err)
@@ -66,7 +72,7 @@ func Projects(ctx *context.Context) {
6672
opTotal, err := project_model.CountProjects(ctx, project_model.SearchOptions{
6773
OwnerID: ctx.ContextUser.ID,
6874
IsClosed: util.OptionalBoolOf(!isShowClosed),
69-
Type: project_model.TypeOrganization,
75+
Type: projectType,
7076
})
7177
if err != nil {
7278
ctx.ServerError("CountProjects", err)
@@ -143,14 +149,21 @@ func NewProjectPost(ctx *context.Context) {
143149
return
144150
}
145151

146-
if err := project_model.NewProject(&project_model.Project{
152+
newProject := project_model.Project{
147153
OwnerID: ctx.ContextUser.ID,
148154
Title: form.Title,
149155
Description: form.Content,
150156
CreatorID: ctx.Doer.ID,
151157
BoardType: form.BoardType,
152-
Type: project_model.TypeOrganization,
153-
}); err != nil {
158+
}
159+
160+
if ctx.ContextUser.IsOrganization() {
161+
newProject.Type = project_model.TypeOrganization
162+
} else {
163+
newProject.Type = project_model.TypeIndividual
164+
}
165+
166+
if err := project_model.NewProject(&newProject); err != nil {
154167
ctx.ServerError("NewProject", err)
155168
return
156169
}

0 commit comments

Comments
 (0)