4
4
"context"
5
5
"fmt"
6
6
"os"
7
- "strconv"
8
7
"strings"
9
8
"sync"
10
9
@@ -13,10 +12,9 @@ import (
13
12
)
14
13
15
14
var (
16
- commentClient * github.Client
17
- commentUser * github.User
18
- mux * sync.RWMutex
19
- commentLineMaxChar int
15
+ commentClient * github.Client
16
+ commentUser * github.User
17
+ mux * sync.RWMutex
20
18
)
21
19
22
20
const commentIdentifier = "<!-- comment produced by argo-diff -->"
@@ -29,17 +27,6 @@ func init() {
29
27
commentClient = github .NewClient (nil ).WithAuthToken (githubPAT )
30
28
}
31
29
mux = & sync.RWMutex {}
32
-
33
- commentLineMaxChar = 175
34
- lineMaxCharStr := os .Getenv ("COMMENT_LINE_MAX_CHARS" )
35
- if lineMaxCharStr != "" {
36
- v , err := strconv .Atoi (lineMaxCharStr )
37
- if err == nil {
38
- commentLineMaxChar = v
39
- } else {
40
- log .Warn ().Err (err ).Msg ("Failed to decode COMMENT_LINE_MAX_CHARS" )
41
- }
42
- }
43
30
}
44
31
45
32
// Populates commentUser singleton with the Github user associated with our github client
@@ -90,15 +77,16 @@ func getCommentUser(ctx context.Context) error {
90
77
// }
91
78
//}
92
79
93
- // Returns a pointer to a pull request comment previously generated by argo-diff.
94
- // Returns nil if there is no matching comment
95
- func getExistingComment (ctx context.Context , owner , repo string , prNum int ) (* github.IssueComment , error ) {
80
+ // Returns a list of pointers to pull request comments previously generated by argo-diff.
81
+ // Returns an empty list if there is no matching comment
82
+ func getExistingComments (ctx context.Context , owner , repo string , prNum int ) ([] * github.IssueComment , error ) {
96
83
sortOpt := "created"
97
84
sortDirection := "asc"
98
85
issueListCommentsOpts := github.IssueListCommentsOptions {
99
86
Sort : & sortOpt ,
100
87
Direction : & sortDirection ,
101
88
}
89
+ var res []* github.IssueComment
102
90
//var existingComment *github.IssueComment
103
91
if commentClient == nil {
104
92
log .Error ().Msg ("Cannot call github API - I don't have a client set" )
@@ -121,63 +109,72 @@ func getExistingComment(ctx context.Context, owner, repo string, prNum int) (*gi
121
109
log .Debug ().Msgf ("Checking %d comments in %s/%s#%d" , len (comments ), owner , repo , prNum )
122
110
for _ , c := range comments {
123
111
if * c .User .Login == * commentUser .Login && strings .Contains (* c .Body , commentIdentifier ) {
124
- return c , nil
112
+ res = append ( res , c )
125
113
}
126
114
}
127
115
if resp .NextPage > 0 {
128
116
issueListCommentsOpts .Page = resp .NextPage
129
117
checkComments = true
130
118
}
131
119
}
132
- return nil , nil
120
+ return res , nil
133
121
}
134
122
135
123
// Creates or updates comment on the specified pull request
136
- func Comment (ctx context.Context , owner , repo string , prNum int , commentBody string ) (int64 , error ) {
137
- existingComment , err := getExistingComment (ctx , owner , repo , prNum )
124
+ func Comment (ctx context.Context , owner , repo string , prNum int , commentBodies []string ) ([]* github.IssueComment , error ) {
125
+ var res []* github.IssueComment
126
+ existingComments , err := getExistingComments (ctx , owner , repo , prNum )
138
127
if err != nil {
139
- return - 1 , err
140
- }
141
- commentBody += "\n \n "
142
- commentBody += commentIdentifier
143
- commentBody += "\n "
144
- newComment := github.IssueComment {Body : truncateLines (commentBody , commentLineMaxChar )}
145
- var issueComment * github.IssueComment
146
- var resp * github.Response
147
- if existingComment != nil {
148
- issueComment , resp , err = commentClient .Issues .EditComment (ctx , owner , repo , * existingComment .ID , & newComment )
149
- } else {
150
- issueComment , resp , err = commentClient .Issues .CreateComment (ctx , owner , repo , prNum , & newComment )
151
- }
152
- if resp != nil {
153
- log .Info ().Msgf ("%s received from %s" , resp .Status , resp .Request .URL .String ())
154
- }
155
- if err != nil {
156
- if existingComment != nil {
157
- log .Error ().Err (err ).Msgf ("Failed to update comment %d for %s/%s#%d" , * existingComment .ID , owner , repo , prNum )
128
+ return res , err
129
+ }
130
+ nextExistingCommentIdx := 0
131
+ for i , commentBody := range commentBodies {
132
+ commentBody += "\n \n "
133
+ commentBody += commentIdentifier
134
+ commentBody += "\n "
135
+ newComment := github.IssueComment {Body : & commentBody }
136
+ var existingComment * github.IssueComment
137
+ var issueComment * github.IssueComment
138
+ var resp * github.Response
139
+ if i < len (existingComments ) {
140
+ nextExistingCommentIdx = i + 1
141
+ existingComment = existingComments [i ]
142
+ issueComment , resp , err = commentClient .Issues .EditComment (ctx , owner , repo , * existingComment .ID , & newComment )
158
143
} else {
159
- log . Error (). Err ( err ). Msgf ( "Failed to create comment for %s/%s#%d" , owner , repo , prNum )
144
+ issueComment , resp , err = commentClient . Issues . CreateComment ( ctx , owner , repo , prNum , & newComment )
160
145
}
161
- return - 1 , err
162
- }
163
- if issueComment == nil {
164
- log .Error ().Msg ("issueComment is nil? How did I get here?" )
165
- return - 1 , fmt .Errorf ("unknown error - issueComment is nil" )
166
- }
167
- log .Info ().Msgf ("Created or Updated comment ID %d in %s/%s#%d: %s" , * issueComment .ID , owner , repo , prNum , * issueComment .IssueURL )
168
- return * issueComment .ID , nil
169
- }
170
-
171
- func truncateLines (s string , maxLen int ) * string {
172
- var result string
173
- lines := strings .Split (s , "\n " )
174
- for _ , line := range lines {
175
- if len (line ) > maxLen {
176
- result += line [:maxLen ] + "...[TRUNCATED]"
146
+ if resp != nil {
147
+ log .Info ().Msgf ("%s received from %s" , resp .Status , resp .Request .URL .String ())
148
+ }
149
+ if err != nil {
150
+ if existingComment != nil {
151
+ log .Error ().Err (err ).Msgf ("Failed to update comment %d for %s/%s#%d" , * existingComment .ID , owner , repo , prNum )
152
+ } else {
153
+ log .Error ().Err (err ).Msgf ("Failed to create comment for %s/%s#%d" , owner , repo , prNum )
154
+ }
155
+ return res , err
156
+ }
157
+ if issueComment == nil {
158
+ log .Error ().Msg ("issueComment is nil? How did I get here?" )
159
+ return res , fmt .Errorf ("unknown error - issueComment is nil" )
160
+ }
161
+ log .Info ().Msgf ("Created or Updated comment ID %d in %s/%s#%d: %s" , * issueComment .ID , owner , repo , prNum , * issueComment .IssueURL )
162
+ res = append (res , issueComment )
163
+ }
164
+ for nextExistingCommentIdx < len (existingComments ) {
165
+ existingComment := existingComments [nextExistingCommentIdx ]
166
+ truncateCommentBody := "[Refreshed diff content is in above comments]\n \n " + commentIdentifier + "\n "
167
+ newComment := github.IssueComment {Body : & truncateCommentBody }
168
+ issueComment , resp , err := commentClient .Issues .EditComment (ctx , owner , repo , * existingComment .ID , & newComment )
169
+ if resp != nil {
170
+ log .Info ().Msgf ("%s received from %s" , resp .Status , resp .Request .URL .String ())
171
+ }
172
+ if err != nil {
173
+ log .Error ().Err (err ).Msgf ("Failed to update comment %d for %s/%s#%d" , * existingComment .ID , owner , repo , prNum )
177
174
} else {
178
- result += line
175
+ res = append ( res , issueComment )
179
176
}
180
- result += " \n "
177
+ nextExistingCommentIdx ++
181
178
}
182
- return & result
179
+ return res , nil
183
180
}
0 commit comments