Skip to content

Commit

Permalink
lab: Add MR and Issue note delete functionality
Browse files Browse the repository at this point in the history
Add the ability to delete notes from issues and MRs with the --deletenote
option.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Feb 22, 2022
1 parent 2ba2e55 commit bff0272
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 2 deletions.
33 changes: 32 additions & 1 deletion cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var issueEditCmd = &cobra.Command{
lab issue edit 14 -m "new title" -m "new desc"
lab issue edit 14 -l new_label --unlabel old_label
lab issue edit --milestone "NewYear"
lab issue edit --force-linebreak`),
lab issue edit --force-linebreak
lab issue edit --delete-note 14:2065489`),
Args: cobra.MinimumNArgs(1),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -54,6 +55,35 @@ var issueEditCmd = &cobra.Command{
log.Fatal(err)
}

deleteNote, err := cmd.Flags().GetBool("delete-note")
if err != nil {
log.Fatal(err)
}
if deleteNote {
discussions, err := lab.IssueListDiscussions(rn, int(issueNum))
if err != nil {
log.Fatal(err)
}

discussion := ""
findDiscussionID:
for _, d := range discussions {
for _, n := range d.Notes {
if n.ID == commentNum {
discussion = d.ID
break findDiscussionID
}
}
}

// delete the note
err = lab.IssueDeleteNote(rn, issueNum, discussion, commentNum)
if err != nil {
log.Fatal(err)
}
return
}

linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -217,6 +247,7 @@ func init() {
issueEditCmd.Flags().StringSliceP("unassign", "", []string{}, "remove an assignee by username")
issueEditCmd.Flags().String("milestone", "", "set milestone")
issueEditCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
issueEditCmd.Flags().Bool("delete-note", false, "delete the given note; must be provided in <issueID>:<noteID> format")
issueEditCmd.Flags().SortFlags = false

issueCmd.AddCommand(issueEditCmd)
Expand Down
92 changes: 92 additions & 0 deletions cmd/issue_edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,95 @@ func Test_issueEditAssignees(t *testing.T) {
// the output should NOT show the assignee
require.NotContains(t, issueShowOuput, "Assignees: lab-testing")
}

func Test_issueNoteDelete(t *testing.T) {
repo := copyTestRepo(t)

issueNum := issueEditCmdTestCreateIssue(t, repo)

// add just a note "DELETED 1"
cmd := exec.Command(labBinaryPath, "issue", "note", "lab-testing", issueNum, "-m", "DELETED 1")
cmd.Dir = repo

weburl, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs := strings.Split(string(weburl), "\n")
noteID := strings.Split(noteIDs[0], "#note_")[1]
deletedNote := issueNum + ":" + noteID

// add another note "REPLY 2"
cmd = exec.Command(labBinaryPath, "issue", "note", "lab-testing", issueNum, "-m", "REPLY 2")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs = strings.Split(string(weburl), "\n")
noteID = strings.Split(noteIDs[0], "#note_")[1]
replyNote := issueNum + ":" + noteID

// reply to the "REPLY 2" comment with a note to create a discussion "DELETED 2"
cmd = exec.Command(labBinaryPath, "issue", "reply", "lab-testing", replyNote, "-m", "DELETED 2")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs = strings.Split(string(weburl), "\n")
noteID = strings.Split(noteIDs[0], "#note_")[1]
deletedDiscussion := issueNum + ":" + noteID

// reply to the comment with a second comment "DISCUSSION 1"
cmd = exec.Command(labBinaryPath, "issue", "reply", "lab-testing", replyNote, "-m", "DISCUSSION 1")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// delete the first note
cmd = exec.Command(labBinaryPath, "issue", "edit", "lab-testing", deletedNote, "--delete-note")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// delete the first discussion reply
cmd = exec.Command(labBinaryPath, "issue", "edit", "lab-testing", deletedDiscussion, "--delete-note")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// show the updated issue and comments
cmd = exec.Command(labBinaryPath, "issue", "show", "lab-testing", issueNum, "--comments")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
issueShowOutput := string(b)

// lab show should not contain "DELETED" notes
require.NotContains(t, issueShowOutput, "DELETED 1")
require.NotContains(t, issueShowOutput, "DELETED 2")
// lab show should contain the other notes and disucssion
require.Contains(t, issueShowOutput, "REPLY 2")
require.Contains(t, issueShowOutput, "DISCUSSION 1")
}
34 changes: 33 additions & 1 deletion cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var mrEditCmd = &cobra.Command{
Short: "Edit or update an MR",
Example: heredoc.Doc(`
lab mr edit 2
lab mr edit 2:5684032
lab mr edit 3 remote -m "new title"
lab mr edit 5 upstream -m "new title" -m "new desc"
lab mr edit 7 -l new_label --unlabel old_label
Expand All @@ -33,7 +34,8 @@ var mrEditCmd = &cobra.Command{
lab mr edit 31 upstream --draft
lab mr edit 37 upstream --ready
lab mr edit 41 upstream -r johndoe -r janedoe
lab mr edit 43 upstream --unreview johndoe`),
lab mr edit 43 upstream --unreview johndoe
lab mr edit --delete-note 2:5684032`),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
commentNum, branchArgs, err := filterCommentArg(args)
Expand All @@ -57,6 +59,35 @@ var mrEditCmd = &cobra.Command{
log.Fatal(err)
}

deleteNote, err := cmd.Flags().GetBool("delete-note")
if err != nil {
log.Fatal(err)
}
if deleteNote {
discussions, err := lab.MRListDiscussions(rn, int(mrNum))
if err != nil {
log.Fatal(err)
}

discussion := ""
findDiscussionID:
for _, d := range discussions {
for _, n := range d.Notes {
if n.ID == commentNum {
discussion = d.ID
break findDiscussionID
}
}
}

// delete the note
err = lab.MRDeleteNote(rn, mrNum, discussion, commentNum)
if err != nil {
log.Fatal(err)
}
return
}

linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -378,6 +409,7 @@ func init() {
mrEditCmd.Flags().Bool("ready", false, "mark the merge request as ready")
mrEditCmd.Flags().StringSliceP("review", "r", []string{}, "add an reviewer by username")
mrEditCmd.Flags().StringSliceP("unreview", "", []string{}, "remove an reviewer by username")
mrEditCmd.Flags().Bool("delete-note", false, "delete the given note; must be provided in <mrID>:<noteID> format")
mrEditCmd.Flags().SortFlags = false

mrCmd.AddCommand(mrEditCmd)
Expand Down
131 changes: 131 additions & 0 deletions cmd/mr_edit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cmd

import (
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func MREditCmdTestCreateMR(t *testing.T, dir string) string {
git := exec.Command("git", "checkout", "-b", "local/mrtest", "origin/mrtest")
git.Dir = dir
g, err := git.CombinedOutput()
if err != nil {
t.Log(string(g))
t.Fatal(err)
}

cmd := exec.Command(labBinaryPath, "mr", "create", "lab-testing", "-m", "mr title", "-m", "mr description")
cmd.Dir = dir

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}

i := strings.Index(string(b), "/diffs\n")
mrID := strings.TrimPrefix(string(b)[:i], "https://gitlab.com/lab-testing/test/-/merge_requests/")
return mrID
}

func Test_MRNoteDelete(t *testing.T) {
repo := copyTestRepo(t)

mrNum := MREditCmdTestCreateMR(t, repo)

// add just a note "DELETED 1"
cmd := exec.Command(labBinaryPath, "mr", "note", "lab-testing", mrNum, "-m", "DELETED 1")
cmd.Dir = repo

weburl, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs := strings.Split(string(weburl), "\n")
noteID := strings.Split(noteIDs[0], "#note_")[1]
deletedNote := mrNum + ":" + noteID

// add another note "REPLY 2"
cmd = exec.Command(labBinaryPath, "mr", "note", "lab-testing", mrNum, "-m", "REPLY 2")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs = strings.Split(string(weburl), "\n")
noteID = strings.Split(noteIDs[0], "#note_")[1]
replyNote := mrNum + ":" + noteID

// reply to the "REPLY 2" comment with a note to create a discussion "DELETED 2"
cmd = exec.Command(labBinaryPath, "mr", "reply", "lab-testing", replyNote, "-m", "DELETED 2")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}
noteIDs = strings.Split(string(weburl), "\n")
noteID = strings.Split(noteIDs[0], "#note_")[1]
deletedDiscussion := mrNum + ":" + noteID

// reply to the comment with a second comment "DISCUSSION 1"
cmd = exec.Command(labBinaryPath, "mr", "reply", "lab-testing", replyNote, "-m", "DISCUSSION 1")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// delete the first note
cmd = exec.Command(labBinaryPath, "mr", "edit", "lab-testing", deletedNote, "--delete-note")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// delete the first discussion reply
cmd = exec.Command(labBinaryPath, "mr", "edit", "lab-testing", deletedDiscussion, "--delete-note")
cmd.Dir = repo

weburl, err = cmd.CombinedOutput()
if err != nil {
t.Log(string(weburl))
t.Fatal(err)
}

// show the updated mr and comments
cmd = exec.Command(labBinaryPath, "mr", "show", "lab-testing", mrNum, "--comments")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
mrShowOutput := string(b)

git := exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrNum)
git.Dir = repo
g, err := git.CombinedOutput()
if err != nil {
t.Log(string(g))
t.Fatal(err)
}

// lab show should not contain "DELETED" notes
require.NotContains(t, mrShowOutput, "DELETED 1")
require.NotContains(t, mrShowOutput, "DELETED 2")
// lab show should contain the other notes and disucssion
require.Contains(t, mrShowOutput, "REPLY 2")
require.Contains(t, mrShowOutput, "DISCUSSION 1")
}
34 changes: 34 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,40 @@ func GetCommitDiff(projID interface{}, sha string) ([]*gitlab.Diff, error) {
return diffs, nil
}

func IssueDeleteNote(projID interface{}, issue int, discussion string, note int) error {

if discussion == "" {
_, err := lab.Notes.DeleteIssueNote(projID, issue, note)
if err != nil {
return err
}
return nil
}

_, err := lab.Discussions.DeleteIssueDiscussionNote(projID, issue, discussion, note)
if err != nil {
return err
}
return nil
}

func MRDeleteNote(projID interface{}, mr int, discussion string, note int) error {

if discussion == "" {
_, err := lab.Notes.DeleteMergeRequestNote(projID, mr, note)
if err != nil {
return err
}
return nil
}

_, err := lab.Discussions.DeleteMergeRequestDiscussionNote(projID, mr, discussion, note)
if err != nil {
return err
}
return nil
}

func CreateCommitComment(projID interface{}, sha string, newFile string, oldFile string, line int, linetype string, comment string) (string, error) {
// Ideally want to use lab.Commits.PostCommitComment, however,
// that API only support comments on linetype=new.
Expand Down

0 comments on commit bff0272

Please sign in to comment.