|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package projects |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strings" |
| 9 | + |
| 10 | + issues_model "code.gitea.io/gitea/models/issues" |
| 11 | + project_model "code.gitea.io/gitea/models/project" |
| 12 | + user_model "code.gitea.io/gitea/models/user" |
| 13 | + "code.gitea.io/gitea/modules/git" |
| 14 | + "code.gitea.io/gitea/modules/gitrepo" |
| 15 | + "code.gitea.io/gitea/modules/log" |
| 16 | + project_module "code.gitea.io/gitea/modules/projects" |
| 17 | + notify_service "code.gitea.io/gitea/services/notify" |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + notify_service.RegisterNotifier(&workflowNotifier{}) |
| 22 | +} |
| 23 | + |
| 24 | +type workflowNotifier struct { |
| 25 | + notify_service.NullNotifier |
| 26 | +} |
| 27 | + |
| 28 | +var _ notify_service.Notifier = &workflowNotifier{} |
| 29 | + |
| 30 | +// NewNotifier create a new workflowNotifier notifier |
| 31 | +func NewNotifier() notify_service.Notifier { |
| 32 | + return &workflowNotifier{} |
| 33 | +} |
| 34 | + |
| 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 |
| 45 | + } |
| 46 | + defer gitRepo.Close() |
| 47 | + |
| 48 | + // Get the commit object for the ref |
| 49 | + commit, err := gitRepo.GetCommit(issue.Repo.DefaultBranch) |
| 50 | + if err != nil { |
| 51 | + log.Error("gitRepo.GetCommit: %w", err) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + tree, err := commit.SubTree(".gitea/projects") |
| 56 | + if _, ok := err.(git.ErrNotExist); ok { |
| 57 | + return |
| 58 | + } |
| 59 | + if err != nil { |
| 60 | + log.Error("commit.SubTree: %w", err) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + entries, err := tree.ListEntriesRecursiveFast() |
| 65 | + if err != nil { |
| 66 | + log.Error("tree.ListEntriesRecursiveFast: %w", err) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 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 | + } |
| 75 | + } |
| 76 | + if len(ret) == 0 { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 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 | + } |
| 98 | + if err := wf.FireAction(project_module.EventItemClosed, func(action project_module.Action) error { |
| 99 | + board, err := project_model.GetBoardByProjectIDAndBoardName(ctx, project.ID, action.SetValue) |
| 100 | + if err != nil { |
| 101 | + log.Error("IssueChangeStatus: GetBoardByProjectIDAndBoardName: %v", err) |
| 102 | + return err |
| 103 | + } |
| 104 | + return project_model.MoveIssueToAnotherBoard(ctx, issue.ID, board) |
| 105 | + }); err != nil { |
| 106 | + log.Error("IssueChangeStatus: FireAction: %v", err) |
| 107 | + return |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments