From f9143e6cd43d553624b97ccf8f29bceaec7fcd1c Mon Sep 17 00:00:00 2001 From: Clement Li Date: Tue, 2 Nov 2021 20:20:33 +0800 Subject: [PATCH] fix input json struct --- src/gitee-utils/client.go | 55 +++++++++++++++++++++----------------- src/gitee-utils/message.go | 14 +++++++--- src/server.go | 19 ++++++++++++- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/gitee-utils/client.go b/src/gitee-utils/client.go index 768d770..f229ccc 100644 --- a/src/gitee-utils/client.go +++ b/src/gitee-utils/client.go @@ -23,23 +23,30 @@ type client struct { userData *sdk.User } +type RepoInfo struct { + Org string `json:"org"` + Repo string `json:"repo"` +} + type TargetInfo struct { - TargetUser string `json:"targetUser"` - InfoType string `json:"infoType"` + TargetUser []string `json:"targetUser"` + InfoType string `json:"infoType"` + InfoContent InfoContent `json:"infoContent"` } -type Issue struct { - IssueID string `json:"issueID"` - EventType string `json:"eventType"` - TargetInfo TargetInfo `json:"targetInfo"` - TargetLabel string `json:"targetLabel"` - TargetAssigneeID string `json:"targetAssigneeID"` - PushTime string `json:"pushTime"` +type InfoContent struct { + ChineseContent string `json:"Chinese"` + EnglishContent string `json:"English"` + GeneralContent string `json:"general_content"` } -type RepoInfo struct { - Org string `json:"org"` - Repo string `json:"repo"` +type Issue struct { + IssueID string `json:"issueID"` + EventType string `json:"eventType"` + TargetInfo TargetInfo `json:"targetInfo"` + TargetLabel string `json:"targetLabel"` + TargetAssigneeID string `json:"targetAssigneeID"` + PushTime string `json:"pushTime"` } func NewClient(getToken func() []byte) Client { @@ -291,7 +298,7 @@ func (c *client) GetPRCommits(org, repo string, number int) ([]sdk.PullRequestCo } func (c *client) AssignGiteeIssue(org, repo, labels string, number string, login string) error { - var labelStr []string + var labelStr []string labelsToAddStr := "" ll, _, errll := c.ac.LabelsApi.GetV5ReposOwnerRepoLabels(context.Background(), org, repo, nil) @@ -314,12 +321,12 @@ func (c *client) AssignGiteeIssue(org, repo, labels string, number string, login return formatErr(nil, "assign assignee to issue") } - labelsToAddStr = strings.Join(labelStr,",") + labelsToAddStr = strings.Join(labelStr, ",") opt := sdk.IssueUpdateParam{ Repo: repo, Assignee: login, - Labels: labelsToAddStr, + Labels: labelsToAddStr, } _, v, err := c.ac.IssuesApi.PatchV5ReposOwnerIssuesNumber( @@ -430,17 +437,17 @@ func (c *client) AddIssueAssignee(org, repo, number, token, assignee string) err return formatErr(err, "issue update") } -func (c *client) GetUserOrg(login string) ([]sdk.Group ,error) { +func (c *client) GetUserOrg(login string) ([]sdk.Group, error) { group, _, err := c.ac.OrganizationsApi.GetV5UsersUsernameOrgs(context.Background(), login, nil) return group, formatErr(err, "get org") } -func (c *client) GetUserEnt(ent, login string) (sdk.EnterpriseMember ,error) { - member, _, err := c.ac.EnterprisesApi.GetV5EnterprisesEnterpriseMembersUsername(context.Background(), ent, login,nil) +func (c *client) GetUserEnt(ent, login string) (sdk.EnterpriseMember, error) { + member, _, err := c.ac.EnterprisesApi.GetV5EnterprisesEnterpriseMembersUsername(context.Background(), ent, login, nil) return member, formatErr(err, "get ent") } -func (c *client) ListIssues(owner, repo, state, since, createAt string, page, perPage int) ([]sdk.Issue, *http.Response ,error) { +func (c *client) ListIssues(owner, repo, state, since, createAt string, page, perPage int) ([]sdk.Issue, *http.Response, error) { oState := optional.NewString(state) oPage := optional.NewInt32(int32(page)) oPerPage := optional.NewInt32(int32(perPage)) @@ -448,10 +455,10 @@ func (c *client) ListIssues(owner, repo, state, since, createAt string, page, pe oCreateAt := optional.NewString(createAt) issueParam := sdk.GetV5ReposOwnerRepoIssuesOpts{State: oState, Page: oPage, PerPage: oPerPage, Since: oSince, CreatedAt: oCreateAt} issues, res, err := c.ac.IssuesApi.GetV5ReposOwnerRepoIssues(context.Background(), owner, repo, &issueParam) - return issues, res,formatErr(err, "list issues") + return issues, res, formatErr(err, "list issues") } -func (c *client) ListLabels(owner, repo string) ([]sdk.Label ,error) { +func (c *client) ListLabels(owner, repo string) ([]sdk.Label, error) { labels, _, err := c.ac.LabelsApi.GetV5ReposOwnerRepoLabels(context.Background(), owner, repo, nil) return labels, formatErr(err, "list labels") } @@ -459,8 +466,8 @@ func (c *client) ListLabels(owner, repo string) ([]sdk.Label ,error) { func (c *client) GetRecommendation(labels string) (string, error) { // create path and map variables urlValues := url.Values{} - urlValues.Add("labels",labels) - resp, err := http.PostForm("http://34.92.52.47/predict",urlValues) + urlValues.Add("labels", labels) + resp, err := http.PostForm("http://34.92.52.47/predict", urlValues) if err != nil { // handle error formatErr(err, "request error") @@ -480,4 +487,4 @@ func formatErr(err error, doWhat string) error { return err } return fmt.Errorf("Failed to %s: %s", doWhat, err.Error()) -} \ No newline at end of file +} diff --git a/src/gitee-utils/message.go b/src/gitee-utils/message.go index 683ba32..1066c99 100644 --- a/src/gitee-utils/message.go +++ b/src/gitee-utils/message.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "os" + "strings" "time" amqp "github.com/rabbitmq/amqp091-go" @@ -249,11 +250,14 @@ func getToken() []byte { func eventHandler(msg amqp.Delivery) error { var repoinfo RepoInfo var msginfo Issue + lineBreaker := "\n" + log.Println(string(msg.Body)) err := json.Unmarshal(repo, &repoinfo) if err != nil { log.Println("wrong repo", err) return err } + err = json.Unmarshal(msg.Body, &msginfo) if err != nil { log.Println("wrong msg.body", err) @@ -263,16 +267,20 @@ func eventHandler(msg amqp.Delivery) error { repoInfo := repoinfo.Repo issueID := msginfo.IssueID eventType := msginfo.EventType - targetInfo := "请注意" - targetUser := msginfo.TargetInfo.TargetUser + generalContent := msginfo.TargetInfo.InfoContent.GeneralContent + chineseContent := msginfo.TargetInfo.InfoContent.ChineseContent + englishContent := msginfo.TargetInfo.InfoContent.EnglishContent infoType := msginfo.TargetInfo.InfoType + targetUser := msginfo.TargetInfo.TargetUser c := NewClient(getToken) switch eventType { case "info": switch infoType { case "issueComment": - strInfo := targetInfo + " @" + targetUser + " " + infoTemp := strings.Replace(generalContent, "{"+"mainCaller1"+"}", fmt.Sprintf("%v", targetUser[0]), -1) + infoTemp = strings.Replace(infoTemp, "{"+"mainCaller2"+"}", fmt.Sprintf("%v", targetUser[1]), -1) + strInfo := englishContent + lineBreaker + chineseContent + lineBreaker + infoTemp res := c.CreateGiteeIssueComment(orgInfo, repoInfo, issueID, strInfo) fmt.Println(strInfo) if res != nil { diff --git a/src/server.go b/src/server.go index f84f552..b0a75fe 100644 --- a/src/server.go +++ b/src/server.go @@ -20,7 +20,7 @@ func doRabbitMQ() { RMQ_EXCHANGE_NAME := os.Getenv("RMQ_EXCHANGE_NAME") RMQ_EXCHANGE_TYPE := os.Getenv("RMQ_EXCHANGE_TYPE") - // RabbitMQ + //RabbitMQ rc := gitee_utils.RabbitConfig{ Schema: "amqp", Username: RMQ_USER, @@ -30,12 +30,29 @@ func doRabbitMQ() { VHost: RMQ_VHOST, ConnectionName: "", } + // rc := gitee_utils.RabbitConfig{ + // Schema: "amqp", + // Username: "guest", + // Password: "guest", + // Host: "140.83.83.152", + // Port: "8090", + // VHost: "", + // ConnectionName: "", + // } rbt := gitee_utils.NewRabbit(rc) if err := rbt.Connect(); err != nil { log.Fatalln("unable to connect to rabbit", err) } // Consumer + // cc := gitee_utils.ConsumerConfig{ + // ExchangeName: "lz.direct", + // ExchangeType: "direct", + // RoutingKey: "lz.issue", + // QueueName: "lz.issue", + // ConsumerCount: 1, + // PrefetchCount: 1, + // } cc := gitee_utils.ConsumerConfig{ ExchangeName: RMQ_EXCHANGE_NAME, ExchangeType: RMQ_EXCHANGE_TYPE, -- Gitee