|
| 1 | +package vsts |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +func getThreadsURL(pullRequestID int) string { |
| 10 | + threadsURLTemplate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads?api-version={version}" |
| 11 | + |
| 12 | + r := strings.NewReplacer( |
| 13 | + "{instance}", config.Instance, |
| 14 | + "{project}", config.Project, |
| 15 | + "{repository}", config.Repo, |
| 16 | + "{pullRequest}", strconv.Itoa(pullRequestID), |
| 17 | + "{version}", "3.0-preview") |
| 18 | + |
| 19 | + return r.Replace(threadsURLTemplate) |
| 20 | +} |
| 21 | + |
| 22 | +func getThreadURL(pullRequestID int, threadID int) string { |
| 23 | + threadURLTempate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads/{threadID}?api-version={version}" |
| 24 | + r := strings.NewReplacer( |
| 25 | + "{instance}", config.Instance, |
| 26 | + "{project}", config.Project, |
| 27 | + "{repository}", config.Repo, |
| 28 | + "{pullRequest}", strconv.Itoa(pullRequestID), |
| 29 | + "{threadID}", strconv.Itoa(threadID), |
| 30 | + "{version}", "3.0-preview") |
| 31 | + |
| 32 | + return r.Replace(threadURLTempate) |
| 33 | +} |
| 34 | + |
| 35 | +func getCommentURL(pullRequestID int, threadID int) string { |
| 36 | + commentURLTempate := "https://{instance}/DefaultCollection/{project}/_apis/git/repositories/{repository}/pullRequests/{pullRequest}/threads/{threadID}/comments?api-version={version}" |
| 37 | + r := strings.NewReplacer( |
| 38 | + "{instance}", config.Instance, |
| 39 | + "{project}", config.Project, |
| 40 | + "{repository}", config.Repo, |
| 41 | + "{pullRequest}", strconv.Itoa(pullRequestID), |
| 42 | + "{threadID}", strconv.Itoa(threadID), |
| 43 | + "{version}", "3.0-preview") |
| 44 | + |
| 45 | + return r.Replace(commentURLTempate) |
| 46 | +} |
| 47 | + |
| 48 | +func getCommentThreads(pullRequestID int) (*commentThreads, error) { |
| 49 | + commentThreads := new(commentThreads) |
| 50 | + |
| 51 | + url := getThreadsURL(pullRequestID) |
| 52 | + err := getFromVsts(url, commentThreads) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + return commentThreads, nil |
| 59 | +} |
| 60 | + |
| 61 | +func createCommentThread(pullRequestID int, filePath string, status int, content string) error { |
| 62 | + log.Printf("Creating comment thread to PR %v...\n", pullRequestID) |
| 63 | + |
| 64 | + thread := postThread{ |
| 65 | + Comments: []postComment{ |
| 66 | + { |
| 67 | + ParentCommentID: 0, |
| 68 | + Content: content, |
| 69 | + CommentType: 1, |
| 70 | + }, |
| 71 | + }, |
| 72 | + Properties: threadProperty{ |
| 73 | + MicrosoftTeamFoundationDiscussionSupportsMarkdown: supportsMarkDown{ |
| 74 | + Type: "System.Int32", |
| 75 | + Value: 1, |
| 76 | + }, |
| 77 | + }, |
| 78 | + Status: status, |
| 79 | + ThreadContext: threadContext{ |
| 80 | + FilePath: filePath, |
| 81 | + RightFileStart: filePosition{ |
| 82 | + Line: 1, |
| 83 | + Offset: 1, |
| 84 | + }, |
| 85 | + RightFileEnd: filePosition{ |
| 86 | + Line: 1, |
| 87 | + Offset: 3, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + url := getThreadsURL(pullRequestID) |
| 93 | + |
| 94 | + err := postToVsts(url, thread) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func addComment(pullRequestID int, thread commentThread, essentialMessage string, content string) error { |
| 103 | + lastCommentID := 0 |
| 104 | + commentContent := "" |
| 105 | + for _, comment := range thread.Comments { |
| 106 | + if !comment.IsDeleted && comment.ID > lastCommentID { |
| 107 | + lastCommentID = comment.ID |
| 108 | + commentContent = comment.Content |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if strings.Contains(commentContent, essentialMessage) { |
| 113 | + log.Printf("Already commented to PR %v thread %v...\n", pullRequestID, thread.ID) |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + log.Printf("Adding comment to PR %v thread %v...\n", pullRequestID, thread.ID) |
| 118 | + |
| 119 | + comment := postComment{ |
| 120 | + ParentCommentID: lastCommentID, |
| 121 | + Content: content, |
| 122 | + CommentType: 1, |
| 123 | + } |
| 124 | + |
| 125 | + url := getCommentURL(pullRequestID, thread.ID) |
| 126 | + |
| 127 | + err := postToVsts(url, comment) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func setCommentThreadStatus(pullRequestID int, thread commentThread, status int) error { |
| 136 | + statusString := "active" |
| 137 | + if status == 2 { |
| 138 | + statusString = "fixed" |
| 139 | + } |
| 140 | + if strings.EqualFold(thread.Status, statusString) { |
| 141 | + log.Printf("PR %v thread %v status is already %v\n", pullRequestID, thread.ID, status) |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + log.Printf("Set PR %v thread %v to %v...\n", pullRequestID, thread.ID, status) |
| 146 | + |
| 147 | + patchThread := patchThread{ |
| 148 | + Status: status, |
| 149 | + } |
| 150 | + |
| 151 | + url := getThreadURL(pullRequestID, thread.ID) |
| 152 | + |
| 153 | + err := patchToVsts(url, patchThread) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
0 commit comments