Skip to content

Commit ba08055

Browse files
Notify action added
Signed-off-by: Mayank Mohapatra <[email protected]>
1 parent 1ba7cbb commit ba08055

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

services/actions/job_emitter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
actions_model "code.gitea.io/gitea/models/actions"
1212
"code.gitea.io/gitea/models/db"
1313
"code.gitea.io/gitea/modules/graceful"
14+
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/queue"
16+
notify_service "code.gitea.io/gitea/services/notify"
1517

1618
"github.com/nektos/act/pkg/jobparser"
1719
"xorm.io/builder"
@@ -70,6 +72,12 @@ func checkJobsOfRun(ctx context.Context, runID int64) error {
7072
}); err != nil {
7173
return err
7274
}
75+
run, _, err := db.GetByID[actions_model.ActionRun](ctx, runID)
76+
if err != nil {
77+
log.Error("GetByID failed: %v", err)
78+
} else if run.Status == actions_model.StatusSuccess || run.Status == actions_model.StatusFailure {
79+
notify_service.ActionRunFinished(ctx, run)
80+
}
7381
CreateCommitStatus(ctx, jobs...)
7482
return nil
7583
}

services/mailer/notify.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
actions_model "code.gitea.io/gitea/models/actions"
1011
activities_model "code.gitea.io/gitea/models/activities"
1112
issues_model "code.gitea.io/gitea/models/issues"
1213
repo_model "code.gitea.io/gitea/models/repo"
@@ -202,3 +203,63 @@ func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *
202203
log.Error("SendRepoTransferNotifyMail: %v", err)
203204
}
204205
}
206+
207+
func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {
208+
// Check status first to avoid unnecessary processing
209+
if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure {
210+
return
211+
}
212+
213+
// Load required attributes after status check
214+
if err := run.LoadAttributes(ctx); err != nil {
215+
log.Error("LoadAttributes: %v", err)
216+
return
217+
}
218+
219+
subject := fmt.Sprintf("[%s] Workflow run %s: %s",
220+
run.Repo.FullName(),
221+
run.WorkflowName,
222+
run.Status,
223+
)
224+
225+
// Safely handle short commit SHA
226+
commitSHA := run.CommitSHA
227+
if len(commitSHA) > 7 {
228+
commitSHA = commitSHA[:7]
229+
}
230+
231+
body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s
232+
233+
Repository: %s
234+
Branch: %s
235+
Commit: %s
236+
Triggered by: %s
237+
238+
View the run details here: %s`,
239+
run.WorkflowName,
240+
run.Index,
241+
run.Status,
242+
run.Repo.FullName(),
243+
run.RefName,
244+
commitSHA,
245+
run.TriggerUser.Name,
246+
run.HTMLURL(),
247+
)
248+
249+
// Send to repo owner if notifications enabled and email present
250+
if run.Repo.Owner.Email != "" &&
251+
run.Repo.Owner.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
252+
if err := SendMail(ctx, []string{run.Repo.Owner.Email}, subject, body); err != nil {
253+
log.Error("Failed to send email to repo owner %s: %v", run.Repo.Owner.Email, err)
254+
}
255+
}
256+
257+
// Send to commit author if different from trigger user and notifications enabled
258+
if run.TriggerUser.ID != run.CommitAuthor.ID &&
259+
run.CommitAuthor.Email != "" &&
260+
run.CommitAuthor.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
261+
if err := SendMail(ctx, []string{run.CommitAuthor.Email}, subject, body); err != nil {
262+
log.Error("Failed to send email to commit author %s: %v", run.CommitAuthor.Email, err)
263+
}
264+
}
265+
}

services/notify/notifier.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package notify
55

66
import (
77
"context"
8-
8+
9+
actions_model "code.gitea.io/gitea/models/actions"
910
git_model "code.gitea.io/gitea/models/git"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
packages_model "code.gitea.io/gitea/models/packages"
@@ -76,5 +77,7 @@ type Notifier interface {
7677

7778
ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository)
7879

80+
ActionRunFinished(ctx context.Context, run *actions_model.ActionRun)
81+
7982
CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus)
8083
}

services/notify/notify.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package notify
66
import (
77
"context"
88

9+
actions_model "code.gitea.io/gitea/models/actions"
910
git_model "code.gitea.io/gitea/models/git"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
packages_model "code.gitea.io/gitea/models/packages"
@@ -374,3 +375,10 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit
374375
notifier.CreateCommitStatus(ctx, repo, commit, sender, status)
375376
}
376377
}
378+
379+
// ActionRunFinished represents action run finished
380+
func ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {
381+
for _, notifier := range notifiers {
382+
notifier.ActionRunFinished(ctx, run)
383+
}
384+
}

services/notify/null.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package notify
66
import (
77
"context"
88

9+
actions_model "code.gitea.io/gitea/models/actions"
910
git_model "code.gitea.io/gitea/models/git"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
packages_model "code.gitea.io/gitea/models/packages"
@@ -212,3 +213,7 @@ func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.R
212213

213214
func (*NullNotifier) CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
214215
}
216+
217+
218+
// ActionRunFinished represents action run finished
219+
func (*NullNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {}

0 commit comments

Comments
 (0)