about summary refs log tree commit diff
path: root/third_party/go/git-appraise/commands/request.go
blob: 9a9854c3f8a66bd9d0e8b12d1ace0cf10eac2f92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
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"
	"strings"

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

// Template for the "request" subcommand's output.
const requestSummaryTemplate = `Review requested:
Commit: %s
Target Ref: %s
Review Ref: %s
Message: "%s"
`

var requestFlagSet = flag.NewFlagSet("request", flag.ExitOnError)

var (
	requestMessageFile      = requestFlagSet.String("F", "", "Take the comment from the given file. Use - to read the message from the standard input")
	requestMessage          = requestFlagSet.String("m", "", "Message to attach to the review")
	requestReviewers        = requestFlagSet.String("r", "", "Comma-separated list of reviewers")
	requestSource           = requestFlagSet.String("source", "HEAD", "Revision to review")
	requestTarget           = requestFlagSet.String("target", "refs/heads/master", "Revision against which to review")
	requestQuiet            = requestFlagSet.Bool("quiet", false, "Suppress review summary output")
	requestAllowUncommitted = requestFlagSet.Bool("allow-uncommitted", false, "Allow uncommitted local changes.")
	requestSign             = requestFlagSet.Bool("S", false,
		"GPG sign the content of the request")
)

// Build the template review request based solely on the parsed flag values.
func buildRequestFromFlags(requester string) (request.Request, error) {
	var reviewers []string
	if len(*requestReviewers) > 0 {
		for _, reviewer := range strings.Split(*requestReviewers, ",") {
			reviewers = append(reviewers, strings.TrimSpace(reviewer))
		}
	}
	if *requestMessageFile != "" && *requestMessage == "" {
		var err error
		*requestMessage, err = input.FromFile(*requestMessageFile)
		if err != nil {
			return request.Request{}, err
		}
	}

	return request.New(requester, reviewers, *requestSource, *requestTarget, *requestMessage), nil
}

// Get the commit at which the review request should be anchored.
func getReviewCommit(repo repository.Repo, r request.Request, args []string) (string, string, error) {
	if len(args) > 1 {
		return "", "", errors.New("Only updating a single review is supported.")
	}
	if len(args) == 1 {
		base, err := repo.MergeBase(r.TargetRef, args[0])
		if err != nil {
			return "", "", err
		}
		return args[0], base, nil
	}

	base, err := repo.MergeBase(r.TargetRef, r.ReviewRef)
	if err != nil {
		return "", "", err
	}
	reviewCommits, err := repo.ListCommitsBetween(base, r.ReviewRef)
	if err != nil {
		return "", "", err
	}
	if reviewCommits == nil {
		return "", "", errors.New("There are no commits included in the review request")
	}
	return reviewCommits[0], base, nil
}

// Create a new code review request.
//
// The "args" parameter is all of the command line arguments that followed the subcommand.
func requestReview(repo repository.Repo, args []string) error {
	requestFlagSet.Parse(args)
	args = requestFlagSet.Args()

	if !*requestAllowUncommitted {
		// Requesting a code review with uncommited local changes is usually a mistake, so
		// we want to report that to the user instead of creating the request.
		hasUncommitted, err := repo.HasUncommittedChanges()
		if err != nil {
			return err
		}
		if hasUncommitted {
			return errors.New("You have uncommitted or untracked files. Use --allow-uncommitted to ignore those.")
		}
	}

	userEmail, err := repo.GetUserEmail()
	if err != nil {
		return err
	}
	r, err := buildRequestFromFlags(userEmail)
	if err != nil {
		return err
	}
	if r.ReviewRef == "HEAD" {
		headRef, err := repo.GetHeadRef()
		if err != nil {
			return err
		}
		r.ReviewRef = headRef
	}
	if err := repo.VerifyGitRef(r.TargetRef); err != nil {
		return err
	}
	if err := repo.VerifyGitRef(r.ReviewRef); err != nil {
		return err
	}

	reviewCommit, baseCommit, err := getReviewCommit(repo, r, args)
	if err != nil {
		return err
	}
	r.BaseCommit = baseCommit
	if r.Description == "" {
		description, err := repo.GetCommitMessage(reviewCommit)
		if err != nil {
			return err
		}
		r.Description = description
	}
	if *requestSign {
		key, err := repo.GetUserSigningKey()
		if err != nil {
			return err
		}
		err = gpg.Sign(key, &r)
		if err != nil {
			return err
		}
	}
	note, err := r.Write()
	if err != nil {
		return err
	}
	repo.AppendNote(request.Ref, reviewCommit, note)
	if !*requestQuiet {
		fmt.Printf(requestSummaryTemplate, reviewCommit, r.TargetRef, r.ReviewRef, r.Description)
	}
	return nil
}

// requestCmd defines the "request" subcommand.
var requestCmd = &Command{
	Usage: func(arg0 string) {
		fmt.Printf("Usage: %s request [<option>...] [<review-hash>]\n\nOptions:\n", arg0)
		requestFlagSet.PrintDefaults()
	},
	RunMethod: func(repo repository.Repo, args []string) error {
		return requestReview(repo, args)
	},
}