Comments on deleted lines

This commit is contained in:
Harrison Cramer
2023-04-23 17:21:13 -04:00
parent b0cdc5dd88
commit 182575d95a
4 changed files with 157 additions and 50 deletions

View File

@@ -1,14 +1,17 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
@@ -16,6 +19,7 @@ import (
)
const mrVersionsUrl = "https://gitlab.com/api/v4/projects/%s/merge_requests/%d/versions"
const commentUrl = "https://gitlab.com/api/v4/projects/%s/repository/commits/%s/comments"
type MRVersion struct {
ID int `json:"id"`
@@ -29,11 +33,11 @@ type MRVersion struct {
}
func (c *Client) Comment() error {
if len(os.Args) < 6 {
if len(os.Args) < 7 {
c.Usage("comment")
}
lineNumber, fileName, comment := os.Args[3], os.Args[4], os.Args[5]
lineNumber, fileName, comment, sha := os.Args[3], os.Args[4], os.Args[5], os.Args[6]
if lineNumber == "" || fileName == "" || comment == "" {
c.Usage("comment")
}
@@ -53,67 +57,97 @@ func (c *Client) Comment() error {
var diffVersionInfo []MRVersion
err = json.Unmarshal(body, &diffVersionInfo)
if err != nil {
return fmt.Errorf("Error unmarshalling version info JSON: %w", err)
}
time := time.Now()
/* This is necessary since we do not know whether the comment is on a line that
has been changed or not. Making all three of these API calls will let us leave
the comment regardless. See the Gitlab documentation: https://docs.gitlab.com/ee/api/discussions.html#create-a-new-thread-in-the-merge-request-diff */
wg := sync.WaitGroup{}
wg.Add(3)
if sha == "" {
/* This is necessary since we do not know whether the comment is on a line that
has been changed or not. Making all three of these API calls will let us leave
the comment regardless. See the Gitlab documentation: https://docs.gitlab.com/ee/api/discussions.html#create-a-new-thread-in-the-merge-request-diff */
wg := sync.WaitGroup{}
wg.Add(2)
resultChannel := make(chan *gitlab.Discussion, 3)
resultChannel := make(chan *gitlab.Discussion, 2)
for i := 0; i < 3; i++ {
ii := i
go func() {
defer wg.Done()
options := gitlab.CreateMergeRequestDiscussionOptions{
Body: &comment,
CreatedAt: &time,
Position: &gitlab.NotePosition{
PositionType: "text",
BaseSHA: diffVersionInfo[0].BaseCommitSHA,
HeadSHA: diffVersionInfo[0].HeadCommitSHA,
StartSHA: diffVersionInfo[0].StartCommitSHA,
NewPath: fileName,
OldPath: fileName,
},
}
for i := 0; i < 2; i++ {
ii := i
go func() {
defer wg.Done()
options := gitlab.CreateMergeRequestDiscussionOptions{
Body: &comment,
CreatedAt: &time,
Position: &gitlab.NotePosition{
PositionType: "text",
BaseSHA: diffVersionInfo[0].BaseCommitSHA,
HeadSHA: diffVersionInfo[0].HeadCommitSHA,
StartSHA: diffVersionInfo[0].StartCommitSHA,
NewPath: fileName,
OldPath: fileName,
},
}
if ii == 0 {
options.Position.NewLine = lineNumberInt
} else if ii == 1 {
options.Position.OldLine = lineNumberInt
} else {
options.Position.NewLine = lineNumberInt
options.Position.OldLine = lineNumberInt
}
if ii == 0 {
options.Position.NewLine = lineNumberInt
} else {
options.Position.NewLine = lineNumberInt
options.Position.OldLine = lineNumberInt
}
discussion, _, _ := c.git.Discussions.CreateMergeRequestDiscussion(c.projectId, c.mergeId, &options)
resultChannel <- discussion
discussion, _, _ := c.git.Discussions.CreateMergeRequestDiscussion(c.projectId, c.mergeId, &options)
resultChannel <- discussion
}()
}
go func() {
wg.Wait()
close(resultChannel)
}()
var createdDiscussion *gitlab.Discussion
for discussion := range resultChannel {
if discussion != nil {
createdDiscussion = discussion
}()
}
}
if createdDiscussion == nil {
return fmt.Errorf("Could not leave comment")
go func() {
wg.Wait()
close(resultChannel)
}()
var createdDiscussion *gitlab.Discussion
for discussion := range resultChannel {
if discussion != nil {
createdDiscussion = discussion
}
}
if createdDiscussion == nil {
return fmt.Errorf("Could not leave comment")
}
} else {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("note", comment)
_ = writer.WriteField("path", "README.md")
_ = writer.WriteField("line", "3")
_ = writer.WriteField("line_type", "old")
err := writer.Close()
if err != nil {
return fmt.Errorf("Error making form data: %w", err)
}
url := fmt.Sprintf(commentUrl, c.projectId, strings.TrimSpace(sha))
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, url, payload)
if err != nil {
return fmt.Errorf("Error building request: %w", err)
}
req.Header.Add("PRIVATE-TOKEN", os.Getenv("GITLAB_TOKEN"))
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error making request: %w", err)
}
defer res.Body.Close()
}
fmt.Println("Left Comment: " + comment[0:min(len(comment), 25)] + "...")
return nil
}
@@ -151,3 +185,29 @@ func getMRVersions(projectId string, mergeId int) (e error, response *http.Respo
return nil, response
}
func (c *Client) OverviewComment() error {
lineNumber, fileName, comment, sha := os.Args[3], os.Args[4], os.Args[5], os.Args[6]
if lineNumber == "" || fileName == "" || comment == "" {
c.Usage("comment")
}
lineNumberInt, err := strconv.Atoi(lineNumber)
if err != nil {
return fmt.Errorf("Not a valid line number: %w", err)
}
postCommitCommentOptions := gitlab.PostCommitCommentOptions{
Note: gitlab.String(comment),
Path: gitlab.String(fileName),
Line: &lineNumberInt,
LineType: gitlab.String("old"),
}
_, _, err = c.git.Commits.PostCommitComment(c.projectId, sha, &postCommitCommentOptions)
if err != nil {
return fmt.Errorf("Error leaving overview comment: %w", err)
}
fmt.Println("Left Overview Comment: " + comment[0:min(len(comment), 25)] + "...")
return nil
}