/
client.go
162 lines (144 loc) · 4.33 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scmnotify
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/xanzy/go-gitlab"
"go.uber.org/zap"
"github.com/koderover/zadig/pkg/microservice/aslan/config"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/pkg/shared/codehost"
"github.com/koderover/zadig/pkg/tool/gerrit"
gitlabtool "github.com/koderover/zadig/pkg/tool/git/gitlab"
"github.com/koderover/zadig/pkg/tool/log"
)
type Client struct {
logger *zap.SugaredLogger
}
func NewClient() *Client {
return &Client{logger: log.SugaredLogger()}
}
// Comment send comment to gitlab and set comment id in notify
func (c *Client) Comment(notify *models.Notification) error {
if notify.PrID == 0 {
return fmt.Errorf("non pr notification not supported yet")
}
var err error
comment := notify.ErrInfo
if comment == "" {
if comment, err = notify.CreateCommentBody(); err != nil {
return fmt.Errorf("failed to create comment body %v", err)
}
}
codeHostDetail, err := codehost.GetCodeHostInfoByID(notify.CodehostID)
if err != nil {
return errors.Wrapf(err, "codehost %d not found to comment", notify.CodehostID)
}
if strings.ToLower(codeHostDetail.Type) == "gitlab" {
var note *gitlab.Note
cli, err := gitlabtool.NewClient(codeHostDetail.Address, codeHostDetail.AccessToken)
if err != nil {
c.logger.Errorf("create gitlab client failed err: %v", err)
return fmt.Errorf("create gitlab client failed err: %v", err)
}
if notify.CommentID == "" {
// create comment
note, _, err = cli.Notes.CreateMergeRequestNote(
notify.ProjectID, notify.PrID, &gitlab.CreateMergeRequestNoteOptions{
Body: &comment,
},
)
if err == nil {
notify.CommentID = strconv.Itoa(note.ID)
}
} else {
// update comment
noteID, _ := strconv.Atoi(notify.CommentID)
_, _, err = cli.Notes.UpdateMergeRequestNote(
notify.ProjectID, notify.PrID, noteID, &gitlab.UpdateMergeRequestNoteOptions{
Body: &comment,
})
}
if err != nil {
return fmt.Errorf("failed to comment gitlab due to %s/%d %v", notify.ProjectID, notify.PrID, err)
}
} else if strings.ToLower(codeHostDetail.Type) == gerrit.CodehostTypeGerrit {
cli := gerrit.NewClient(codeHostDetail.Address, codeHostDetail.AccessToken)
for _, task := range notify.Tasks {
// create task created comment
if !task.FirstCommented && task.Status == config.TaskStatusReady {
if e := cli.SetReview(
notify.ProjectID,
notify.PrID,
fmt.Sprintf(""+
"%s ⏱️ %s/v1/projects/detail/%s/pipelines/multi/%s/%d",
strings.ToUpper(string(task.Status)),
notify.BaseURI,
task.ProductName,
task.WorkflowName,
task.ID,
),
notify.Label,
"0",
notify.Revision,
); e != nil {
c.logger.Warnf("failed to set review %v %v %v", task, notify, e)
}
task.FirstCommented = true
continue
}
/* set review score*/
var emoji, score string
var skip bool
switch task.Status {
case config.TaskStatusPass:
emoji = "✅"
score = "+1"
case config.TaskStatusCancelled:
emoji = "✖️"
score = "0"
case config.TaskStatusTimeout, config.TaskStatusFailed:
emoji = "❌"
score = "-1"
default:
skip = true
}
if !skip {
if e := cli.SetReview(
notify.ProjectID,
notify.PrID,
fmt.Sprintf(""+
"%s %s %s/v1/projects/detail/%s/pipelines/multi/%s/%d",
strings.ToUpper(string(task.Status)),
emoji,
notify.BaseURI,
task.ProductName,
task.WorkflowName,
task.ID,
),
notify.Label,
score,
notify.Revision,
); e != nil {
c.logger.Warnf("failed to set review %v %v %v", task, notify, e)
}
}
}
} else {
return fmt.Errorf("non gitlab source not supported to comment")
}
return nil
}