@@ -87,7 +87,7 @@ func LoadIssuesFromColumnList(ctx context.Context, bs project_model.ColumnList)
8787// If newProjectID is 0, the issue is removed from the project
8888func IssueAssignOrRemoveProject (ctx context.Context , issue * Issue , doer * user_model.User , newProjectID , newColumnID int64 , action string ) error {
8989 return db .WithTx (ctx , func (ctx context.Context ) error {
90- oldProjectIDs := issue . projectIDs ( ctx )
90+ var oldProjectIDs [] int64
9191 var err error
9292 if err := issue .LoadRepo (ctx ); err != nil {
9393 return err
@@ -111,65 +111,53 @@ func IssueAssignOrRemoveProject(ctx context.Context, issue *Issue, doer *user_mo
111111 }
112112 }
113113
114- if action == "null" {
115- if newProjectID == 0 {
116- action = "clear"
117- } else {
118- action = "attach"
119- count , err := db .GetEngine (ctx ).Table ("project_issue" ).Where ("issue_id=? AND project_id=?" , issue .ID , newProjectID ).Count ()
120- if err != nil {
121- return err
122- }
123- if count > 0 {
124- action = "detach"
125- }
126- }
127- }
128-
129- if action == "attach" {
114+ if action == "attach" || (action == "null" && newProjectID > 0 ) {
130115 if newProjectID == 0 {
131116 return nil
132117 }
133118 if newColumnID == 0 {
134119 panic ("newColumnID must not be zero" ) // shouldn't happen
135120 }
136121 res := struct {
137- MaxSorting int64
138122 IssueCount int64
139123 }{}
140- if _ , err := db .GetEngine (ctx ).Select ("max(sorting) as max_sorting, count(*) as issue_count" ).Table ("project_issue" ).
124+ if _ , err := db .GetEngine (ctx ).Select ("count(*) as issue_count" ).Table ("project_issue" ).
141125 Where ("project_id=?" , newProjectID ).
142126 And ("project_board_id=?" , newColumnID ).
143127 Get (& res ); err != nil {
144128 return err
145129 }
146- newSorting := util .Iif (res .IssueCount > 0 , res .MaxSorting + 1 , 0 )
147- err = db .Insert (ctx , & project_model.ProjectIssue {
148- IssueID : issue .ID ,
149- ProjectID : newProjectID ,
150- ProjectColumnID : newColumnID ,
151- Sorting : newSorting ,
152- })
153- oldProjectIDs = append (oldProjectIDs , 0 )
130+ if res .IssueCount == 0 {
131+ err = db .Insert (ctx , & project_model.ProjectIssue {
132+ IssueID : issue .ID ,
133+ ProjectID : newProjectID ,
134+ ProjectColumnID : newColumnID ,
135+ })
136+ oldProjectIDs = []int64 {0 }
137+ } else {
138+ _ , err = db .GetEngine (ctx ).Where ("issue_id=? AND project_id=?" , issue .ID , newProjectID ).Delete (& project_model.ProjectIssue {})
139+ oldProjectIDs = []int64 {newProjectID }
140+ newProjectID = 0
141+ }
154142 } else if action == "detach" {
155143 _ , err = db .GetEngine (ctx ).Where ("issue_id=? AND project_id=?" , issue .ID , newProjectID ).Delete (& project_model.ProjectIssue {})
156144 oldProjectIDs = append (oldProjectIDs , newProjectID )
157145 newProjectID = 0
158- } else if action == "clear" {
146+ } else if action == "clear" || ( action == "null" && newProjectID == 0 ) {
159147 if err = db .GetEngine (ctx ).Table ("project_issue" ).Select ("project_id" ).Where ("issue_id=?" , issue .ID ).Find (& oldProjectIDs ); err != nil {
160148 return err
161149 }
162150 _ , err = db .GetEngine (ctx ).Where ("issue_id=?" , issue .ID ).Delete (& project_model.ProjectIssue {})
163151 newProjectID = 0
164152 }
165153
166- for i := range oldProjectIDs {
154+ for _ , oldProjectID := range oldProjectIDs {
167155 if _ , err := CreateComment (ctx , & CreateCommentOptions {
168156 Type : CommentTypeProject ,
169157 Doer : doer ,
170158 Repo : issue .Repo ,
171159 Issue : issue ,
172- OldProjectID : oldProjectIDs [ i ] ,
160+ OldProjectID : oldProjectID ,
173161 ProjectID : newProjectID ,
174162 }); err != nil {
175163 return err
0 commit comments