-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.go
163 lines (132 loc) · 3.97 KB
/
comment.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
163
package vsts
import (
"log"
"strconv"
"strings"
)
func getThreadsURL(pullRequestID int) string {
threadsURLTemplate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads?api-version={version}"
r := strings.NewReplacer(
"{instance}", config.Instance,
"{project}", config.Project,
"{repository}", config.Repo,
"{pullRequest}", strconv.Itoa(pullRequestID),
"{version}", "3.0-preview")
return r.Replace(threadsURLTemplate)
}
func getThreadURL(pullRequestID int, threadID int) string {
threadURLTempate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads/{threadID}?api-version={version}"
r := strings.NewReplacer(
"{instance}", config.Instance,
"{project}", config.Project,
"{repository}", config.Repo,
"{pullRequest}", strconv.Itoa(pullRequestID),
"{threadID}", strconv.Itoa(threadID),
"{version}", "3.0-preview")
return r.Replace(threadURLTempate)
}
func getCommentURL(pullRequestID int, threadID int) string {
commentURLTempate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads/{threadID}/comments?api-version={version}"
r := strings.NewReplacer(
"{instance}", config.Instance,
"{project}", config.Project,
"{repository}", config.Repo,
"{pullRequest}", strconv.Itoa(pullRequestID),
"{threadID}", strconv.Itoa(threadID),
"{version}", "3.0-preview")
return r.Replace(commentURLTempate)
}
func getCommentThreads(pullRequestID int) (*commentThreads, error) {
commentThreads := new(commentThreads)
url := getThreadsURL(pullRequestID)
err := getFromVsts(url, commentThreads)
if err != nil {
return nil, err
}
return commentThreads, nil
}
func createCommentThread(pullRequestID int, filePath string, status int, content string) error {
log.Printf("Creating comment thread to PR %v...\n", pullRequestID)
thread := postThread{
Comments: []postComment{
{
ParentCommentID: 0,
Content: content,
CommentType: 1,
},
},
Properties: threadProperty{
MicrosoftTeamFoundationDiscussionSupportsMarkdown: supportsMarkDown{
Type: "System.Int32",
Value: 1,
},
},
Status: status,
ThreadContext: threadContext{
FilePath: filePath,
RightFileStart: filePosition{
Line: 1,
Offset: 1,
},
RightFileEnd: filePosition{
Line: 1,
Offset: 3,
},
},
}
if filePath == "" {
thread.ThreadContext = threadContext{}
}
url := getThreadsURL(pullRequestID)
err := postToVsts(url, thread)
if err != nil {
return err
}
return nil
}
func addComment(pullRequestID int, thread commentThread, essentialMessage string, content string) error {
lastCommentID := 0
commentContent := ""
for _, comment := range thread.Comments {
if !comment.IsDeleted && comment.ID > lastCommentID {
lastCommentID = comment.ID
commentContent = comment.Content
}
}
if strings.Contains(commentContent, essentialMessage) {
log.Printf("Already commented to PR %v thread %v...\n", pullRequestID, thread.ID)
return nil
}
log.Printf("Adding comment to PR %v thread %v...\n", pullRequestID, thread.ID)
comment := postComment{
ParentCommentID: lastCommentID,
Content: content,
CommentType: 1,
}
url := getCommentURL(pullRequestID, thread.ID)
err := postToVsts(url, comment)
if err != nil {
return err
}
return nil
}
func setCommentThreadStatus(pullRequestID int, thread commentThread, status int) error {
statusString := "active"
if status == 2 {
statusString = "fixed"
}
if strings.EqualFold(thread.Status, statusString) {
log.Printf("PR %v thread %v status is already %v\n", pullRequestID, thread.ID, status)
return nil
}
log.Printf("Set PR %v thread %v to %v...\n", pullRequestID, thread.ID, status)
patchThread := patchThread{
Status: status,
}
url := getThreadURL(pullRequestID, thread.ID)
err := patchToVsts(url, patchThread)
if err != nil {
return err
}
return nil
}