|
| 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