Skip to content

Commit db6bfbf

Browse files
committed
Submitting review 2993185
Added a reject subcommand The reject subcommand adds a NMW comment to the review. Unlike the accept subcommand, this requires a comment. The -m flag can be passed to do this, or if no flag is passed it will open the default git text editor. This is what I have in mind in regards to #17
2 parents 27e8458 + 34bc911 commit db6bfbf

File tree

3 files changed

+151
-29
lines changed

3 files changed

+151
-29
lines changed

commands/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var CommandMap = map[string]*Command{
4444
"list": listCmd,
4545
"pull": pullCmd,
4646
"push": pushCmd,
47+
"reject": rejectCmd,
4748
"request": requestCmd,
4849
"show": showCmd,
4950
"submit": submitCmd,

commands/comment.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ func commentHashExists(hashToFind string, threads []review.CommentThread) bool {
5656
func commentOnReview(repo repository.Repo, args []string) error {
5757
commentFlagSet.Parse(args)
5858
args = commentFlagSet.Args()
59+
60+
var r *review.Review
61+
var err error
62+
if len(args) > 1 {
63+
return errors.New("Only accepting a single review is supported.")
64+
}
65+
66+
if len(args) == 1 {
67+
r, err = review.Get(repo, args[0])
68+
} else {
69+
r, err = review.GetCurrent(repo)
70+
}
71+
72+
if err != nil {
73+
return fmt.Errorf("Failed to load the review: %v\n", err)
74+
}
75+
if r == nil {
76+
return errors.New("There is no matching review.")
77+
}
78+
79+
if *commentLgtm && *commentNmw {
80+
return errors.New("You cannot combine the flags -lgtm and -nmw.")
81+
}
82+
if *commentLine != 0 && *commentFile == "" {
83+
return errors.New("Specifying a line number with the -l flag requires that you also specify a file name with the -f flag.")
84+
}
85+
if *commentParent != "" && !commentHashExists(*commentParent, r.Comments) {
86+
return errors.New("There is no matching parent comment.")
87+
}
88+
5989
if *commentMessage == "" {
6090
editor, err := repo.GetCoreEditor()
6191
if err != nil {
@@ -86,35 +116,6 @@ func commentOnReview(repo repository.Repo, args []string) error {
86116
*commentMessage = string(comment)
87117
os.Remove(path)
88118
}
89-
if *commentLgtm && *commentNmw {
90-
return errors.New("You cannot combine the flags -lgtm and -nmw.")
91-
}
92-
if *commentLine != 0 && *commentFile == "" {
93-
return errors.New("Specifying a line number with the -l flag requires that you also specify a file name with the -f flag.")
94-
}
95-
96-
var r *review.Review
97-
var err error
98-
if len(args) > 1 {
99-
return errors.New("Only accepting a single review is supported.")
100-
}
101-
102-
if len(args) == 1 {
103-
r, err = review.Get(repo, args[0])
104-
} else {
105-
r, err = review.GetCurrent(repo)
106-
}
107-
108-
if err != nil {
109-
return fmt.Errorf("Failed to load the review: %v\n", err)
110-
}
111-
if r == nil {
112-
return errors.New("There is no matching review.")
113-
}
114-
115-
if *commentParent != "" && !commentHashExists(*commentParent, r.Comments) {
116-
return errors.New("There is no matching parent comment.")
117-
}
118119

119120
commentedUponCommit, err := r.GetHeadCommit()
120121
if err != nil {

commands/reject.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright 2015 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package commands
18+
19+
import (
20+
"errors"
21+
"flag"
22+
"fmt"
23+
"github.com/google/git-appraise/repository"
24+
"github.com/google/git-appraise/review"
25+
"github.com/google/git-appraise/review/comment"
26+
"io/ioutil"
27+
"os"
28+
"os/exec"
29+
)
30+
31+
var rejectFlagSet = flag.NewFlagSet("reject", flag.ExitOnError)
32+
var rejectFilename = "APPRAISE_COMMENT_EDITMSG"
33+
34+
var (
35+
rejectMessage = rejectFlagSet.String("m", "", "Message to attach to the review")
36+
)
37+
38+
// rejectReview adds an NMW comment to the current code review.
39+
func rejectReview(repo repository.Repo, args []string) error {
40+
rejectFlagSet.Parse(args)
41+
args = rejectFlagSet.Args()
42+
43+
var r *review.Review
44+
var err error
45+
if len(args) > 1 {
46+
return errors.New("Only rejecting a single review is supported.")
47+
}
48+
49+
if len(args) == 1 {
50+
r, err = review.Get(repo, args[0])
51+
} else {
52+
r, err = review.GetCurrent(repo)
53+
}
54+
55+
if err != nil {
56+
return fmt.Errorf("Failed to load the review: %v\n", err)
57+
}
58+
if r == nil {
59+
return errors.New("There is no matching review.")
60+
}
61+
62+
if *rejectMessage == "" {
63+
editor, err := repo.GetCoreEditor()
64+
if err != nil {
65+
return fmt.Errorf("Unable to detect default git editor: %v\n", err)
66+
}
67+
68+
path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), rejectFilename)
69+
70+
cmd := exec.Command(editor, path)
71+
cmd.Stdin = os.Stdin
72+
cmd.Stdout = os.Stdout
73+
cmd.Stderr = os.Stderr
74+
err = cmd.Start()
75+
if err != nil {
76+
return fmt.Errorf("Unable to start editor: %v\n", err)
77+
}
78+
79+
err = cmd.Wait()
80+
if err != nil {
81+
return fmt.Errorf("Editing finished with error: %v\n", err)
82+
}
83+
84+
comment, err := ioutil.ReadFile(path)
85+
if err != nil {
86+
os.Remove(path)
87+
return fmt.Errorf("Error reading comment file: %v\n", err)
88+
}
89+
*rejectMessage = string(comment)
90+
os.Remove(path)
91+
}
92+
93+
rejectedCommit, err := r.GetHeadCommit()
94+
if err != nil {
95+
return err
96+
}
97+
location := comment.Location{
98+
Commit: rejectedCommit,
99+
}
100+
resolved := false
101+
userEmail, err := repo.GetUserEmail()
102+
if err != nil {
103+
return err
104+
}
105+
c := comment.New(userEmail, *rejectMessage)
106+
c.Location = &location
107+
c.Resolved = &resolved
108+
return r.AddComment(c)
109+
}
110+
111+
// rejectCmd defines the "reject" subcommand.
112+
var rejectCmd = &Command{
113+
Usage: func(arg0 string) {
114+
fmt.Printf("Usage: %s reject [<option>...] [<commit>]\n\nOptions:\n", arg0)
115+
rejectFlagSet.PrintDefaults()
116+
},
117+
RunMethod: func(repo repository.Repo, args []string) error {
118+
return rejectReview(repo, args)
119+
},
120+
}

0 commit comments

Comments
 (0)