Skip to content

Commit 8def936

Browse files
authored
Merge pull request #741 from zampierilucas/review_with_quick_actions
Review with quick actions
2 parents 2fb6734 + 8bbc211 commit 8def936

3 files changed

Lines changed: 53 additions & 35 deletions

File tree

cmd/mr_approve.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package cmd
33
import (
44
"fmt"
55
"github.com/MakeNowJust/heredoc/v2"
6-
"os"
7-
86
"github.com/rsteube/carapace"
97
"github.com/spf13/cobra"
108
"github.com/zaquestion/lab/internal/action"
119
lab "github.com/zaquestion/lab/internal/gitlab"
10+
"os"
1211
)
1312

1413
var mrApproveCmd = &cobra.Command{
@@ -28,11 +27,18 @@ var mrApproveCmd = &cobra.Command{
2827
log.Fatal(err)
2928
}
3029

31-
p, err := lab.FindProject(rn)
30+
approvalConfig, err := lab.GetMRApprovalsConfiguration(rn, int(id))
3231
if err != nil {
3332
log.Fatal(err)
3433
}
3534

35+
for _, approvers := range approvalConfig.ApprovedBy {
36+
if approvers.User.Username == lab.User() {
37+
fmt.Printf("Merge Request !%d already approved\n", id)
38+
os.Exit(1)
39+
}
40+
}
41+
3642
comment, err := cmd.Flags().GetBool("with-comment")
3743
if err != nil {
3844
log.Fatal(err)
@@ -48,26 +54,23 @@ var mrApproveCmd = &cobra.Command{
4854
log.Fatal(err)
4955
}
5056

51-
if comment || len(msgs) > 0 || filename != "" {
52-
linebreak, err := cmd.Flags().GetBool("force-linebreak")
57+
note := comment || len(msgs) > 0 || filename != ""
58+
linebreak := false
59+
if note {
60+
linebreak, err = cmd.Flags().GetBool("force-linebreak")
5361
if err != nil {
5462
log.Fatal(err)
5563
}
56-
57-
createNote(rn, true, int(id), msgs, filename, linebreak, "")
58-
}
59-
60-
err = lab.MRApprove(p.ID, int(id))
61-
if err != nil {
62-
if err == lab.ErrStatusForbidden {
63-
log.Fatal(err)
64-
}
65-
if err == lab.ErrActionRepeated {
66-
fmt.Printf("Merge Request !%d already approved\n", id)
67-
os.Exit(1)
64+
if comment {
65+
state := noteGetState(rn, true, int(id))
66+
msg, _ := noteMsg(msgs, true, int(id), state, "", "")
67+
msgs = append(msgs, msg)
6868
}
6969
}
7070

71+
msgs = append(msgs, "/approve")
72+
createNote(rn, true, int(id), msgs, filename, linebreak, "", note)
73+
7174
fmt.Printf("Merge Request !%d approved\n", id)
7275
},
7376
}

cmd/mr_unapprove.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@ var mrUnapproveCmd = &cobra.Command{
2828
log.Fatal(err)
2929
}
3030

31-
p, err := lab.FindProject(rn)
31+
approvalConfig, err := lab.GetMRApprovalsConfiguration(rn, int(id))
3232
if err != nil {
3333
log.Fatal(err)
3434
}
3535

36+
canUnapprove := false
37+
for _, approvers := range approvalConfig.ApprovedBy {
38+
if approvers.User.Username == lab.User() {
39+
canUnapprove = true
40+
}
41+
}
42+
43+
if !canUnapprove {
44+
fmt.Printf("Merge Request !%d already unapproved\n", id)
45+
os.Exit(1)
46+
}
47+
3648
comment, err := cmd.Flags().GetBool("with-comment")
3749
if err != nil {
3850
log.Fatal(err)
@@ -48,25 +60,22 @@ var mrUnapproveCmd = &cobra.Command{
4860
log.Fatal(err)
4961
}
5062

51-
err = lab.MRUnapprove(p.ID, int(id))
52-
if err != nil {
53-
if err == lab.ErrStatusForbidden {
63+
note := comment || len(msgs) > 0 || filename != ""
64+
linebreak := false
65+
if note {
66+
linebreak, err = cmd.Flags().GetBool("force-linebreak")
67+
if err != nil {
5468
log.Fatal(err)
5569
}
56-
if err == lab.ErrActionRepeated {
57-
fmt.Printf("Merge Request !%d already unapproved\n", id)
58-
os.Exit(1)
70+
if comment {
71+
state := noteGetState(rn, true, int(id))
72+
msg, _ := noteMsg(msgs, true, int(id), state, "", "")
73+
msgs = append(msgs, msg)
5974
}
6075
}
6176

62-
if comment || len(msgs) > 0 || filename != "" {
63-
linebreak, err := cmd.Flags().GetBool("force-linebreak")
64-
if err != nil {
65-
log.Fatal(err)
66-
}
67-
68-
createNote(rn, true, int(id), msgs, filename, linebreak, "")
69-
}
77+
msgs = append(msgs, "/unapprove")
78+
createNote(rn, true, int(id), msgs, filename, linebreak, "", note)
7079

7180
fmt.Printf("Merge Request !%d unapproved\n", id)
7281
},

cmd/note_common.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func noteRunFn(cmd *cobra.Command, args []string) {
9191
return
9292
}
9393

94-
createNote(rn, isMR, int(idNum), msgs, filename, linebreak, commit)
94+
createNote(rn, isMR, int(idNum), msgs, filename, linebreak, commit, true)
9595
}
9696

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

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

237238
body := ""
@@ -241,6 +242,9 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
241242
log.Fatal(err)
242243
}
243244
body = string(content)
245+
if hasNote && len(msgs) > 0 {
246+
body += msgs[0]
247+
}
244248
} else {
245249
state := noteGetState(rn, isMR, idNum)
246250

@@ -283,7 +287,9 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
283287
if err != nil {
284288
log.Fatal(err)
285289
}
286-
fmt.Println(noteURL)
290+
if hasNote {
291+
fmt.Println(noteURL)
292+
}
287293
}
288294

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

0 commit comments

Comments
 (0)