Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Matrix webhook #10831

Merged
merged 14 commits into from
Mar 28, 2020
Prev Previous commit
Next Next commit
Add raw commit information to webhook
  • Loading branch information
S7evinK committed Mar 28, 2020
commit e00b4e5535b83203947af04e05912452a7bd5a3e
38 changes: 23 additions & 15 deletions modules/webhook/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
api "code.gitea.io/gitea/modules/structs"
)

const matrixPayloadSizeLimit = 1024 * 64

// MatrixMeta contains the Matrix metadata
type MatrixMeta struct {
HomeserverURL string `json:"homeserver_url"`
Expand Down Expand Up @@ -55,15 +57,17 @@ func (p *MatrixPayloadUnsafe) safePayload() *MatrixPayloadSafe {
MsgType: p.MsgType,
Format: p.Format,
FormattedBody: p.FormattedBody,
Commits: p.Commits,
}
}

// MatrixPayloadSafe contains (safe) payload for a Matrix room
type MatrixPayloadSafe struct {
Body string `json:"body"`
MsgType string `json:"msgtype"`
Format string `json:"format"`
FormattedBody string `json:"formatted_body"`
Body string `json:"body"`
MsgType string `json:"msgtype"`
Format string `json:"format"`
FormattedBody string `json:"formatted_body"`
Commits []*api.PayloadCommit `json:"io.gitea.commits,omitempty"`
}

// SetSecret sets the Matrix secret
Expand Down Expand Up @@ -101,7 +105,7 @@ func getMatrixCreatePayload(p *api.CreatePayload, matrix *MatrixMeta) (*MatrixPa
refLink := MatrixLinkToRef(p.Repo.HTMLURL, p.Ref)
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

// getMatrixDeletePayload composes Matrix payload for delete a branch or tag.
Expand All @@ -110,7 +114,7 @@ func getMatrixDeletePayload(p *api.DeletePayload, matrix *MatrixMeta) (*MatrixPa
repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

// getMatrixForkPayload composes Matrix payload for forked by a repository.
Expand All @@ -119,25 +123,25 @@ func getMatrixForkPayload(p *api.ForkPayload, matrix *MatrixMeta) (*MatrixPayloa
forkLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixIssuesPayload(p *api.IssuePayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
text, _, _, _ := getIssuesPayloadInfo(p, MatrixLinkFormatter, true)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixIssueCommentPayload(p *api.IssueCommentPayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
text, _, _ := getIssueCommentPayloadInfo(p, MatrixLinkFormatter, true)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixReleasePayload(p *api.ReleasePayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
text, _ := getReleasePayloadInfo(p, MatrixLinkFormatter, true)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixPushPayload(p *api.PushPayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
Expand All @@ -163,13 +167,13 @@ func getMatrixPushPayload(p *api.PushPayload, matrix *MatrixMeta) (*MatrixPayloa

}

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, p.Commits, matrix), nil
}

func getMatrixPullRequestPayload(p *api.PullRequestPayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
text, _, _, _ := getPullRequestPayloadInfo(p, MatrixLinkFormatter, true)

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixPullRequestApprovalPayload(p *api.PullRequestPayload, matrix *MatrixMeta, event models.HookEventType) (*MatrixPayloadUnsafe, error) {
Expand All @@ -189,7 +193,7 @@ func getMatrixPullRequestApprovalPayload(p *api.PullRequestPayload, matrix *Matr
text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", repoLink, action, title, titleLink, senderLink)
}

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

func getMatrixRepositoryPayload(p *api.RepositoryPayload, matrix *MatrixMeta) (*MatrixPayloadUnsafe, error) {
Expand All @@ -204,7 +208,7 @@ func getMatrixRepositoryPayload(p *api.RepositoryPayload, matrix *MatrixMeta) (*
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink)
}

return getMatrixPayloadUnsafe(text, matrix), nil
return getMatrixPayloadUnsafe(text, nil, matrix), nil
}

// GetMatrixPayload converts a Matrix webhook into a MatrixPayloadUnsafe
Expand Down Expand Up @@ -243,13 +247,14 @@ func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string)
return s, nil
}

func getMatrixPayloadUnsafe(text string, matrix *MatrixMeta) *MatrixPayloadUnsafe {
func getMatrixPayloadUnsafe(text string, commits []*api.PayloadCommit, matrix *MatrixMeta) *MatrixPayloadUnsafe {
p := MatrixPayloadUnsafe{}
p.AccessToken = matrix.AccessToken
p.FormattedBody = text
p.Body = getMessageBody(text)
p.Format = "org.matrix.custom.html"
p.MsgType = messageTypeText[matrix.MessageType]
p.Commits = commits
return &p
}

Expand Down Expand Up @@ -277,6 +282,9 @@ func getMatrixHookRequest(t *models.HookTask) (*http.Request, error) {
if payload, err = json.MarshalIndent(payloadsafe, "", " "); err != nil {
return nil, err
}
if len(payload) >= matrixPayloadSizeLimit {
return nil, fmt.Errorf("getMatrixHookRequest: payload size %d > %d", len(payload), matrixPayloadSizeLimit)
}
t.PayloadContent = string(payload)

req, err := http.NewRequest("POST", t.URL, strings.NewReader(string(payload)))
Expand Down
46 changes: 45 additions & 1 deletion modules/webhook/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ func TestMatrixHookRequest(t *testing.T) {
"msgtype": "m.notice",
"format": "org.matrix.custom.html",
"formatted_body": "[\u003ca href=\"http:https://localhost:3000/user1/test\"\u003euser1/test\u003c/a\u003e] user1 pushed 1 commit to \u003ca href=\"http:https://localhost:3000/user1/test/src/branch/master\"\u003emaster\u003c/a\u003e:\u003cbr\u003e\u003ca href=\"http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee\"\u003e5175ef2\u003c/a\u003e: Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n - user1",
"io.gitea.commits": [
{
"id": "5175ef26201c58b035a3404b3fe02b4e8d436eee",
"message": "Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n",
"url": "http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee",
"author": {
"name": "user1",
"email": "[email protected]",
"username": ""
},
"committer": {
"name": "user1",
"email": "[email protected]",
"username": ""
},
"verification": null,
"timestamp": "0001-01-01T00:00:00Z",
"added": null,
"removed": null,
"modified": null
}
],
"access_token": "dummy_access_token"
}`,
}
Expand All @@ -100,7 +122,29 @@ func TestMatrixHookRequest(t *testing.T) {
"body": "[[user1/test](http:https://localhost:3000/user1/test)] user1 pushed 1 commit to [master](http:https://localhost:3000/user1/test/src/branch/master):\n[5175ef2](http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee): Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n - user1",
"msgtype": "m.notice",
"format": "org.matrix.custom.html",
"formatted_body": "[\u003ca href=\"http:https://localhost:3000/user1/test\"\u003euser1/test\u003c/a\u003e] user1 pushed 1 commit to \u003ca href=\"http:https://localhost:3000/user1/test/src/branch/master\"\u003emaster\u003c/a\u003e:\u003cbr\u003e\u003ca href=\"http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee\"\u003e5175ef2\u003c/a\u003e: Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n - user1"
"formatted_body": "[\u003ca href=\"http:https://localhost:3000/user1/test\"\u003euser1/test\u003c/a\u003e] user1 pushed 1 commit to \u003ca href=\"http:https://localhost:3000/user1/test/src/branch/master\"\u003emaster\u003c/a\u003e:\u003cbr\u003e\u003ca href=\"http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee\"\u003e5175ef2\u003c/a\u003e: Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n - user1",
"io.gitea.commits": [
{
"id": "5175ef26201c58b035a3404b3fe02b4e8d436eee",
"message": "Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http:https://localhost:3000/user1/test/pulls/2\n",
"url": "http:https://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee",
"author": {
"name": "user1",
"email": "[email protected]",
"username": ""
},
"committer": {
"name": "user1",
"email": "[email protected]",
"username": ""
},
"verification": null,
"timestamp": "0001-01-01T00:00:00Z",
"added": null,
"removed": null,
"modified": null
}
]
}`

req, err := getMatrixHookRequest(h)
Expand Down