X Tutup
Skip to content

Commit 75cfed4

Browse files
committed
Import PR list API implementation to pr/list package
Also splits List vs. Search queries into separate methods for better maintanability.
1 parent 9b0f706 commit 75cfed4

File tree

6 files changed

+293
-255
lines changed

6 files changed

+293
-255
lines changed

api/queries_pr.go

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/cli/cli/internal/ghinstance"
1414
"github.com/cli/cli/internal/ghrepo"
15-
"github.com/cli/cli/pkg/githubsearch"
1615
"github.com/shurcooL/githubv4"
1716
"golang.org/x/sync/errgroup"
1817
)
@@ -910,173 +909,6 @@ func isBlank(v interface{}) bool {
910909
}
911910
}
912911

913-
func PullRequestList(client *Client, repo ghrepo.Interface, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
914-
type prBlock struct {
915-
Edges []struct {
916-
Node PullRequest
917-
}
918-
PageInfo struct {
919-
HasNextPage bool
920-
EndCursor string
921-
}
922-
TotalCount int
923-
IssueCount int
924-
}
925-
type response struct {
926-
Repository struct {
927-
PullRequests prBlock
928-
}
929-
Search prBlock
930-
}
931-
932-
fragment := `
933-
fragment pr on PullRequest {
934-
number
935-
title
936-
state
937-
url
938-
headRefName
939-
headRepositoryOwner {
940-
login
941-
}
942-
isCrossRepository
943-
isDraft
944-
}
945-
`
946-
947-
// If assignee wasn't specified, use `Repository.pullRequest` for ability to
948-
// query by multiple labels
949-
query := fragment + `
950-
query PullRequestList(
951-
$owner: String!,
952-
$repo: String!,
953-
$limit: Int!,
954-
$endCursor: String,
955-
$baseBranch: String,
956-
$labels: [String!],
957-
$state: [PullRequestState!] = OPEN
958-
) {
959-
repository(owner: $owner, name: $repo) {
960-
pullRequests(
961-
states: $state,
962-
baseRefName: $baseBranch,
963-
labels: $labels,
964-
first: $limit,
965-
after: $endCursor,
966-
orderBy: {field: CREATED_AT, direction: DESC}
967-
) {
968-
totalCount
969-
edges {
970-
node {
971-
...pr
972-
}
973-
}
974-
pageInfo {
975-
hasNextPage
976-
endCursor
977-
}
978-
}
979-
}
980-
}`
981-
982-
var check = make(map[int]struct{})
983-
var prs []PullRequest
984-
pageLimit := min(limit, 100)
985-
variables := map[string]interface{}{}
986-
res := PullRequestAndTotalCount{}
987-
988-
// If assignee was specified, use the `search` API rather than
989-
// `Repository.pullRequests`, but this mode doesn't support multiple labels
990-
if assignee, ok := vars["assignee"].(string); ok {
991-
query = fragment + `
992-
query PullRequestList(
993-
$q: String!,
994-
$limit: Int!,
995-
$endCursor: String,
996-
) {
997-
search(query: $q, type: ISSUE, first: $limit, after: $endCursor) {
998-
issueCount
999-
edges {
1000-
node {
1001-
...pr
1002-
}
1003-
}
1004-
pageInfo {
1005-
hasNextPage
1006-
endCursor
1007-
}
1008-
}
1009-
}`
1010-
q := githubsearch.NewQuery()
1011-
q.SetType(githubsearch.PullRequest)
1012-
q.InRepository(ghrepo.FullName(repo))
1013-
q.AssignedTo(assignee)
1014-
q.SortBy(githubsearch.CreatedAt, githubsearch.Desc)
1015-
if states, ok := vars["state"].([]string); ok && len(states) == 1 {
1016-
switch states[0] {
1017-
case "OPEN":
1018-
q.SetState(githubsearch.Open)
1019-
case "CLOSED":
1020-
q.SetState(githubsearch.Closed)
1021-
case "MERGED":
1022-
q.SetState(githubsearch.Merged)
1023-
}
1024-
}
1025-
if labels, ok := vars["labels"].([]string); ok && len(labels) > 0 {
1026-
if len(labels) > 1 {
1027-
return nil, fmt.Errorf("multiple labels with --assignee are not supported")
1028-
}
1029-
q.AddLabel(labels[0])
1030-
}
1031-
if baseBranch, ok := vars["baseBranch"].(string); ok {
1032-
q.SetBaseBranch(baseBranch)
1033-
}
1034-
variables["q"] = q.String()
1035-
} else {
1036-
variables["owner"] = repo.RepoOwner()
1037-
variables["repo"] = repo.RepoName()
1038-
for name, val := range vars {
1039-
variables[name] = val
1040-
}
1041-
}
1042-
loop:
1043-
for {
1044-
variables["limit"] = pageLimit
1045-
var data response
1046-
err := client.GraphQL(repo.RepoHost(), query, variables, &data)
1047-
if err != nil {
1048-
return nil, err
1049-
}
1050-
prData := data.Repository.PullRequests
1051-
res.TotalCount = prData.TotalCount
1052-
if _, ok := variables["q"]; ok {
1053-
prData = data.Search
1054-
res.TotalCount = prData.IssueCount
1055-
}
1056-
1057-
for _, edge := range prData.Edges {
1058-
if _, exists := check[edge.Node.Number]; exists {
1059-
continue
1060-
}
1061-
1062-
prs = append(prs, edge.Node)
1063-
check[edge.Node.Number] = struct{}{}
1064-
if len(prs) == limit {
1065-
break loop
1066-
}
1067-
}
1068-
1069-
if prData.PageInfo.HasNextPage {
1070-
variables["endCursor"] = prData.PageInfo.EndCursor
1071-
pageLimit = min(pageLimit, limit-len(prs))
1072-
} else {
1073-
break
1074-
}
1075-
}
1076-
res.PullRequests = prs
1077-
return &res, nil
1078-
}
1079-
1080912
func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
1081913
var mutation struct {
1082914
ClosePullRequest struct {

pkg/cmd/pr/list/fixtures/prList.json

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,34 @@
33
"repository": {
44
"pullRequests": {
55
"totalCount": 3,
6-
"edges": [
6+
"nodes": [
77
{
8-
"node": {
9-
"number": 32,
10-
"title": "New feature",
11-
"url": "https://github.com/monalisa/hello/pull/32",
12-
"headRefName": "feature",
13-
"state": "OPEN",
14-
"isDraft": true
15-
}
8+
"number": 32,
9+
"title": "New feature",
10+
"url": "https://github.com/monalisa/hello/pull/32",
11+
"headRefName": "feature",
12+
"state": "OPEN",
13+
"isDraft": true
1614
},
1715
{
18-
"node": {
19-
"number": 29,
20-
"title": "Fixed bad bug",
21-
"url": "https://github.com/monalisa/hello/pull/29",
22-
"headRefName": "bug-fix",
23-
"state": "OPEN",
24-
"isDraft": false,
25-
"isCrossRepository": true,
26-
"headRepositoryOwner": {
27-
"login": "hubot"
28-
}
16+
"number": 29,
17+
"title": "Fixed bad bug",
18+
"url": "https://github.com/monalisa/hello/pull/29",
19+
"headRefName": "bug-fix",
20+
"state": "OPEN",
21+
"isDraft": false,
22+
"isCrossRepository": true,
23+
"headRepositoryOwner": {
24+
"login": "hubot"
2925
}
3026
},
3127
{
32-
"node": {
33-
"number": 28,
34-
"state": "MERGED",
35-
"isDraft": false,
36-
"title": "Improve documentation",
37-
"url": "https://github.com/monalisa/hello/pull/28",
38-
"headRefName": "docs"
39-
}
28+
"number": 28,
29+
"state": "MERGED",
30+
"isDraft": false,
31+
"title": "Improve documentation",
32+
"url": "https://github.com/monalisa/hello/pull/28",
33+
"headRefName": "docs"
4034
}
4135
],
4236
"pageInfo": {

pkg/cmd/pr/list/fixtures/prListWithDuplicates.json

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,27 @@
22
"data": {
33
"repository": {
44
"pullRequests": {
5-
"edges": [
5+
"nodes": [
66
{
7-
"node": {
8-
"number": 32,
9-
"title": "New feature",
10-
"url": "https://github.com/monalisa/hello/pull/32",
11-
"headRefName": "feature"
12-
}
7+
"number": 32,
8+
"title": "New feature",
9+
"url": "https://github.com/monalisa/hello/pull/32",
10+
"headRefName": "feature"
1311
},
1412
{
15-
"node": {
16-
"number": 32,
17-
"title": "New feature",
18-
"url": "https://github.com/monalisa/hello/pull/32",
19-
"headRefName": "feature"
20-
}
13+
"number": 32,
14+
"title": "New feature",
15+
"url": "https://github.com/monalisa/hello/pull/32",
16+
"headRefName": "feature"
2117
},
2218
{
23-
"node": {
24-
"number": 29,
25-
"title": "Fixed bad bug",
26-
"url": "https://github.com/monalisa/hello/pull/29",
27-
"headRefName": "bug-fix",
28-
"isCrossRepository": true,
29-
"headRepositoryOwner": {
30-
"login": "hubot"
31-
}
19+
"number": 29,
20+
"title": "Fixed bad bug",
21+
"url": "https://github.com/monalisa/hello/pull/29",
22+
"headRefName": "bug-fix",
23+
"isCrossRepository": true,
24+
"headRepositoryOwner": {
25+
"login": "hubot"
3226
}
3327
},
3428
{

0 commit comments

Comments
 (0)
X Tutup