Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a reject subcommand #18

Merged
merged 2 commits into from
Jan 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var CommandMap = map[string]*Command{
"list": listCmd,
"pull": pullCmd,
"push": pushCmd,
"reject": rejectCmd,
"request": requestCmd,
"show": showCmd,
"submit": submitCmd,
Expand Down
120 changes: 120 additions & 0 deletions commands/reject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

import (
"errors"
"flag"
"fmt"
"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
"github.com/google/git-appraise/review/comment"
"io/ioutil"
"os"
"os/exec"
)

var rejectFlagSet = flag.NewFlagSet("reject", flag.ExitOnError)
var rejectFilename = "APPRAISE_COMMENT_EDITMSG"

var (
rejectMessage = rejectFlagSet.String("m", "", "Message to attach to the review")
)

// rejectReview adds an NMW comment to the current code review.
func rejectReview(repo repository.Repo, args []string) error {
rejectFlagSet.Parse(args)
args = rejectFlagSet.Args()

if *rejectMessage == "" {
editor, err := repo.GetCoreEditor()
if err != nil {
return fmt.Errorf("Unable to detect default git editor: %v\n", err)
}

path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), rejectFilename)

cmd := exec.Command(editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return fmt.Errorf("Unable to start editor: %v\n", err)
}

err = cmd.Wait()
if err != nil {
return fmt.Errorf("Editing finished with error: %v\n", err)
}

comment, err := ioutil.ReadFile(path)
if err != nil {
os.Remove(path)
return fmt.Errorf("Error reading comment file: %v\n", err)
}
*rejectMessage = string(comment)
os.Remove(path)
}

var r *review.Review
var err error
if len(args) > 1 {
return errors.New("Only rejecting a single review is supported.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this (and the other checks below) not be done before obtaining the reject message (which may possibly prompt the user to enter a message)?

Edited to avoid confusion

}

if len(args) == 1 {
r, err = review.Get(repo, args[0])
} else {
r, err = review.GetCurrent(repo)
}

if err != nil {
return fmt.Errorf("Failed to load the review: %v\n", err)
}
if r == nil {
return errors.New("There is no matching review.")
}

rejectedCommit, err := r.GetHeadCommit()
if err != nil {
return err
}
location := comment.Location{
Commit: rejectedCommit,
}
resolved := false
userEmail, err := repo.GetUserEmail()
if err != nil {
return err
}
c := comment.New(userEmail, *rejectMessage)
c.Location = &location
c.Resolved = &resolved
return r.AddComment(c)
}

// rejectCmd defines the "reject" subcommand.
var rejectCmd = &Command{
Usage: func(arg0 string) {
fmt.Printf("Usage: %s reject [<option>...] [<commit>]\n\nOptions:\n", arg0)
rejectFlagSet.PrintDefaults()
},
RunMethod: func(repo repository.Repo, args []string) error {
return rejectReview(repo, args)
},
}