Skip to content

Commit e7d65cb

Browse files
authored
Add email notify for new release (#12463)
* Add email notify for new release Signed-off-by: a1012112796 <[email protected]>
1 parent e429c11 commit e7d65cb

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

models/release.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Release struct {
3535
NumCommits int64
3636
NumCommitsBehind int64 `xorm:"-"`
3737
Note string `xorm:"TEXT"`
38+
RenderedNote string `xorm:"-"`
3839
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
3940
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
4041
IsTag bool `xorm:"NOT NULL DEFAULT false"`

modules/notification/mail/mail.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,16 @@ func (m *mailNotifier) NotifyPullRequestPushCommits(doer *models.User, pr *model
145145

146146
m.NotifyCreateIssueComment(doer, comment.Issue.Repo, comment.Issue, comment)
147147
}
148+
149+
func (m *mailNotifier) NotifyNewRelease(rel *models.Release) {
150+
if err := rel.LoadAttributes(); err != nil {
151+
log.Error("NotifyNewRelease: %v", err)
152+
return
153+
}
154+
155+
if rel.IsDraft || rel.IsPrerelease {
156+
return
157+
}
158+
159+
mailer.MailNewRelease(rel)
160+
}

services/mailer/mail_release.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mailer
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/base"
13+
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/markup/markdown"
15+
"code.gitea.io/gitea/modules/setting"
16+
)
17+
18+
const (
19+
tplNewReleaseMail base.TplName = "release"
20+
)
21+
22+
// MailNewRelease send new release notify to all all repo watchers.
23+
func MailNewRelease(rel *models.Release) {
24+
watcherIDList, err := models.GetRepoWatchersIDs(rel.RepoID)
25+
if err != nil {
26+
log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err)
27+
return
28+
}
29+
30+
recipients, err := models.GetMaileableUsersByIDs(watcherIDList)
31+
if err != nil {
32+
log.Error("models.GetMaileableUsersByIDs: %v", err)
33+
return
34+
}
35+
36+
tos := make([]string, 0, len(recipients))
37+
for _, to := range recipients {
38+
if to.ID != rel.PublisherID {
39+
tos = append(tos, to.Email)
40+
}
41+
}
42+
43+
rel.RenderedNote = markdown.RenderString(rel.Note, rel.Repo.Link(), rel.Repo.ComposeMetas())
44+
subject := fmt.Sprintf("%s in %s released", rel.TagName, rel.Repo.FullName())
45+
46+
mailMeta := map[string]interface{}{
47+
"Release": rel,
48+
"Subject": subject,
49+
}
50+
51+
var mailBody bytes.Buffer
52+
53+
if err = bodyTemplates.ExecuteTemplate(&mailBody, string(tplNewReleaseMail), mailMeta); err != nil {
54+
log.Error("ExecuteTemplate [%s]: %v", string(tplNewReleaseMail)+"/body", err)
55+
return
56+
}
57+
58+
msgs := make([]*Message, 0, len(recipients))
59+
publisherName := rel.Publisher.DisplayName()
60+
relURL := "<" + rel.HTMLURL() + ">"
61+
for _, to := range tos {
62+
msg := NewMessageFrom([]string{to}, publisherName, setting.MailService.FromEmail, subject, mailBody.String())
63+
msg.Info = subject
64+
msg.SetHeader("Message-ID", relURL)
65+
msgs = append(msgs, msg)
66+
}
67+
68+
SendAsyncs(msgs)
69+
}

templates/mail/release.tmpl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>{{.Subject}}</title>
6+
7+
<style>
8+
blockquote { padding-left: 1em; margin: 1em 0; border-left: 1px solid grey; color: #777}
9+
.footer { font-size:small; color:#666;}
10+
</style>
11+
12+
</head>
13+
14+
<body>
15+
<p><b>@{{.Release.Publisher.Name}}</b> released <a href="{{.Release.HTMLURL}}">{{.Release.TagName}}</a>
16+
in <a href="{{AppUrl}}{{.Release.Repo.OwnerName}}/{{.Release.Repo.Name}}">{{.Release.Repo.FullName}} </p>
17+
<h4>Title: {{.Release.Title}}</h4>
18+
<p>
19+
Note: <br>
20+
{{- if eq .Release.RenderedNote ""}}
21+
{{else}}
22+
{{.Release.RenderedNote | Str2html}}
23+
{{end -}}
24+
</p>
25+
<br><br>
26+
<p>
27+
---
28+
<br>
29+
Downloads:
30+
<ul>
31+
<li>
32+
<a href="{{AppUrl}}{{.Release.Repo.OwnerName}}/{{.Release.Repo.Name}}/archive/{{.Release.TagName | EscapePound}}.zip" rel="nofollow"><strong> Source Code (ZIP)</strong></a>
33+
</li>
34+
<li>
35+
<a href="{{AppUrl}}{{.Release.Repo.OwnerName}}/{{.Release.Repo.Name}}/archive/{{.Release.TagName | EscapePound}}.tar.gz"><strong> Source Code (TAR.GZ)</strong></a>
36+
</li>
37+
{{if .Release.Attachments}}
38+
{{range .Release.Attachments}}
39+
<li>
40+
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
41+
<strong>{{.Name}} ({{.Size | FileSize}})</strong>
42+
</a>
43+
</li>
44+
{{end}}
45+
{{end}}
46+
</ul>
47+
</p>
48+
<div class="footer">
49+
<p>
50+
---
51+
<br>
52+
<a href="{{.Release.HTMLURL}}">View it on {{AppName}}</a>.
53+
</p>
54+
</div>
55+
</body>
56+
</html>

0 commit comments

Comments
 (0)