Skip to content

Commit

Permalink
Added abandon command
Browse files Browse the repository at this point in the history
This commit resolve #17
  • Loading branch information
sokolovstas committed Jan 24, 2017
1 parent 4a3502c commit 460d35e
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 4 deletions.
120 changes: 120 additions & 0 deletions commands/abandon.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"
"log"

"github.com/google/git-appraise/commands/input"
"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
"github.com/google/git-appraise/review/comment"
"github.com/google/git-appraise/review/request"
)

var abandonFlagSet = flag.NewFlagSet("abandon", flag.ExitOnError)

var (
abandonMessageFile = abandonFlagSet.String("F", "", "Take the comment from the given file. Use - to read the message from the standard input")
abandonMessage = abandonFlagSet.String("m", "", "Message to attach to the review")
)

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

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

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.")
}

if *abandonMessageFile != "" && *abandonMessage == "" {
*abandonMessage, err = input.FromFile(*abandonMessageFile)
if err != nil {
return err
}
}
if *abandonMessageFile == "" && *abandonMessage == "" {
*abandonMessage, err = input.LaunchEditor(repo, commentFilename)
if err != nil {
return err
}
}

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

err = r.AddComment(c)

if err != nil {
return err
}

// Empty target ref indicates that request was abandoned
r.Request.TargetRef = ""

note, err := r.Request.Write()
if err != nil {
return err
}

log.Print(request.Ref, abandonedCommit, string(note))

return repo.EditNote(request.Ref, r.Revision, note)
}

// abandonCmd defines the "abandon" subcommand.
var abandonCmd = &Command{
Usage: func(arg0 string) {
fmt.Printf("Usage: %s abandon [<option>...] [<commit>]\n\nOptions:\n", arg0)
abandonFlagSet.PrintDefaults()
},
RunMethod: func(repo repository.Repo, args []string) error {
return abandonReview(repo, args)
},
}
1 change: 1 addition & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (cmd *Command) Run(repo repository.Repo, args []string) error {

// CommandMap defines all of the available (sub)commands.
var CommandMap = map[string]*Command{
"abandon": abandonCmd,
"accept": acceptCmd,
"comment": commentCmd,
"list": listCmd,
Expand Down
3 changes: 3 additions & 0 deletions commands/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func getStatusString(r *review.Summary) string {
if r.Submitted {
return "danger"
}
if r.Request.TargetRef == "" {
return "abandon"
}
return "rejected"
}

Expand Down
5 changes: 5 additions & 0 deletions commands/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"flag"
"fmt"

"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
)
Expand Down Expand Up @@ -56,6 +57,10 @@ func validateRebaseRequest(repo repository.Repo, args []string) (*review.Review,
return nil, errors.New("The review has already been submitted.")
}

if r.Request.TargetRef == "" {
return nil, errors.New("The review was abandoned.")
}

target := r.Request.TargetRef
if err := repo.VerifyGitRef(target); err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions commands/reject.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"flag"
"fmt"

"github.com/google/git-appraise/commands/input"
"github.com/google/git-appraise/repository"
"github.com/google/git-appraise/review"
Expand Down Expand Up @@ -57,6 +58,10 @@ func rejectReview(repo repository.Repo, args []string) error {
return errors.New("There is no matching review.")
}

if r.Request.TargetRef == "" {
return errors.New("The review was abandoned.")
}

if *rejectMessageFile != "" && *rejectMessage == "" {
*rejectMessage, err = input.FromFile(*rejectMessageFile)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions repository/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,12 @@ func (repo *GitRepo) GetAllNotes(notesRef string) (map[string][]Note, error) {
return commitNotesMap, nil
}

// EditNote edit a note under the given ref.
func (repo *GitRepo) EditNote(notesRef, revision string, note Note) error {
_, err := repo.runGitCommand("notes", "--ref", notesRef, "edit", "-m", string(note), revision)
return err
}

// AppendNote appends a note to a revision under the given ref.
func (repo *GitRepo) AppendNote(notesRef, revision string, note Note) error {
_, err := repo.runGitCommand("notes", "--ref", notesRef, "append", "-m", string(note), revision)
Expand Down
8 changes: 8 additions & 0 deletions repository/mock_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,14 @@ func (r *mockRepoForTest) GetAllNotes(notesRef string) (map[string][]Note, error
return notesMap, nil
}

// EditNote appends a note to a revision under the given ref.
func (r *mockRepoForTest) EditNote(ref, revision string, note Note) error {
existingNotes := r.Notes[ref][revision]
newNotes := existingNotes + "\n" + string(note)
r.Notes[ref][revision] = newNotes
return nil
}

// AppendNote appends a note to a revision under the given ref.
func (r *mockRepoForTest) AppendNote(ref, revision string, note Note) error {
existingNotes := r.Notes[ref][revision]
Expand Down
3 changes: 3 additions & 0 deletions repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ type Repo interface {
// This is the batch version of the corresponding GetNotes(...) method.
GetAllNotes(notesRef string) (map[string][]Note, error)

// EditNote edit a note to a revision under the given ref.
EditNote(ref, revision string, note Note) error

// AppendNote appends a note to a revision under the given ref.
AppendNote(ref, revision string, note Note) error

Expand Down
2 changes: 1 addition & 1 deletion review/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ParseAllValid(notes []repository.Note) []Request {
var requests []Request
for _, note := range notes {
request, err := Parse(note)
if err == nil && request.Version == FormatVersion && request.TargetRef != "" {
if err == nil && request.Version == FormatVersion {
requests = append(requests, request)
}
}
Expand Down
2 changes: 1 addition & 1 deletion review/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func ListAll(repo repository.Repo) []Summary {
func ListOpen(repo repository.Repo) []Summary {
var openReviews []Summary
for _, review := range unsortedListAll(repo) {
if !review.Submitted {
if !review.Submitted && review.Request.TargetRef != "" {
openReviews = append(openReviews, review)
}
}
Expand Down
3 changes: 1 addition & 2 deletions schema/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

"required": [
"timestamp",
"requester",
"targetRef"
"requester"
]
}

0 comments on commit 460d35e

Please sign in to comment.