mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-07-22 17:18:33 +00:00
Reduce links in chat notifications to avoid multiple previews (#6908)
Some messages had multiple links resulting in multiple previews per message. The superfluous links have been removed leaving only the most important link. All info the other links provided can be immediately accessed through the main link that is left over. The fork and push messages still have multiple links because from the former you want to see where the fork originates from, the latter has a link per commit. Resolves #162 ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6908 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: patka <patka@patka.dev> Co-committed-by: patka <patka@patka.dev>
This commit is contained in:
parent
06556abb6d
commit
31e7aa61b1
8 changed files with 120 additions and 147 deletions
|
@ -92,7 +92,6 @@ func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator
|
|||
}
|
||||
|
||||
func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
|
||||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
|
||||
titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
|
||||
var text string
|
||||
|
@ -100,39 +99,37 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, with
|
|||
|
||||
switch p.Action {
|
||||
case api.HookIssueOpened:
|
||||
text = fmt.Sprintf("[%s] Issue opened: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue opened: %s", p.Repository.FullName, titleLink)
|
||||
color = orangeColor
|
||||
case api.HookIssueClosed:
|
||||
text = fmt.Sprintf("[%s] Issue closed: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue closed: %s", p.Repository.FullName, titleLink)
|
||||
color = redColor
|
||||
case api.HookIssueReOpened:
|
||||
text = fmt.Sprintf("[%s] Issue re-opened: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue re-opened: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueEdited:
|
||||
text = fmt.Sprintf("[%s] Issue edited: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue edited: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueAssigned:
|
||||
list := make([]string, len(p.Issue.Assignees))
|
||||
for i, user := range p.Issue.Assignees {
|
||||
list[i] = linkFormatter(setting.AppURL+url.PathEscape(user.UserName), user.UserName)
|
||||
}
|
||||
text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink, strings.Join(list, ", "), titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue assigned to %s: %s", p.Repository.FullName, strings.Join(list, ", "), titleLink)
|
||||
color = greenColor
|
||||
case api.HookIssueUnassigned:
|
||||
text = fmt.Sprintf("[%s] Issue unassigned: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue unassigned: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueLabelUpdated:
|
||||
text = fmt.Sprintf("[%s] Issue labels updated: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue labels updated: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueLabelCleared:
|
||||
text = fmt.Sprintf("[%s] Issue labels cleared: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue labels cleared: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueSynchronized:
|
||||
text = fmt.Sprintf("[%s] Issue synchronized: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue synchronized: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueMilestoned:
|
||||
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
|
||||
text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", repoLink,
|
||||
linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", p.Repository.FullName, p.Issue.Milestone.Title, titleLink)
|
||||
case api.HookIssueDemilestoned:
|
||||
text = fmt.Sprintf("[%s] Issue milestone cleared: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Issue milestone cleared: %s", p.Repository.FullName, titleLink)
|
||||
}
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
var attachmentText string
|
||||
|
@ -144,7 +141,6 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, with
|
|||
}
|
||||
|
||||
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
|
||||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
||||
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
|
||||
var text string
|
||||
|
@ -153,83 +149,79 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
|
|||
|
||||
switch p.Action {
|
||||
case api.HookIssueOpened:
|
||||
text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request opened: %s", p.Repository.FullName, titleLink)
|
||||
attachmentText = p.PullRequest.Body
|
||||
color = greenColor
|
||||
case api.HookIssueClosed:
|
||||
if p.PullRequest.HasMerged {
|
||||
text = fmt.Sprintf("[%s] Pull request merged: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request merged: %s", p.Repository.FullName, titleLink)
|
||||
color = purpleColor
|
||||
} else {
|
||||
text = fmt.Sprintf("[%s] Pull request closed: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request closed: %s", p.Repository.FullName, titleLink)
|
||||
color = redColor
|
||||
}
|
||||
case api.HookIssueReOpened:
|
||||
text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request re-opened: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueEdited:
|
||||
text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request edited: %s", p.Repository.FullName, titleLink)
|
||||
attachmentText = p.PullRequest.Body
|
||||
case api.HookIssueAssigned:
|
||||
list := make([]string, len(p.PullRequest.Assignees))
|
||||
for i, user := range p.PullRequest.Assignees {
|
||||
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
|
||||
}
|
||||
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s", repoLink,
|
||||
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s", p.Repository.FullName,
|
||||
strings.Join(list, ", "), titleLink)
|
||||
color = greenColor
|
||||
case api.HookIssueUnassigned:
|
||||
text = fmt.Sprintf("[%s] Pull request unassigned: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request unassigned: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueLabelUpdated:
|
||||
text = fmt.Sprintf("[%s] Pull request labels updated: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request labels updated: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueLabelCleared:
|
||||
text = fmt.Sprintf("[%s] Pull request labels cleared: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request labels cleared: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueSynchronized:
|
||||
text = fmt.Sprintf("[%s] Pull request synchronized: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request synchronized: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueMilestoned:
|
||||
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
|
||||
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", repoLink,
|
||||
linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", p.Repository.FullName, p.PullRequest.Milestone.Title, titleLink)
|
||||
case api.HookIssueDemilestoned:
|
||||
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueReviewed:
|
||||
text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request reviewed: %s", p.Repository.FullName, titleLink)
|
||||
attachmentText = p.Review.Content
|
||||
case api.HookIssueReviewRequested:
|
||||
text = fmt.Sprintf("[%s] Pull request review requested: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request review requested: %s", p.Repository.FullName, titleLink)
|
||||
case api.HookIssueReviewRequestRemoved:
|
||||
text = fmt.Sprintf("[%s] Pull request review request removed: %s", repoLink, titleLink)
|
||||
text = fmt.Sprintf("[%s] Pull request review request removed: %s", p.Repository.FullName, titleLink)
|
||||
}
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
return text, issueTitle, attachmentText, color
|
||||
}
|
||||
|
||||
func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
|
||||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||
refLink := linkFormatter(p.Repository.HTMLURL+"/releases/tag/"+util.PathEscapeSegments(p.Release.TagName), p.Release.TagName)
|
||||
|
||||
switch p.Action {
|
||||
case api.HookReleasePublished:
|
||||
text = fmt.Sprintf("[%s] Release created: %s", repoLink, refLink)
|
||||
text = fmt.Sprintf("[%s] Release created: %s", p.Repository.FullName, refLink)
|
||||
color = greenColor
|
||||
case api.HookReleaseUpdated:
|
||||
text = fmt.Sprintf("[%s] Release updated: %s", repoLink, refLink)
|
||||
text = fmt.Sprintf("[%s] Release updated: %s", p.Repository.FullName, refLink)
|
||||
color = yellowColor
|
||||
case api.HookReleaseDeleted:
|
||||
text = fmt.Sprintf("[%s] Release deleted: %s", repoLink, refLink)
|
||||
text = fmt.Sprintf("[%s] Release deleted: %s", p.Repository.FullName, refLink)
|
||||
color = redColor
|
||||
}
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
return text, color
|
||||
}
|
||||
|
||||
func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) {
|
||||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||
pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page)
|
||||
|
||||
var text string
|
||||
|
@ -237,12 +229,12 @@ func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSen
|
|||
|
||||
switch p.Action {
|
||||
case api.HookWikiCreated:
|
||||
text = fmt.Sprintf("[%s] New wiki page '%s'", repoLink, pageLink)
|
||||
text = fmt.Sprintf("[%s] New wiki page '%s'", p.Repository.FullName, pageLink)
|
||||
case api.HookWikiEdited:
|
||||
text = fmt.Sprintf("[%s] Wiki page '%s' edited", repoLink, pageLink)
|
||||
text = fmt.Sprintf("[%s] Wiki page '%s' edited", p.Repository.FullName, pageLink)
|
||||
color = yellowColor
|
||||
case api.HookWikiDeleted:
|
||||
text = fmt.Sprintf("[%s] Wiki page '%s' deleted", repoLink, pageLink)
|
||||
text = fmt.Sprintf("[%s] Wiki page '%s' deleted", p.Repository.FullName, pageLink)
|
||||
color = redColor
|
||||
}
|
||||
|
||||
|
@ -251,14 +243,13 @@ func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSen
|
|||
}
|
||||
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
return text, color, pageLink
|
||||
}
|
||||
|
||||
func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) {
|
||||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||
issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
|
||||
|
||||
var text, typ, titleLink string
|
||||
|
@ -274,20 +265,20 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo
|
|||
|
||||
switch p.Action {
|
||||
case api.HookIssueCommentCreated:
|
||||
text = fmt.Sprintf("[%s] New comment on %s %s", repoLink, typ, titleLink)
|
||||
text = fmt.Sprintf("[%s] New comment on %s %s", p.Repository.FullName, typ, titleLink)
|
||||
if p.IsPull {
|
||||
color = greenColorLight
|
||||
} else {
|
||||
color = orangeColorLight
|
||||
}
|
||||
case api.HookIssueCommentEdited:
|
||||
text = fmt.Sprintf("[%s] Comment edited on %s %s", repoLink, typ, titleLink)
|
||||
text = fmt.Sprintf("[%s] Comment edited on %s %s", p.Repository.FullName, typ, titleLink)
|
||||
case api.HookIssueCommentDeleted:
|
||||
text = fmt.Sprintf("[%s] Comment deleted on %s %s", repoLink, typ, titleLink)
|
||||
text = fmt.Sprintf("[%s] Comment deleted on %s %s", p.Repository.FullName, typ, titleLink)
|
||||
color = redColor
|
||||
}
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
return text, issueTitle, color
|
||||
|
@ -305,7 +296,7 @@ func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, w
|
|||
color = redColor
|
||||
}
|
||||
if withSender {
|
||||
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
|
||||
text += fmt.Sprintf(" by %s", p.Sender.UserName)
|
||||
}
|
||||
|
||||
return text, color
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue