Skip to content

Commit

Permalink
Merge pull request #741 from zampierilucas/review_with_quick_actions
Browse files Browse the repository at this point in the history
Review with quick actions
  • Loading branch information
bmeneg authored Sep 30, 2021
2 parents 2fb6734 + 8bbc211 commit 8def936
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
37 changes: 20 additions & 17 deletions cmd/mr_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package cmd
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"os"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
"os"
)

var mrApproveCmd = &cobra.Command{
Expand All @@ -28,11 +27,18 @@ var mrApproveCmd = &cobra.Command{
log.Fatal(err)
}

p, err := lab.FindProject(rn)
approvalConfig, err := lab.GetMRApprovalsConfiguration(rn, int(id))
if err != nil {
log.Fatal(err)
}

for _, approvers := range approvalConfig.ApprovedBy {
if approvers.User.Username == lab.User() {
fmt.Printf("Merge Request !%d already approved\n", id)
os.Exit(1)
}
}

comment, err := cmd.Flags().GetBool("with-comment")
if err != nil {
log.Fatal(err)
Expand All @@ -48,26 +54,23 @@ var mrApproveCmd = &cobra.Command{
log.Fatal(err)
}

if comment || len(msgs) > 0 || filename != "" {
linebreak, err := cmd.Flags().GetBool("force-linebreak")
note := comment || len(msgs) > 0 || filename != ""
linebreak := false
if note {
linebreak, err = cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}

createNote(rn, true, int(id), msgs, filename, linebreak, "")
}

err = lab.MRApprove(p.ID, int(id))
if err != nil {
if err == lab.ErrStatusForbidden {
log.Fatal(err)
}
if err == lab.ErrActionRepeated {
fmt.Printf("Merge Request !%d already approved\n", id)
os.Exit(1)
if comment {
state := noteGetState(rn, true, int(id))
msg, _ := noteMsg(msgs, true, int(id), state, "", "")
msgs = append(msgs, msg)
}
}

msgs = append(msgs, "/approve")
createNote(rn, true, int(id), msgs, filename, linebreak, "", note)

fmt.Printf("Merge Request !%d approved\n", id)
},
}
Expand Down
39 changes: 24 additions & 15 deletions cmd/mr_unapprove.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ var mrUnapproveCmd = &cobra.Command{
log.Fatal(err)
}

p, err := lab.FindProject(rn)
approvalConfig, err := lab.GetMRApprovalsConfiguration(rn, int(id))
if err != nil {
log.Fatal(err)
}

canUnapprove := false
for _, approvers := range approvalConfig.ApprovedBy {
if approvers.User.Username == lab.User() {
canUnapprove = true
}
}

if !canUnapprove {
fmt.Printf("Merge Request !%d already unapproved\n", id)
os.Exit(1)
}

comment, err := cmd.Flags().GetBool("with-comment")
if err != nil {
log.Fatal(err)
Expand All @@ -48,25 +60,22 @@ var mrUnapproveCmd = &cobra.Command{
log.Fatal(err)
}

err = lab.MRUnapprove(p.ID, int(id))
if err != nil {
if err == lab.ErrStatusForbidden {
note := comment || len(msgs) > 0 || filename != ""
linebreak := false
if note {
linebreak, err = cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}
if err == lab.ErrActionRepeated {
fmt.Printf("Merge Request !%d already unapproved\n", id)
os.Exit(1)
if comment {
state := noteGetState(rn, true, int(id))
msg, _ := noteMsg(msgs, true, int(id), state, "", "")
msgs = append(msgs, msg)
}
}

if comment || len(msgs) > 0 || filename != "" {
linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}

createNote(rn, true, int(id), msgs, filename, linebreak, "")
}
msgs = append(msgs, "/unapprove")
createNote(rn, true, int(id), msgs, filename, linebreak, "", note)

fmt.Printf("Merge Request !%d unapproved\n", id)
},
Expand Down
12 changes: 9 additions & 3 deletions cmd/note_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func noteRunFn(cmd *cobra.Command, args []string) {
return
}

createNote(rn, isMR, int(idNum), msgs, filename, linebreak, commit)
createNote(rn, isMR, int(idNum), msgs, filename, linebreak, commit, true)
}

func createCommitNote(rn string, mrID int, sha string, newFile string, oldFile string, oldline int, newline int, comment string, block bool) {
Expand Down Expand Up @@ -231,7 +231,8 @@ func noteGetState(rn string, isMR bool, idNum int) (state string) {
return state
}

func createNote(rn string, isMR bool, idNum int, msgs []string, filename string, linebreak bool, commit string) {
func createNote(rn string, isMR bool, idNum int, msgs []string, filename string, linebreak bool, commit string, hasNote bool) {
// hasNote is used by action that take advantage of Gitlab 'quick-action' notes, which do not create a noteURL
var err error

body := ""
Expand All @@ -241,6 +242,9 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
log.Fatal(err)
}
body = string(content)
if hasNote && len(msgs) > 0 {
body += msgs[0]
}
} else {
state := noteGetState(rn, isMR, idNum)

Expand Down Expand Up @@ -283,7 +287,9 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
if err != nil {
log.Fatal(err)
}
fmt.Println(noteURL)
if hasNote {
fmt.Println(noteURL)
}
}

func noteMsg(msgs []string, isMR bool, idNum int, state string, commit string, body string) (string, error) {
Expand Down

0 comments on commit 8def936

Please sign in to comment.