mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-02-22 19:35:47 -05: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
|
@ -150,12 +150,12 @@ func TestWebhookDeliverHookTask(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, `{"data": 42}`, string(body))
|
assert.Equal(t, `{"data": 42}`, string(body))
|
||||||
|
|
||||||
case "/webhook/6db5dc1e282529a8c162c7fe93dd2667494eeb51":
|
case "/webhook/86aaa4d69df5aa487cb0148af4ae7e546933057b":
|
||||||
// Version 2
|
// Version 2
|
||||||
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Len(t, body, 2147)
|
assert.Len(t, body, 1909)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
|
|
|
@ -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) {
|
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)
|
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
|
||||||
titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
|
titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
|
||||||
var text string
|
var text string
|
||||||
|
@ -100,39 +99,37 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, with
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookIssueOpened:
|
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
|
color = orangeColor
|
||||||
case api.HookIssueClosed:
|
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
|
color = redColor
|
||||||
case api.HookIssueReOpened:
|
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:
|
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:
|
case api.HookIssueAssigned:
|
||||||
list := make([]string, len(p.Issue.Assignees))
|
list := make([]string, len(p.Issue.Assignees))
|
||||||
for i, user := range p.Issue.Assignees {
|
for i, user := range p.Issue.Assignees {
|
||||||
list[i] = linkFormatter(setting.AppURL+url.PathEscape(user.UserName), user.UserName)
|
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
|
color = greenColor
|
||||||
case api.HookIssueUnassigned:
|
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:
|
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:
|
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:
|
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:
|
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", p.Repository.FullName, p.Issue.Milestone.Title, titleLink)
|
||||||
text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", repoLink,
|
|
||||||
linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink)
|
|
||||||
case api.HookIssueDemilestoned:
|
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 {
|
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
|
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) {
|
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)
|
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
||||||
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
|
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
|
||||||
var text string
|
var text string
|
||||||
|
@ -153,83 +149,79 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookIssueOpened:
|
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
|
attachmentText = p.PullRequest.Body
|
||||||
color = greenColor
|
color = greenColor
|
||||||
case api.HookIssueClosed:
|
case api.HookIssueClosed:
|
||||||
if p.PullRequest.HasMerged {
|
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
|
color = purpleColor
|
||||||
} else {
|
} 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
|
color = redColor
|
||||||
}
|
}
|
||||||
case api.HookIssueReOpened:
|
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:
|
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
|
attachmentText = p.PullRequest.Body
|
||||||
case api.HookIssueAssigned:
|
case api.HookIssueAssigned:
|
||||||
list := make([]string, len(p.PullRequest.Assignees))
|
list := make([]string, len(p.PullRequest.Assignees))
|
||||||
for i, user := range p.PullRequest.Assignees {
|
for i, user := range p.PullRequest.Assignees {
|
||||||
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
|
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)
|
strings.Join(list, ", "), titleLink)
|
||||||
color = greenColor
|
color = greenColor
|
||||||
case api.HookIssueUnassigned:
|
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:
|
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:
|
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:
|
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:
|
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", p.Repository.FullName, p.PullRequest.Milestone.Title, titleLink)
|
||||||
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", repoLink,
|
|
||||||
linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink)
|
|
||||||
case api.HookIssueDemilestoned:
|
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:
|
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
|
attachmentText = p.Review.Content
|
||||||
case api.HookIssueReviewRequested:
|
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:
|
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 {
|
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
|
return text, issueTitle, attachmentText, color
|
||||||
}
|
}
|
||||||
|
|
||||||
func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
|
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)
|
refLink := linkFormatter(p.Repository.HTMLURL+"/releases/tag/"+util.PathEscapeSegments(p.Release.TagName), p.Release.TagName)
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookReleasePublished:
|
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
|
color = greenColor
|
||||||
case api.HookReleaseUpdated:
|
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
|
color = yellowColor
|
||||||
case api.HookReleaseDeleted:
|
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
|
color = redColor
|
||||||
}
|
}
|
||||||
if withSender {
|
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
|
return text, color
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) {
|
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)
|
pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page)
|
||||||
|
|
||||||
var text string
|
var text string
|
||||||
|
@ -237,12 +229,12 @@ func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSen
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookWikiCreated:
|
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:
|
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
|
color = yellowColor
|
||||||
case api.HookWikiDeleted:
|
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
|
color = redColor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,14 +243,13 @@ func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSen
|
||||||
}
|
}
|
||||||
|
|
||||||
if withSender {
|
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
|
return text, color, pageLink
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) {
|
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)
|
issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
|
||||||
|
|
||||||
var text, typ, titleLink string
|
var text, typ, titleLink string
|
||||||
|
@ -274,20 +265,20 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookIssueCommentCreated:
|
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 {
|
if p.IsPull {
|
||||||
color = greenColorLight
|
color = greenColorLight
|
||||||
} else {
|
} else {
|
||||||
color = orangeColorLight
|
color = orangeColorLight
|
||||||
}
|
}
|
||||||
case api.HookIssueCommentEdited:
|
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:
|
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
|
color = redColor
|
||||||
}
|
}
|
||||||
if withSender {
|
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
|
return text, issueTitle, color
|
||||||
|
@ -305,7 +296,7 @@ func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, w
|
||||||
color = redColor
|
color = redColor
|
||||||
}
|
}
|
||||||
if withSender {
|
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
|
return text, color
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -19,7 +20,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/svg"
|
"code.gitea.io/gitea/modules/svg"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
@ -143,9 +143,8 @@ func (m matrixConvertor) newPayload(text string, commits ...*api.PayloadCommit)
|
||||||
|
|
||||||
// Create implements payloadConvertor Create method
|
// Create implements payloadConvertor Create method
|
||||||
func (m matrixConvertor) Create(p *api.CreatePayload) (MatrixPayload, error) {
|
func (m matrixConvertor) Create(p *api.CreatePayload) (MatrixPayload, error) {
|
||||||
repoLink := htmlLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
|
||||||
refLink := MatrixLinkToRef(p.Repo.HTMLURL, p.Ref)
|
refLink := MatrixLinkToRef(p.Repo.HTMLURL, p.Ref)
|
||||||
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
|
text := fmt.Sprintf("[%s:%s] %s created by %s", p.Repo.FullName, refLink, p.RefType, p.Sender.UserName)
|
||||||
|
|
||||||
return m.newPayload(text)
|
return m.newPayload(text)
|
||||||
}
|
}
|
||||||
|
@ -206,9 +205,8 @@ func (m matrixConvertor) Push(p *api.PushPayload) (MatrixPayload, error) {
|
||||||
commitDesc = fmt.Sprintf("%d commits", p.TotalCommits)
|
commitDesc = fmt.Sprintf("%d commits", p.TotalCommits)
|
||||||
}
|
}
|
||||||
|
|
||||||
repoLink := htmlLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
refName := html.EscapeString(git.RefName(p.Ref).ShortName())
|
||||||
branchLink := MatrixLinkToRef(p.Repo.HTMLURL, p.Ref)
|
text := fmt.Sprintf("[%s] %s pushed %s to %s:<br>", p.Repo.FullName, p.Pusher.UserName, commitDesc, refName)
|
||||||
text := fmt.Sprintf("[%s] %s pushed %s to %s:<br>", repoLink, p.Pusher.UserName, commitDesc, branchLink)
|
|
||||||
|
|
||||||
// for each commit, generate a new line text
|
// for each commit, generate a new line text
|
||||||
for i, commit := range p.Commits {
|
for i, commit := range p.Commits {
|
||||||
|
@ -231,10 +229,8 @@ func (m matrixConvertor) PullRequest(p *api.PullRequestPayload) (MatrixPayload,
|
||||||
|
|
||||||
// Review implements payloadConvertor Review method
|
// Review implements payloadConvertor Review method
|
||||||
func (m matrixConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (MatrixPayload, error) {
|
func (m matrixConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (MatrixPayload, error) {
|
||||||
senderLink := htmlLinkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
|
|
||||||
title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
||||||
titleLink := htmlLinkFormatter(p.PullRequest.HTMLURL, title)
|
titleLink := htmlLinkFormatter(p.PullRequest.HTMLURL, title)
|
||||||
repoLink := htmlLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
if p.Action == api.HookIssueReviewed {
|
if p.Action == api.HookIssueReviewed {
|
||||||
|
@ -243,7 +239,7 @@ func (m matrixConvertor) Review(p *api.PullRequestPayload, event webhook_module.
|
||||||
return MatrixPayload{}, err
|
return MatrixPayload{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
text = fmt.Sprintf("[%s] Pull request review %s: %s by %s", repoLink, action, titleLink, senderLink)
|
text = fmt.Sprintf("[%s] Pull request review %s: %s by %s", p.Repository.FullName, action, titleLink, p.Sender.UserName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.newPayload(text)
|
return m.newPayload(text)
|
||||||
|
@ -251,29 +247,27 @@ func (m matrixConvertor) Review(p *api.PullRequestPayload, event webhook_module.
|
||||||
|
|
||||||
// Repository implements payloadConvertor Repository method
|
// Repository implements payloadConvertor Repository method
|
||||||
func (m matrixConvertor) Repository(p *api.RepositoryPayload) (MatrixPayload, error) {
|
func (m matrixConvertor) Repository(p *api.RepositoryPayload) (MatrixPayload, error) {
|
||||||
senderLink := htmlLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
|
|
||||||
repoLink := htmlLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
repoLink := htmlLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookRepoCreated:
|
case api.HookRepoCreated:
|
||||||
text = fmt.Sprintf("[%s] Repository created by %s", repoLink, senderLink)
|
text = fmt.Sprintf("[%s] Repository created by %s", repoLink, p.Sender.UserName)
|
||||||
case api.HookRepoDeleted:
|
case api.HookRepoDeleted:
|
||||||
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink)
|
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, p.Sender.UserName)
|
||||||
}
|
}
|
||||||
return m.newPayload(text)
|
return m.newPayload(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m matrixConvertor) Package(p *api.PackagePayload) (MatrixPayload, error) {
|
func (m matrixConvertor) Package(p *api.PackagePayload) (MatrixPayload, error) {
|
||||||
senderLink := htmlLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
|
|
||||||
packageLink := htmlLinkFormatter(p.Package.HTMLURL, p.Package.Name)
|
packageLink := htmlLinkFormatter(p.Package.HTMLURL, p.Package.Name)
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookPackageCreated:
|
case api.HookPackageCreated:
|
||||||
text = fmt.Sprintf("[%s] Package published by %s", packageLink, senderLink)
|
text = fmt.Sprintf("[%s] Package published by %s", packageLink, p.Sender.UserName)
|
||||||
case api.HookPackageDeleted:
|
case api.HookPackageDeleted:
|
||||||
text = fmt.Sprintf("[%s] Package deleted by %s", packageLink, senderLink)
|
text = fmt.Sprintf("[%s] Package deleted by %s", packageLink, p.Sender.UserName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.newPayload(text)
|
return m.newPayload(text)
|
||||||
|
|
|
@ -28,8 +28,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1", pl.Body)
|
assert.Equal(t, "[test/repo:[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Delete", func(t *testing.T) {
|
t.Run("Delete", func(t *testing.T) {
|
||||||
|
@ -61,8 +61,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", pl.Body)
|
assert.Equal(t, "[test/repo] user1 pushed 2 commits to test:\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] user1 pushed 2 commits to <a href="http://localhost:3000/test/repo/src/branch/test">test</a>:<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] user1 pushed 2 commits to test:<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Issue", func(t *testing.T) {
|
t.Run("Issue", func(t *testing.T) {
|
||||||
|
@ -73,16 +73,16 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Issue opened: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by user1`, pl.FormattedBody)
|
||||||
|
|
||||||
p.Action = api.HookIssueClosed
|
p.Action = api.HookIssueClosed
|
||||||
pl, err = mc.Issue(p)
|
pl, err = mc.Issue(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Issue closed: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("IssueComment", func(t *testing.T) {
|
t.Run("IssueComment", func(t *testing.T) {
|
||||||
|
@ -92,8 +92,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] New comment on issue <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PullRequest", func(t *testing.T) {
|
t.Run("PullRequest", func(t *testing.T) {
|
||||||
|
@ -103,8 +103,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PullRequestComment", func(t *testing.T) {
|
t.Run("PullRequestComment", func(t *testing.T) {
|
||||||
|
@ -114,8 +114,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Review", func(t *testing.T) {
|
t.Run("Review", func(t *testing.T) {
|
||||||
|
@ -126,8 +126,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request review approved: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Pull request review approved: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Repository", func(t *testing.T) {
|
t.Run("Repository", func(t *testing.T) {
|
||||||
|
@ -137,8 +137,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, `[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`, pl.Body)
|
assert.Equal(t, `[[test/repo](http://localhost:3000/test/repo)] Repository created by user1`, pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Package", func(t *testing.T) {
|
t.Run("Package", func(t *testing.T) {
|
||||||
|
@ -148,8 +148,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, `[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`, pl.Body)
|
assert.Equal(t, `[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by user1`, pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Wiki", func(t *testing.T) {
|
t.Run("Wiki", func(t *testing.T) {
|
||||||
|
@ -160,24 +160,24 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by user1`, pl.FormattedBody)
|
||||||
|
|
||||||
p.Action = api.HookWikiEdited
|
p.Action = api.HookWikiEdited
|
||||||
pl, err = mc.Wiki(p)
|
pl, err = mc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by user1`, pl.FormattedBody)
|
||||||
|
|
||||||
p.Action = api.HookWikiDeleted
|
p.Action = api.HookWikiDeleted
|
||||||
pl, err = mc.Wiki(p)
|
pl, err = mc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Release", func(t *testing.T) {
|
t.Run("Release", func(t *testing.T) {
|
||||||
|
@ -187,8 +187,8 @@ func TestMatrixPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, pl)
|
require.NotNil(t, pl)
|
||||||
|
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by [user1](https://try.gitea.io/user1)", pl.Body)
|
assert.Equal(t, "[test/repo] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by user1", pl.Body)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0">v1.0</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.FormattedBody)
|
assert.Equal(t, `[test/repo] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0">v1.0</a> by user1`, pl.FormattedBody)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,12 +217,12 @@ func TestMatrixJSONPayload(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "PUT", req.Method)
|
assert.Equal(t, "PUT", req.Method)
|
||||||
assert.Equal(t, "/_matrix/client/v3/rooms/ROOM_ID/send/m.room.message/6db5dc1e282529a8c162c7fe93dd2667494eeb51", req.URL.Path)
|
assert.Equal(t, "/_matrix/client/v3/rooms/ROOM_ID/send/m.room.message/86aaa4d69df5aa487cb0148af4ae7e546933057b", req.URL.Path)
|
||||||
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
|
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
|
||||||
var body MatrixPayload
|
var body MatrixPayload
|
||||||
err = json.NewDecoder(req.Body).Decode(&body)
|
err = json.NewDecoder(req.Body).Decode(&body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", body.Body)
|
assert.Equal(t, "[test/repo] user1 pushed 2 commits to test:\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1", body.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_getTxnID(t *testing.T) {
|
func Test_getTxnID(t *testing.T) {
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
gitea_context "code.gitea.io/gitea/services/context"
|
gitea_context "code.gitea.io/gitea/services/context"
|
||||||
|
@ -145,9 +144,8 @@ func SlackLinkToRef(repoURL, ref string) string {
|
||||||
|
|
||||||
// Create implements payloadConvertor Create method
|
// Create implements payloadConvertor Create method
|
||||||
func (s slackConvertor) Create(p *api.CreatePayload) (SlackPayload, error) {
|
func (s slackConvertor) Create(p *api.CreatePayload) (SlackPayload, error) {
|
||||||
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
|
||||||
refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
||||||
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
|
text := fmt.Sprintf("[%s:%s] %s created by %s", p.Repo.FullName, refLink, p.RefType, p.Sender.UserName)
|
||||||
|
|
||||||
return s.createPayload(text, nil), nil
|
return s.createPayload(text, nil), nil
|
||||||
}
|
}
|
||||||
|
@ -240,9 +238,8 @@ func (s slackConvertor) Push(p *api.PushPayload) (SlackPayload, error) {
|
||||||
commitString = commitDesc
|
commitString = commitDesc
|
||||||
}
|
}
|
||||||
|
|
||||||
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
|
||||||
branchLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
branchLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
||||||
text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName)
|
text := fmt.Sprintf("[%s:%s] %s pushed by %s", p.Repo.FullName, branchLink, commitString, p.Pusher.UserName)
|
||||||
|
|
||||||
var attachmentText string
|
var attachmentText string
|
||||||
// for each commit, generate attachment text
|
// for each commit, generate attachment text
|
||||||
|
@ -283,10 +280,8 @@ func (s slackConvertor) PullRequest(p *api.PullRequestPayload) (SlackPayload, er
|
||||||
|
|
||||||
// Review implements payloadConvertor Review method
|
// Review implements payloadConvertor Review method
|
||||||
func (s slackConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (SlackPayload, error) {
|
func (s slackConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (SlackPayload, error) {
|
||||||
senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
|
|
||||||
title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
||||||
titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
|
titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
|
||||||
repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
if p.Action == api.HookIssueReviewed {
|
if p.Action == api.HookIssueReviewed {
|
||||||
|
@ -295,7 +290,7 @@ func (s slackConvertor) Review(p *api.PullRequestPayload, event webhook_module.H
|
||||||
return SlackPayload{}, err
|
return SlackPayload{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", repoLink, action, title, titleLink, senderLink)
|
text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", p.Repository.FullName, action, title, titleLink, p.Sender.UserName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.createPayload(text, nil), nil
|
return s.createPayload(text, nil), nil
|
||||||
|
@ -303,15 +298,14 @@ func (s slackConvertor) Review(p *api.PullRequestPayload, event webhook_module.H
|
||||||
|
|
||||||
// Repository implements payloadConvertor Repository method
|
// Repository implements payloadConvertor Repository method
|
||||||
func (s slackConvertor) Repository(p *api.RepositoryPayload) (SlackPayload, error) {
|
func (s slackConvertor) Repository(p *api.RepositoryPayload) (SlackPayload, error) {
|
||||||
senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
|
|
||||||
repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
switch p.Action {
|
switch p.Action {
|
||||||
case api.HookRepoCreated:
|
case api.HookRepoCreated:
|
||||||
text = fmt.Sprintf("[%s] Repository created by %s", repoLink, senderLink)
|
text = fmt.Sprintf("[%s] Repository created by %s", repoLink, p.Sender.UserName)
|
||||||
case api.HookRepoDeleted:
|
case api.HookRepoDeleted:
|
||||||
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink)
|
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, p.Sender.UserName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.createPayload(text, nil), nil
|
return s.createPayload(text, nil), nil
|
||||||
|
|
|
@ -25,7 +25,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Create(p)
|
pl, err := sc.Create(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1", pl.Text)
|
assert.Equal(t, "[test/repo:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Delete", func(t *testing.T) {
|
t.Run("Delete", func(t *testing.T) {
|
||||||
|
@ -52,7 +52,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Push(p)
|
pl, err := sc.Push(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", pl.Text)
|
assert.Equal(t, "[test/repo:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Issue", func(t *testing.T) {
|
t.Run("Issue", func(t *testing.T) {
|
||||||
|
@ -62,13 +62,13 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Issue(p)
|
pl, err := sc.Issue(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by user1", pl.Text)
|
||||||
|
|
||||||
p.Action = api.HookIssueClosed
|
p.Action = api.HookIssueClosed
|
||||||
pl, err = sc.Issue(p)
|
pl, err = sc.Issue(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("IssueComment", func(t *testing.T) {
|
t.Run("IssueComment", func(t *testing.T) {
|
||||||
|
@ -77,7 +77,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.IssueComment(p)
|
pl, err := sc.IssueComment(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] New comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PullRequest", func(t *testing.T) {
|
t.Run("PullRequest", func(t *testing.T) {
|
||||||
|
@ -86,7 +86,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.PullRequest(p)
|
pl, err := sc.PullRequest(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PullRequestComment", func(t *testing.T) {
|
t.Run("PullRequestComment", func(t *testing.T) {
|
||||||
|
@ -95,7 +95,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.IssueComment(p)
|
pl, err := sc.IssueComment(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] New comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Review", func(t *testing.T) {
|
t.Run("Review", func(t *testing.T) {
|
||||||
|
@ -105,7 +105,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
|
pl, err := sc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Repository", func(t *testing.T) {
|
t.Run("Repository", func(t *testing.T) {
|
||||||
|
@ -114,7 +114,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Repository(p)
|
pl, err := sc.Repository(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Package", func(t *testing.T) {
|
t.Run("Package", func(t *testing.T) {
|
||||||
|
@ -123,7 +123,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Package(p)
|
pl, err := sc.Package(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Wiki", func(t *testing.T) {
|
t.Run("Wiki", func(t *testing.T) {
|
||||||
|
@ -133,19 +133,19 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Wiki(p)
|
pl, err := sc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by user1", pl.Text)
|
||||||
|
|
||||||
p.Action = api.HookWikiEdited
|
p.Action = api.HookWikiEdited
|
||||||
pl, err = sc.Wiki(p)
|
pl, err = sc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by user1", pl.Text)
|
||||||
|
|
||||||
p.Action = api.HookWikiDeleted
|
p.Action = api.HookWikiDeleted
|
||||||
pl, err = sc.Wiki(p)
|
pl, err = sc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by user1", pl.Text)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Release", func(t *testing.T) {
|
t.Run("Release", func(t *testing.T) {
|
||||||
|
@ -154,7 +154,7 @@ func TestSlackPayload(t *testing.T) {
|
||||||
pl, err := sc.Release(p)
|
pl, err := sc.Release(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by <https://try.gitea.io/user1|user1>", pl.Text)
|
assert.Equal(t, "[test/repo] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by user1", pl.Text)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ func TestSlackJSONPayload(t *testing.T) {
|
||||||
var body SlackPayload
|
var body SlackPayload
|
||||||
err = json.NewDecoder(req.Body).Decode(&body)
|
err = json.NewDecoder(req.Body).Decode(&body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", body.Text)
|
assert.Equal(t, "[test/repo:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1", body.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValidSlackChannel(t *testing.T) {
|
func TestIsValidSlackChannel(t *testing.T) {
|
||||||
|
|
|
@ -79,7 +79,7 @@ func (telegramHandler) Metadata(w *webhook_model.Webhook) any {
|
||||||
func (t telegramConvertor) Create(p *api.CreatePayload) (TelegramPayload, error) {
|
func (t telegramConvertor) Create(p *api.CreatePayload) (TelegramPayload, error) {
|
||||||
// created tag/branch
|
// created tag/branch
|
||||||
refName := git.RefName(p.Ref).ShortName()
|
refName := git.RefName(p.Ref).ShortName()
|
||||||
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
|
title := fmt.Sprintf(`[%s] %s <a href="%s">%s</a> created`, p.Repo.FullName, p.RefType,
|
||||||
p.Repo.HTMLURL+"/src/"+refName, refName)
|
p.Repo.HTMLURL+"/src/"+refName, refName)
|
||||||
|
|
||||||
return createTelegramPayload(title), nil
|
return createTelegramPayload(title), nil
|
||||||
|
@ -89,7 +89,7 @@ func (t telegramConvertor) Create(p *api.CreatePayload) (TelegramPayload, error)
|
||||||
func (t telegramConvertor) Delete(p *api.DeletePayload) (TelegramPayload, error) {
|
func (t telegramConvertor) Delete(p *api.DeletePayload) (TelegramPayload, error) {
|
||||||
// created tag/branch
|
// created tag/branch
|
||||||
refName := git.RefName(p.Ref).ShortName()
|
refName := git.RefName(p.Ref).ShortName()
|
||||||
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
|
title := fmt.Sprintf(`[%s] %s <a href="%s">%s</a> deleted`, p.Repo.FullName, p.RefType,
|
||||||
p.Repo.HTMLURL+"/src/"+refName, refName)
|
p.Repo.HTMLURL+"/src/"+refName, refName)
|
||||||
|
|
||||||
return createTelegramPayload(title), nil
|
return createTelegramPayload(title), nil
|
||||||
|
@ -108,19 +108,13 @@ func (t telegramConvertor) Push(p *api.PushPayload) (TelegramPayload, error) {
|
||||||
branchName = git.RefName(p.Ref).ShortName()
|
branchName = git.RefName(p.Ref).ShortName()
|
||||||
commitDesc string
|
commitDesc string
|
||||||
)
|
)
|
||||||
|
|
||||||
var titleLink string
|
|
||||||
if p.TotalCommits == 1 {
|
if p.TotalCommits == 1 {
|
||||||
commitDesc = "1 new commit"
|
commitDesc = "1 new commit"
|
||||||
titleLink = p.Commits[0].URL
|
|
||||||
} else {
|
} else {
|
||||||
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
|
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
|
||||||
titleLink = p.CompareURL
|
|
||||||
}
|
}
|
||||||
if titleLink == "" {
|
|
||||||
titleLink = p.Repo.HTMLURL + "/src/" + branchName
|
title := fmt.Sprintf(`[%s:%s] %s`, p.Repo.FullName, branchName, commitDesc)
|
||||||
}
|
|
||||||
title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, branchName, commitDesc)
|
|
||||||
|
|
||||||
var text string
|
var text string
|
||||||
// for each commit, generate attachment text
|
// for each commit, generate attachment text
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestTelegramPayload(t *testing.T) {
|
||||||
pl, err := tc.Create(p)
|
pl, err := tc.Create(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> created`, pl.Message)
|
assert.Equal(t, `[test/repo] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> created`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Delete", func(t *testing.T) {
|
t.Run("Delete", func(t *testing.T) {
|
||||||
|
@ -42,7 +42,7 @@ func TestTelegramPayload(t *testing.T) {
|
||||||
pl, err := tc.Delete(p)
|
pl, err := tc.Delete(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> deleted`, pl.Message)
|
assert.Equal(t, `[test/repo] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> deleted`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Fork", func(t *testing.T) {
|
t.Run("Fork", func(t *testing.T) {
|
||||||
|
@ -60,7 +60,7 @@ func TestTelegramPayload(t *testing.T) {
|
||||||
pl, err := tc.Push(p)
|
pl, err := tc.Push(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
|
assert.Equal(t, `[test/repo:test] 2 new commits
|
||||||
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
|
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
|
||||||
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, pl.Message)
|
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, pl.Message)
|
||||||
})
|
})
|
||||||
|
@ -72,7 +72,7 @@ func TestTelegramPayload(t *testing.T) {
|
||||||
pl, err := tc.Issue(p)
|
pl, err := tc.Issue(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
|
assert.Equal(t, `[test/repo] Issue opened: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by user1
|
||||||
|
|
||||||
issue body`, pl.Message)
|
issue body`, pl.Message)
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ issue body`, pl.Message)
|
||||||
pl, err = tc.Issue(p)
|
pl, err = tc.Issue(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `[test/repo] Issue closed: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by user1`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("IssueComment", func(t *testing.T) {
|
t.Run("IssueComment", func(t *testing.T) {
|
||||||
|
@ -89,7 +89,7 @@ issue body`, pl.Message)
|
||||||
pl, err := tc.IssueComment(p)
|
pl, err := tc.IssueComment(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
|
assert.Equal(t, `[test/repo] New comment on issue <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by user1
|
||||||
more info needed`, pl.Message)
|
more info needed`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ more info needed`, pl.Message)
|
||||||
pl, err := tc.PullRequest(p)
|
pl, err := tc.PullRequest(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
|
assert.Equal(t, `[test/repo] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by user1
|
||||||
fixes bug #2`, pl.Message)
|
fixes bug #2`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ fixes bug #2`, pl.Message)
|
||||||
pl, err := tc.IssueComment(p)
|
pl, err := tc.IssueComment(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
|
assert.Equal(t, `[test/repo] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by user1
|
||||||
changes requested`, pl.Message)
|
changes requested`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ good job`, pl.Message)
|
||||||
pl, err := tc.Package(p)
|
pl, err := tc.Package(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest" rel="nofollow">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest" rel="nofollow">GiteaContainer:latest</a> by user1`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Wiki", func(t *testing.T) {
|
t.Run("Wiki", func(t *testing.T) {
|
||||||
|
@ -149,19 +149,19 @@ good job`, pl.Message)
|
||||||
pl, err := tc.Wiki(p)
|
pl, err := tc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `[test/repo] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' (Wiki change comment) by user1`, pl.Message)
|
||||||
|
|
||||||
p.Action = api.HookWikiEdited
|
p.Action = api.HookWikiEdited
|
||||||
pl, err = tc.Wiki(p)
|
pl, err = tc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `[test/repo] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' edited (Wiki change comment) by user1`, pl.Message)
|
||||||
|
|
||||||
p.Action = api.HookWikiDeleted
|
p.Action = api.HookWikiDeleted
|
||||||
pl, err = tc.Wiki(p)
|
pl, err = tc.Wiki(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' deleted by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `[test/repo] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>' deleted by user1`, pl.Message)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Release", func(t *testing.T) {
|
t.Run("Release", func(t *testing.T) {
|
||||||
|
@ -170,7 +170,7 @@ good job`, pl.Message)
|
||||||
pl, err := tc.Release(p)
|
pl, err := tc.Release(p)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0" rel="nofollow">v1.0</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
|
assert.Equal(t, `[test/repo] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0" rel="nofollow">v1.0</a> by user1`, pl.Message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ func TestTelegramJSONPayload(t *testing.T) {
|
||||||
var body TelegramPayload
|
var body TelegramPayload
|
||||||
err = json.NewDecoder(req.Body).Decode(&body)
|
err = json.NewDecoder(req.Body).Decode(&body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
|
assert.Equal(t, `[test/repo:test] 2 new commits
|
||||||
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
|
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
|
||||||
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, body.Message)
|
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, body.Message)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue