pr.go 5.1 KB
Newer Older
J
Johan Walles 已提交
1 2 3 4
package commands

import (
	"fmt"
5
	"os"
J
Johan Walles 已提交
6 7 8
	"strconv"

	"github.com/github/hub/github"
9
	"github.com/github/hub/ui"
J
Johan Walles 已提交
10 11 12
	"github.com/github/hub/utils"
)

13
var (
J
Johan Walles 已提交
14
	cmdPr = &Command{
15 16 17 18 19 20
		Run: printHelp,
		Usage: `
pr list [-s <STATE>] [-h <HEAD>] [-b <BASE>] [-o <SORT_KEY> [-^]] [-L <LIMIT>]
pr checkout <PR-NUMBER> [<BRANCH>]
`,
		Long: `Manage GitHub pull requests for the current project.
J
Johan Walles 已提交
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
## Commands:

	* _list_:
		List pull requests in the current project.

	* _checkout_:
		Check out the head of a pull request in a new branch.

## Options:

	-s, --state <STATE>
		Display pull requests with state <STATE> (default: "open").

	-f, --format <FORMAT>
		Pretty print the list of pull requests using format <FORMAT> (default:
		"%sC%>(8)%i%Creset  %t%  l%n"). See the "PRETTY FORMATS" section of the
		git-log manual for some additional details on how placeholders are used in
		format. The available placeholders are:

		%I: pull request number

		%i: pull request number prefixed with "#"

		%U: the URL of this pull request

		%S: state (i.e. "open", "closed")

		%sC: set color to red or green, depending on pull request state.

		%t: title

		%l: colored labels

		%L: raw, comma-separated labels

		%b: body

59 60 61 62
		%B: base branch

		%H: head branch

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
		%au: login name of author

		%as: comma-separated list of assignees

		%Mn: milestone number

		%Mt: milestone title

		%NC: number of comments

		%Nc: number of comments wrapped in parentheses, or blank string if zero.

		%cD: created date-only (no time of day)

		%cr: created date, relative

		%ct: created date, UNIX timestamp

		%cI: created date, ISO 8601 format

		%uD: updated date-only (no time of day)

		%ur: updated date, relative

		%ut: updated date, UNIX timestamp

		%uI: updated date, ISO 8601 format

	-o, --sort <SORT_KEY>
		Sort displayed issues by "created" (default), "updated", "popularity", or "long-running".

	-^ --sort-ascending
		Sort by ascending dates instead of descending.

	-L, --limit <LIMIT>
		Display only the first <LIMIT> issues.
J
Johan Walles 已提交
99 100 101

## See also:

102 103
hub-issue(1), hub-pull-request(1), hub(1)
`,
104 105 106 107
	}

	cmdCheckoutPr = &Command{
		Key: "checkout",
J
Johan Walles 已提交
108
		Run: checkoutPr,
109
	}
110 111 112 113 114 115 116 117 118 119 120 121 122

	cmdListPulls = &Command{
		Key: "list",
		Run: listPulls,
	}

	flagPullRequestState,
	flagPullRequestFormat,
	flagPullRequestSort string

	flagPullRequestSortAscending bool

	flagPullRequestLimit int
123
)
J
Johan Walles 已提交
124 125

func init() {
126 127 128 129 130 131 132 133 134
	cmdListPulls.Flag.StringVarP(&flagPullRequestState, "state", "s", "", "STATE")
	cmdListPulls.Flag.StringVarP(&flagPullRequestBase, "base", "b", "", "BASE")
	cmdListPulls.Flag.StringVarP(&flagPullRequestHead, "head", "h", "", "HEAD")
	cmdListPulls.Flag.StringVarP(&flagPullRequestFormat, "format", "f", "%sC%>(8)%i%Creset  %t%  l%n", "FORMAT")
	cmdListPulls.Flag.StringVarP(&flagPullRequestSort, "sort", "o", "created", "SORT_KEY")
	cmdListPulls.Flag.BoolVarP(&flagPullRequestSortAscending, "sort-ascending", "^", false, "SORT_KEY")
	cmdListPulls.Flag.IntVarP(&flagPullRequestLimit, "limit", "L", -1, "LIMIT")

	cmdPr.Use(cmdListPulls)
J
Johan Walles 已提交
135
	cmdPr.Use(cmdCheckoutPr)
J
Johan Walles 已提交
136
	CmdRunner.Use(cmdPr)
J
Johan Walles 已提交
137 138
}

139 140 141 142 143
func printHelp(command *Command, args *Args) {
	fmt.Print(command.HelpText())
	os.Exit(0)
}

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 183
func listPulls(cmd *Command, args *Args) {
	localRepo, err := github.LocalRepo()
	utils.Check(err)

	project, err := localRepo.MainProject()
	utils.Check(err)

	gh := github.NewClient(project.Host)

	args.NoForward()
	if args.Noop {
		ui.Printf("Would request list of pull requests for %s\n", project)
		return
	}

	flagFilters := map[string]string{
		"state": flagPullRequestState,
		"head":  flagPullRequestHead,
		"base":  flagPullRequestBase,
		"sort":  flagPullRequestSort,
	}
	filters := map[string]interface{}{}
	for flag, filter := range flagFilters {
		if cmd.FlagPassed(flag) {
			filters[flag] = filter
		}
	}
	if flagPullRequestSortAscending {
		filters["direction"] = "asc"
	}

	pulls, err := gh.FetchPullRequests(project, filters, flagPullRequestLimit, nil)
	utils.Check(err)

	colorize := ui.IsTerminal(os.Stdout)
	for _, pr := range pulls {
		ui.Printf(formatPullRequest(pr, flagPullRequestFormat, colorize))
	}
}

J
Johan Walles 已提交
184
func checkoutPr(command *Command, args *Args) {
185 186 187 188 189 190 191
	words := args.Words()
	var newBranchName string

	if len(words) == 0 {
		utils.Check(fmt.Errorf("Error: No pull request number given"))
	} else if len(words) > 1 {
		newBranchName = words[1]
J
Johan Walles 已提交
192 193
	}

194
	prNumberString := words[0]
J
Johan Walles 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208
	_, err := strconv.Atoi(prNumberString)
	utils.Check(err)

	// Figure out the PR URL
	localRepo, err := github.LocalRepo()
	utils.Check(err)
	baseProject, err := localRepo.MainProject()
	utils.Check(err)
	host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
	utils.Check(err)
	client := github.NewClientWithHost(host)
	pr, err := client.PullRequest(baseProject, prNumberString)
	utils.Check(err)

209
	newArgs, err := transformCheckoutArgs(args, pr, newBranchName)
J
Johan Walles 已提交
210
	utils.Check(err)
J
Johan Walles 已提交
211

212
	args.Replace(args.Executable, "checkout", newArgs...)
J
Johan Walles 已提交
213
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227

func formatPullRequest(pr github.PullRequest, format string, colorize bool) string {
	base := pr.Base.Ref
	head := pr.Head.Label
	if pr.IsSameRepo() {
		head = pr.Head.Ref
	}

	placeholders := formatIssuePlaceholders(github.Issue(pr), colorize)
	placeholders["B"] = base
	placeholders["H"] = head

	return ui.Expand(format, placeholders, colorize)
}