code.gitea.io/gitea@v1.22.3/services/webhook/wechatwork.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webhook
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"net/http"
    10  	"strings"
    11  
    12  	webhook_model "code.gitea.io/gitea/models/webhook"
    13  	"code.gitea.io/gitea/modules/git"
    14  	api "code.gitea.io/gitea/modules/structs"
    15  	webhook_module "code.gitea.io/gitea/modules/webhook"
    16  )
    17  
    18  type (
    19  	// WechatworkPayload represents
    20  	WechatworkPayload struct {
    21  		Msgtype string `json:"msgtype"`
    22  		Text    struct {
    23  			Content             string   `json:"content"`
    24  			MentionedList       []string `json:"mentioned_list"`
    25  			MentionedMobileList []string `json:"mentioned_mobile_list"`
    26  		} `json:"text"`
    27  		Markdown struct {
    28  			Content string `json:"content"`
    29  		} `json:"markdown"`
    30  	}
    31  )
    32  
    33  func newWechatworkMarkdownPayload(title string) WechatworkPayload {
    34  	return WechatworkPayload{
    35  		Msgtype: "markdown",
    36  		Markdown: struct {
    37  			Content string `json:"content"`
    38  		}{
    39  			Content: title,
    40  		},
    41  	}
    42  }
    43  
    44  // Create implements PayloadConvertor Create method
    45  func (wc wechatworkConvertor) Create(p *api.CreatePayload) (WechatworkPayload, error) {
    46  	// created tag/branch
    47  	refName := git.RefName(p.Ref).ShortName()
    48  	title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
    49  
    50  	return newWechatworkMarkdownPayload(title), nil
    51  }
    52  
    53  // Delete implements PayloadConvertor Delete method
    54  func (wc wechatworkConvertor) Delete(p *api.DeletePayload) (WechatworkPayload, error) {
    55  	// created tag/branch
    56  	refName := git.RefName(p.Ref).ShortName()
    57  	title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
    58  
    59  	return newWechatworkMarkdownPayload(title), nil
    60  }
    61  
    62  // Fork implements PayloadConvertor Fork method
    63  func (wc wechatworkConvertor) Fork(p *api.ForkPayload) (WechatworkPayload, error) {
    64  	title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
    65  
    66  	return newWechatworkMarkdownPayload(title), nil
    67  }
    68  
    69  // Push implements PayloadConvertor Push method
    70  func (wc wechatworkConvertor) Push(p *api.PushPayload) (WechatworkPayload, error) {
    71  	var (
    72  		branchName = git.RefName(p.Ref).ShortName()
    73  		commitDesc string
    74  	)
    75  
    76  	title := fmt.Sprintf("# %s:%s <font color=\"warning\">  %s  </font>", p.Repo.FullName, branchName, commitDesc)
    77  
    78  	var text string
    79  	// for each commit, generate attachment text
    80  	for i, commit := range p.Commits {
    81  		var authorName string
    82  		if commit.Author != nil {
    83  			authorName = "Author: " + commit.Author.Name
    84  		}
    85  
    86  		message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")
    87  		text += fmt.Sprintf(" > [%s](%s) \r\n ><font color=\"info\">%s</font> \n ><font color=\"warning\">%s</font>", commit.ID[:7], commit.URL,
    88  			message, authorName)
    89  
    90  		// add linebreak to each commit but the last
    91  		if i < len(p.Commits)-1 {
    92  			text += "\n"
    93  		}
    94  	}
    95  	return newWechatworkMarkdownPayload(title + "\r\n\r\n" + text), nil
    96  }
    97  
    98  // Issue implements PayloadConvertor Issue method
    99  func (wc wechatworkConvertor) Issue(p *api.IssuePayload) (WechatworkPayload, error) {
   100  	text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
   101  	var content string
   102  	content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\"> %s</font> \n [%s](%s)", text, attachmentText, issueTitle, p.Issue.HTMLURL, p.Issue.HTMLURL)
   103  
   104  	return newWechatworkMarkdownPayload(content), nil
   105  }
   106  
   107  // IssueComment implements PayloadConvertor IssueComment method
   108  func (wc wechatworkConvertor) IssueComment(p *api.IssueCommentPayload) (WechatworkPayload, error) {
   109  	text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
   110  	var content string
   111  	content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\">%s</font> \n [%s](%s)", text, p.Comment.Body, issueTitle, p.Comment.HTMLURL, p.Comment.HTMLURL)
   112  
   113  	return newWechatworkMarkdownPayload(content), nil
   114  }
   115  
   116  // PullRequest implements PayloadConvertor PullRequest method
   117  func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (WechatworkPayload, error) {
   118  	text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
   119  	pr := fmt.Sprintf("> <font color=\"info\"> %s </font> \r\n > <font color=\"comment\">%s </font> \r\n > <font color=\"comment\">%s </font> \r\n",
   120  		text, issueTitle, attachmentText)
   121  
   122  	return newWechatworkMarkdownPayload(pr), nil
   123  }
   124  
   125  // Review implements PayloadConvertor Review method
   126  func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
   127  	var text, title string
   128  	switch p.Action {
   129  	case api.HookIssueReviewed:
   130  		action, err := parseHookPullRequestEventType(event)
   131  		if err != nil {
   132  			return WechatworkPayload{}, err
   133  		}
   134  		title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
   135  		text = p.Review.Content
   136  	}
   137  
   138  	return newWechatworkMarkdownPayload("# " + title + "\r\n\r\n >" + text), nil
   139  }
   140  
   141  // Repository implements PayloadConvertor Repository method
   142  func (wc wechatworkConvertor) Repository(p *api.RepositoryPayload) (WechatworkPayload, error) {
   143  	var title string
   144  	switch p.Action {
   145  	case api.HookRepoCreated:
   146  		title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
   147  		return newWechatworkMarkdownPayload(title), nil
   148  	case api.HookRepoDeleted:
   149  		title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
   150  		return newWechatworkMarkdownPayload(title), nil
   151  	}
   152  
   153  	return WechatworkPayload{}, nil
   154  }
   155  
   156  // Wiki implements PayloadConvertor Wiki method
   157  func (wc wechatworkConvertor) Wiki(p *api.WikiPayload) (WechatworkPayload, error) {
   158  	text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
   159  
   160  	return newWechatworkMarkdownPayload(text), nil
   161  }
   162  
   163  // Release implements PayloadConvertor Release method
   164  func (wc wechatworkConvertor) Release(p *api.ReleasePayload) (WechatworkPayload, error) {
   165  	text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
   166  
   167  	return newWechatworkMarkdownPayload(text), nil
   168  }
   169  
   170  func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, error) {
   171  	text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
   172  
   173  	return newWechatworkMarkdownPayload(text), nil
   174  }
   175  
   176  type wechatworkConvertor struct{}
   177  
   178  var _ payloadConvertor[WechatworkPayload] = wechatworkConvertor{}
   179  
   180  func newWechatworkRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
   181  	return newJSONRequest(wechatworkConvertor{}, w, t, true)
   182  }