@@ -14,27 +14,20 @@ import (
1414
1515// LoadProject load the project the issue was assigned to
1616func (issue * Issue ) LoadProject (ctx context.Context ) (err error ) {
17- if issue .Project == nil {
18- var p project_model.Project
19- has , err := db .GetEngine (ctx ).Table ("project" ).
17+ if issue .Projects == nil {
18+ err = db .GetEngine (ctx ).Table ("project" ).
2019 Join ("INNER" , "project_issue" , "project.id=project_issue.project_id" ).
21- Where ("project_issue.issue_id = ?" , issue .ID ).Get (& p )
22- if err != nil {
23- return err
24- } else if has {
25- issue .Project = & p
26- }
20+ Where ("project_issue.issue_id = ?" , issue .ID ).Find (& issue .Projects )
2721 }
2822 return err
2923}
3024
31- func (issue * Issue ) projectID (ctx context.Context ) int64 {
32- var ip project_model.ProjectIssue
33- has , err := db .GetEngine (ctx ).Where ("issue_id=?" , issue .ID ).Get (& ip )
34- if err != nil || ! has {
35- return 0
25+ func (issue * Issue ) projectIDs (ctx context.Context ) []int64 {
26+ var ips []int64
27+ if err := db .GetEngine (ctx ).Table ("project_issue" ).Select ("project_id" ).Where ("issue_id=?" , issue .ID ).Find (& ips ); err != nil {
28+ return nil
3629 }
37- return ip . ProjectID
30+ return ips
3831}
3932
4033// ProjectBoardID return project board id if issue was assigned to one
@@ -91,24 +84,25 @@ func LoadIssuesFromBoardList(ctx context.Context, bs project_model.BoardList) (m
9184}
9285
9386// ChangeProjectAssign changes the project associated with an issue
94- func ChangeProjectAssign (ctx context.Context , issue * Issue , doer * user_model.User , newProjectID int64 ) error {
87+ func ChangeProjectAssign (ctx context.Context , issue * Issue , doer * user_model.User , newProjectID int64 , action string ) error {
9588 ctx , committer , err := db .TxContext (ctx )
9689 if err != nil {
9790 return err
9891 }
9992 defer committer .Close ()
10093
101- if err := addUpdateIssueProject (ctx , issue , doer , newProjectID ); err != nil {
94+ if err := addUpdateIssueProject (ctx , issue , doer , newProjectID , action ); err != nil {
10295 return err
10396 }
10497
10598 return committer .Commit ()
10699}
107100
108- func addUpdateIssueProject (ctx context.Context , issue * Issue , doer * user_model.User , newProjectID int64 ) error {
109- oldProjectID := issue .projectID (ctx )
101+ func addUpdateIssueProject (ctx context.Context , issue * Issue , doer * user_model.User , newProjectID int64 , action string ) error {
102+ var oldProjectIDs []int64
103+ var err error
110104
111- if err : = issue .LoadRepo (ctx ); err != nil {
105+ if err = issue .LoadRepo (ctx ); err != nil {
112106 return err
113107 }
114108
@@ -123,25 +117,51 @@ func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.U
123117 }
124118 }
125119
126- if _ , err := db .GetEngine (ctx ).Where ("project_issue.issue_id=?" , issue .ID ).Delete (& project_model.ProjectIssue {}); err != nil {
127- return err
120+ if action == "null" {
121+ if newProjectID == 0 {
122+ action = "clear"
123+ } else {
124+ action = "attach"
125+ count , err := db .GetEngine (ctx ).Table ("project_issue" ).Where ("issue_id=? AND project_id=?" , issue .ID , newProjectID ).Count ()
126+ if err != nil {
127+ return err
128+ }
129+ if count > 0 {
130+ action = "detach"
131+ }
132+ }
133+ }
134+
135+ if action == "attach" {
136+ err = db .Insert (ctx , & project_model.ProjectIssue {
137+ IssueID : issue .ID ,
138+ ProjectID : newProjectID ,
139+ })
140+ oldProjectIDs = append (oldProjectIDs , 0 )
141+ } else if action == "detach" {
142+ _ , err = db .GetEngine (ctx ).Where ("issue_id=? AND project_id=?" , issue .ID , newProjectID ).Delete (& project_model.ProjectIssue {})
143+ oldProjectIDs = append (oldProjectIDs , newProjectID )
144+ newProjectID = 0
145+ } else if action == "clear" {
146+ if err = db .GetEngine (ctx ).Table ("project_issue" ).Select ("project_id" ).Where ("issue_id=?" , issue .ID ).Find (& oldProjectIDs ); err != nil {
147+ return err
148+ }
149+ _ , err = db .GetEngine (ctx ).Where ("issue_id=?" , issue .ID ).Delete (& project_model.ProjectIssue {})
150+ newProjectID = 0
128151 }
129152
130- if oldProjectID > 0 || newProjectID > 0 {
153+ for i := range oldProjectIDs {
131154 if _ , err := CreateComment (ctx , & CreateCommentOptions {
132155 Type : CommentTypeProject ,
133156 Doer : doer ,
134157 Repo : issue .Repo ,
135158 Issue : issue ,
136- OldProjectID : oldProjectID ,
159+ OldProjectID : oldProjectIDs [ i ] ,
137160 ProjectID : newProjectID ,
138161 }); err != nil {
139162 return err
140163 }
141164 }
142165
143- return db .Insert (ctx , & project_model.ProjectIssue {
144- IssueID : issue .ID ,
145- ProjectID : newProjectID ,
146- })
166+ return err
147167}
0 commit comments