|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/git" |
| 13 | + api "code.gitea.io/sdk/gitea" |
| 14 | + |
| 15 | + dingtalk "github.com/lunny/dingtalk_webhook" |
| 16 | +) |
| 17 | + |
| 18 | +type ( |
| 19 | + // DingtalkPayload represents |
| 20 | + DingtalkPayload dingtalk.Payload |
| 21 | +) |
| 22 | + |
| 23 | +// SetSecret sets the dingtalk secret |
| 24 | +func (p *DingtalkPayload) SetSecret(_ string) {} |
| 25 | + |
| 26 | +// JSONPayload Marshals the DingtalkPayload to json |
| 27 | +func (p *DingtalkPayload) JSONPayload() ([]byte, error) { |
| 28 | + data, err := json.MarshalIndent(p, "", " ") |
| 29 | + if err != nil { |
| 30 | + return []byte{}, err |
| 31 | + } |
| 32 | + return data, nil |
| 33 | +} |
| 34 | + |
| 35 | +func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) { |
| 36 | + // created tag/branch |
| 37 | + refName := git.RefEndName(p.Ref) |
| 38 | + title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) |
| 39 | + |
| 40 | + return &DingtalkPayload{ |
| 41 | + MsgType: "actionCard", |
| 42 | + ActionCard: dingtalk.ActionCard{ |
| 43 | + Text: title, |
| 44 | + Title: title, |
| 45 | + HideAvatar: "0", |
| 46 | + SingleTitle: fmt.Sprintf("view branch %s", refName), |
| 47 | + SingleURL: p.Repo.HTMLURL + "/src/" + refName, |
| 48 | + }, |
| 49 | + }, nil |
| 50 | +} |
| 51 | + |
| 52 | +func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) { |
| 53 | + var ( |
| 54 | + branchName = git.RefEndName(p.Ref) |
| 55 | + commitDesc string |
| 56 | + ) |
| 57 | + |
| 58 | + var titleLink, linkText string |
| 59 | + if len(p.Commits) == 1 { |
| 60 | + commitDesc = "1 new commit" |
| 61 | + titleLink = p.Commits[0].URL |
| 62 | + linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7]) |
| 63 | + } else { |
| 64 | + commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) |
| 65 | + titleLink = p.CompareURL |
| 66 | + linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7]) |
| 67 | + } |
| 68 | + if titleLink == "" { |
| 69 | + titleLink = p.Repo.HTMLURL + "/src/" + branchName |
| 70 | + } |
| 71 | + |
| 72 | + title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc) |
| 73 | + |
| 74 | + var text string |
| 75 | + // for each commit, generate attachment text |
| 76 | + for i, commit := range p.Commits { |
| 77 | + var authorName string |
| 78 | + if commit.Author != nil { |
| 79 | + authorName = " - " + commit.Author.Name |
| 80 | + } |
| 81 | + text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL, |
| 82 | + strings.TrimRight(commit.Message, "\r\n")) + authorName |
| 83 | + // add linebreak to each commit but the last |
| 84 | + if i < len(p.Commits)-1 { |
| 85 | + text += "\n" |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return &DingtalkPayload{ |
| 90 | + MsgType: "actionCard", |
| 91 | + ActionCard: dingtalk.ActionCard{ |
| 92 | + Text: text, |
| 93 | + Title: title, |
| 94 | + HideAvatar: "0", |
| 95 | + SingleTitle: linkText, |
| 96 | + SingleURL: titleLink, |
| 97 | + }, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) { |
| 102 | + var text, title string |
| 103 | + switch p.Action { |
| 104 | + case api.HookIssueOpened: |
| 105 | + title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 106 | + text = p.PullRequest.Body |
| 107 | + case api.HookIssueClosed: |
| 108 | + if p.PullRequest.HasMerged { |
| 109 | + title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 110 | + } else { |
| 111 | + title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 112 | + } |
| 113 | + text = p.PullRequest.Body |
| 114 | + case api.HookIssueReOpened: |
| 115 | + title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 116 | + text = p.PullRequest.Body |
| 117 | + case api.HookIssueEdited: |
| 118 | + title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 119 | + text = p.PullRequest.Body |
| 120 | + case api.HookIssueAssigned: |
| 121 | + title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName, |
| 122 | + p.PullRequest.Assignee.UserName, p.Index, p.PullRequest.Title) |
| 123 | + text = p.PullRequest.Body |
| 124 | + case api.HookIssueUnassigned: |
| 125 | + title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 126 | + text = p.PullRequest.Body |
| 127 | + case api.HookIssueLabelUpdated: |
| 128 | + title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 129 | + text = p.PullRequest.Body |
| 130 | + case api.HookIssueLabelCleared: |
| 131 | + title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 132 | + text = p.PullRequest.Body |
| 133 | + case api.HookIssueSynchronized: |
| 134 | + title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) |
| 135 | + text = p.PullRequest.Body |
| 136 | + } |
| 137 | + |
| 138 | + return &DingtalkPayload{ |
| 139 | + MsgType: "actionCard", |
| 140 | + ActionCard: dingtalk.ActionCard{ |
| 141 | + Text: text, |
| 142 | + Title: title, |
| 143 | + HideAvatar: "0", |
| 144 | + SingleTitle: "view pull request", |
| 145 | + SingleURL: p.PullRequest.HTMLURL, |
| 146 | + }, |
| 147 | + }, nil |
| 148 | +} |
| 149 | + |
| 150 | +func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) { |
| 151 | + var title, url string |
| 152 | + switch p.Action { |
| 153 | + case api.HookRepoCreated: |
| 154 | + title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName) |
| 155 | + url = p.Repository.HTMLURL |
| 156 | + return &DingtalkPayload{ |
| 157 | + MsgType: "actionCard", |
| 158 | + ActionCard: dingtalk.ActionCard{ |
| 159 | + Text: title, |
| 160 | + Title: title, |
| 161 | + HideAvatar: "0", |
| 162 | + SingleTitle: "view repository", |
| 163 | + SingleURL: url, |
| 164 | + }, |
| 165 | + }, nil |
| 166 | + case api.HookRepoDeleted: |
| 167 | + title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) |
| 168 | + return &DingtalkPayload{ |
| 169 | + MsgType: "text", |
| 170 | + Text: struct { |
| 171 | + Content string `json:"content"` |
| 172 | + }{ |
| 173 | + Content: title, |
| 174 | + }, |
| 175 | + }, nil |
| 176 | + } |
| 177 | + |
| 178 | + return nil, nil |
| 179 | +} |
| 180 | + |
| 181 | +// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload |
| 182 | +func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) { |
| 183 | + s := new(DingtalkPayload) |
| 184 | + |
| 185 | + switch event { |
| 186 | + case HookEventCreate: |
| 187 | + return getDingtalkCreatePayload(p.(*api.CreatePayload)) |
| 188 | + case HookEventPush: |
| 189 | + return getDingtalkPushPayload(p.(*api.PushPayload)) |
| 190 | + case HookEventPullRequest: |
| 191 | + return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload)) |
| 192 | + case HookEventRepository: |
| 193 | + return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload)) |
| 194 | + } |
| 195 | + |
| 196 | + return s, nil |
| 197 | +} |
0 commit comments