99
1010 issues_model "code.gitea.io/gitea/models/issues"
1111 project_model "code.gitea.io/gitea/models/project"
12+ repo_model "code.gitea.io/gitea/models/repo"
1213 user_model "code.gitea.io/gitea/models/user"
1314 "code.gitea.io/gitea/modules/git"
1415 "code.gitea.io/gitea/modules/gitrepo"
@@ -32,71 +33,114 @@ func NewNotifier() notify_service.Notifier {
3233 return & workflowNotifier {}
3334}
3435
35- func (m * workflowNotifier ) IssueChangeStatus (ctx context.Context , doer * user_model.User , commitID string , issue * issues_model.Issue , actionComment * issues_model.Comment , isClosed bool ) {
36- if isClosed {
37- if err := issue .LoadRepo (ctx ); err != nil {
38- log .Error ("IssueChangeStatus: LoadRepo: %v" , err )
39- return
40- }
41- gitRepo , err := gitrepo .OpenRepository (ctx , issue .Repo )
42- if err != nil {
43- log .Error ("IssueChangeStatus: OpenRepository: %v" , err )
44- return
36+ func findRepoProjectsWorkflows (ctx context.Context , repo * repo_model.Repository ) ([]* project_module.Workflow , error ) {
37+ gitRepo , err := gitrepo .OpenRepository (ctx , repo )
38+ if err != nil {
39+ log .Error ("IssueChangeStatus: OpenRepository: %v" , err )
40+ return nil , err
41+ }
42+ defer gitRepo .Close ()
43+
44+ // Get the commit object for the ref
45+ commit , err := gitRepo .GetCommit (repo .DefaultBranch )
46+ if err != nil {
47+ log .Error ("gitRepo.GetCommit: %w" , err )
48+ return nil , err
49+ }
50+
51+ tree , err := commit .SubTree (".gitea/projects" )
52+ if _ , ok := err .(git.ErrNotExist ); ok {
53+ return nil , nil
54+ }
55+ if err != nil {
56+ log .Error ("commit.SubTree: %w" , err )
57+ return nil , err
58+ }
59+
60+ entries , err := tree .ListEntriesRecursiveFast ()
61+ if err != nil {
62+ log .Error ("tree.ListEntriesRecursiveFast: %w" , err )
63+ return nil , err
64+ }
65+
66+ ret := make (git.Entries , 0 , len (entries ))
67+ for _ , entry := range entries {
68+ if strings .HasSuffix (entry .Name (), ".yml" ) || strings .HasSuffix (entry .Name (), ".yaml" ) {
69+ ret = append (ret , entry )
4570 }
46- defer gitRepo .Close ()
71+ }
72+ if len (ret ) == 0 {
73+ return nil , nil
74+ }
4775
48- // Get the commit object for the ref
49- commit , err := gitRepo .GetCommit (issue .Repo .DefaultBranch )
76+ wfs := make ([]* project_module.Workflow , 0 , len (ret ))
77+ for _ , entry := range ret {
78+ workflowContent , err := commit .GetFileContent (".gitea/projects/" + entry .Name (), 1024 * 1024 )
5079 if err != nil {
5180 log .Error ("gitRepo.GetCommit: %w" , err )
52- return
81+ return nil , err
5382 }
5483
55- tree , err := commit .SubTree (".gitea/projects" )
56- if _ , ok := err .(git.ErrNotExist ); ok {
57- return
84+ wf , err := project_module .ParseWorkflow (workflowContent )
85+ if err != nil {
86+ log .Error ("IssueChangeStatus: OpenRepository: %v" , err )
87+ return nil , err
5888 }
89+ projectName := strings .TrimSuffix (strings .TrimSuffix (entry .Name (), ".yml" ), ".yaml" )
90+ project , err := project_model .GetProjectByName (ctx , repo .ID , projectName )
5991 if err != nil {
60- log .Error ("commit.SubTree: %w " , err )
61- return
92+ log .Error ("IssueChangeStatus: GetProjectByName: %v " , err )
93+ return nil , err
6294 }
95+ wf .ProjectID = project .ID
6396
64- entries , err := tree .ListEntriesRecursiveFast ()
65- if err != nil {
66- log .Error ("tree.ListEntriesRecursiveFast: %w" , err )
97+ wfs = append (wfs , wf )
98+ }
99+ return wfs , nil
100+ }
101+
102+ func (m * workflowNotifier ) NewIssue (ctx context.Context , issue * issues_model.Issue , mentions []* user_model.User ) {
103+ if err := issue .LoadRepo (ctx ); err != nil {
104+ log .Error ("NewIssue: LoadRepo: %v" , err )
105+ return
106+ }
107+ wfs , err := findRepoProjectsWorkflows (ctx , issue .Repo )
108+ if err != nil {
109+ log .Error ("NewIssue: findRepoProjectsWorkflows: %v" , err )
110+ return
111+ }
112+
113+ for _ , wf := range wfs {
114+ if err := wf .FireAction (project_module .EventItemClosed , func (action project_module.Action ) error {
115+ board , err := project_model .GetBoardByProjectIDAndBoardName (ctx , wf .ProjectID , action .SetValue )
116+ if err != nil {
117+ log .Error ("NewIssue: GetBoardByProjectIDAndBoardName: %v" , err )
118+ return err
119+ }
120+ return project_model .AddIssueToBoard (ctx , issue .ID , board )
121+ }); err != nil {
122+ log .Error ("NewIssue: FireAction: %v" , err )
67123 return
68124 }
125+ }
126+ }
127+ }
69128
70- ret := make (git. Entries , 0 , len ( entries ))
71- for _ , entry := range entries {
72- if strings . HasSuffix ( entry . Name (), ".yml" ) || strings . HasSuffix ( entry . Name (), ".yaml" ) {
73- ret = append ( ret , entry )
74- }
129+ func ( m * workflowNotifier ) IssueChangeStatus ( ctx context. Context , doer * user_model. User , commitID string , issue * issues_model. Issue , actionComment * issues_model. Comment , isClosed bool ) {
130+ if isClosed {
131+ if err := issue . LoadRepo ( ctx ); err != nil {
132+ log . Error ( "IssueChangeStatus: LoadRepo: %v" , err )
133+ return
75134 }
76- if len (ret ) == 0 {
135+ wfs , err := findRepoProjectsWorkflows (ctx , issue .Repo )
136+ if err != nil {
137+ log .Error ("IssueChangeStatus: findRepoProjectsWorkflows: %v" , err )
77138 return
78139 }
79140
80- for _ , entry := range ret {
81- workflowContent , err := commit .GetFileContent (".gitea/projects/" + entry .Name (), 1024 * 1024 )
82- if err != nil {
83- log .Error ("gitRepo.GetCommit: %w" , err )
84- return
85- }
86-
87- wf , err := project_module .ParseWorkflow (workflowContent )
88- if err != nil {
89- log .Error ("IssueChangeStatus: OpenRepository: %v" , err )
90- return
91- }
92- projectName := strings .TrimSuffix (strings .TrimSuffix (entry .Name (), ".yml" ), ".yaml" )
93- project , err := project_model .GetProjectByName (ctx , issue .RepoID , projectName )
94- if err != nil {
95- log .Error ("IssueChangeStatus: GetProjectByName: %v" , err )
96- return
97- }
141+ for _ , wf := range wfs {
98142 if err := wf .FireAction (project_module .EventItemClosed , func (action project_module.Action ) error {
99- board , err := project_model .GetBoardByProjectIDAndBoardName (ctx , project . ID , action .SetValue )
143+ board , err := project_model .GetBoardByProjectIDAndBoardName (ctx , wf . ProjectID , action .SetValue )
100144 if err != nil {
101145 log .Error ("IssueChangeStatus: GetBoardByProjectIDAndBoardName: %v" , err )
102146 return err
0 commit comments