X Tutup
Skip to content

Commit 19ea49b

Browse files
committed
Move issue list queries to under the issue/list package
1 parent 61a8049 commit 19ea49b

File tree

4 files changed

+250
-218
lines changed

4 files changed

+250
-218
lines changed

api/queries_issue.go

Lines changed: 0 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package api
22

33
import (
44
"context"
5-
"encoding/base64"
65
"fmt"
7-
"strconv"
8-
"strings"
96
"time"
107

118
"github.com/cli/cli/internal/ghrepo"
@@ -233,123 +230,6 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
233230
return &payload, nil
234231
}
235232

236-
func IssueList(client *Client, repo ghrepo.Interface, state string, assigneeString string, limit int, authorString string, mentionString string, milestoneString string) (*IssuesAndTotalCount, error) {
237-
var states []string
238-
switch state {
239-
case "open", "":
240-
states = []string{"OPEN"}
241-
case "closed":
242-
states = []string{"CLOSED"}
243-
case "all":
244-
states = []string{"OPEN", "CLOSED"}
245-
default:
246-
return nil, fmt.Errorf("invalid state: %s", state)
247-
}
248-
249-
query := fragments + `
250-
query IssueList($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $assignee: String, $author: String, $mention: String, $milestone: String) {
251-
repository(owner: $owner, name: $repo) {
252-
hasIssuesEnabled
253-
issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, filterBy: {assignee: $assignee, createdBy: $author, mentioned: $mention, milestone: $milestone}) {
254-
totalCount
255-
nodes {
256-
...issue
257-
}
258-
pageInfo {
259-
hasNextPage
260-
endCursor
261-
}
262-
}
263-
}
264-
}
265-
`
266-
267-
variables := map[string]interface{}{
268-
"owner": repo.RepoOwner(),
269-
"repo": repo.RepoName(),
270-
"states": states,
271-
}
272-
if assigneeString != "" {
273-
variables["assignee"] = assigneeString
274-
}
275-
if authorString != "" {
276-
variables["author"] = authorString
277-
}
278-
if mentionString != "" {
279-
variables["mention"] = mentionString
280-
}
281-
282-
if milestoneString != "" {
283-
var milestone *RepoMilestone
284-
if milestoneNumber, err := strconv.ParseInt(milestoneString, 10, 32); err == nil {
285-
milestone, err = MilestoneByNumber(client, repo, int32(milestoneNumber))
286-
if err != nil {
287-
return nil, err
288-
}
289-
} else {
290-
milestone, err = MilestoneByTitle(client, repo, "all", milestoneString)
291-
if err != nil {
292-
return nil, err
293-
}
294-
}
295-
296-
milestoneRESTID, err := milestoneNodeIdToDatabaseId(milestone.ID)
297-
if err != nil {
298-
return nil, err
299-
}
300-
variables["milestone"] = milestoneRESTID
301-
}
302-
303-
type responseData struct {
304-
Repository struct {
305-
Issues struct {
306-
TotalCount int
307-
Nodes []Issue
308-
PageInfo struct {
309-
HasNextPage bool
310-
EndCursor string
311-
}
312-
}
313-
HasIssuesEnabled bool
314-
}
315-
}
316-
317-
var issues []Issue
318-
var totalCount int
319-
pageLimit := min(limit, 100)
320-
321-
loop:
322-
for {
323-
var response responseData
324-
variables["limit"] = pageLimit
325-
err := client.GraphQL(repo.RepoHost(), query, variables, &response)
326-
if err != nil {
327-
return nil, err
328-
}
329-
if !response.Repository.HasIssuesEnabled {
330-
return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo))
331-
}
332-
totalCount = response.Repository.Issues.TotalCount
333-
334-
for _, issue := range response.Repository.Issues.Nodes {
335-
issues = append(issues, issue)
336-
if len(issues) == limit {
337-
break loop
338-
}
339-
}
340-
341-
if response.Repository.Issues.PageInfo.HasNextPage {
342-
variables["endCursor"] = response.Repository.Issues.PageInfo.EndCursor
343-
pageLimit = min(pageLimit, limit-len(issues))
344-
} else {
345-
break
346-
}
347-
}
348-
349-
res := IssuesAndTotalCount{Issues: issues, TotalCount: totalCount}
350-
return &res, nil
351-
}
352-
353233
func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, error) {
354234
type response struct {
355235
Repository struct {
@@ -450,80 +330,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
450330
return &resp.Repository.Issue, nil
451331
}
452332

453-
func IssueSearch(client *Client, repo ghrepo.Interface, searchQuery string, limit int) (*IssuesAndTotalCount, error) {
454-
query := fragments +
455-
`query IssueSearch($repo: String!, $owner: String!, $type: SearchType!, $limit: Int, $after: String, $query: String!) {
456-
repository(name: $repo, owner: $owner) {
457-
hasIssuesEnabled
458-
}
459-
search(type: $type, last: $limit, after: $after, query: $query) {
460-
issueCount
461-
nodes { ...issue }
462-
pageInfo {
463-
hasNextPage
464-
endCursor
465-
}
466-
}
467-
}`
468-
469-
type response struct {
470-
Repository struct {
471-
HasIssuesEnabled bool
472-
}
473-
Search struct {
474-
IssueCount int
475-
Nodes []Issue
476-
PageInfo struct {
477-
HasNextPage bool
478-
EndCursor string
479-
}
480-
}
481-
}
482-
483-
perPage := min(limit, 100)
484-
searchQuery = fmt.Sprintf("repo:%s/%s %s", repo.RepoOwner(), repo.RepoName(), searchQuery)
485-
486-
variables := map[string]interface{}{
487-
"owner": repo.RepoOwner(),
488-
"repo": repo.RepoName(),
489-
"type": "ISSUE",
490-
"limit": perPage,
491-
"query": searchQuery,
492-
}
493-
494-
ic := IssuesAndTotalCount{}
495-
496-
loop:
497-
for {
498-
var resp response
499-
err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
500-
if err != nil {
501-
return nil, err
502-
}
503-
504-
if !resp.Repository.HasIssuesEnabled {
505-
return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo))
506-
}
507-
508-
ic.TotalCount = resp.Search.IssueCount
509-
510-
for _, issue := range resp.Search.Nodes {
511-
ic.Issues = append(ic.Issues, issue)
512-
if len(ic.Issues) == limit {
513-
break loop
514-
}
515-
}
516-
517-
if !resp.Search.PageInfo.HasNextPage {
518-
break
519-
}
520-
variables["after"] = resp.Search.PageInfo.EndCursor
521-
variables["perPage"] = min(perPage, limit-len(ic.Issues))
522-
}
523-
524-
return &ic, nil
525-
}
526-
527333
func IssueClose(client *Client, repo ghrepo.Interface, issue Issue) error {
528334
var mutation struct {
529335
CloseIssue struct {
@@ -605,23 +411,6 @@ func IssueUpdate(client *Client, repo ghrepo.Interface, params githubv4.UpdateIs
605411
return err
606412
}
607413

608-
// milestoneNodeIdToDatabaseId extracts the REST Database ID from the GraphQL Node ID
609-
// This conversion is necessary since the GraphQL API requires the use of the milestone's database ID
610-
// for querying the related issues.
611-
func milestoneNodeIdToDatabaseId(nodeId string) (string, error) {
612-
// The Node ID is Base64 obfuscated, with an underlying pattern:
613-
// "09:Milestone12345", where "12345" is the database ID
614-
decoded, err := base64.StdEncoding.DecodeString(nodeId)
615-
if err != nil {
616-
return "", err
617-
}
618-
splitted := strings.Split(string(decoded), "Milestone")
619-
if len(splitted) != 2 {
620-
return "", fmt.Errorf("couldn't get database id from node id")
621-
}
622-
return splitted[1], nil
623-
}
624-
625414
func (i Issue) Link() string {
626415
return i.URL
627416
}

0 commit comments

Comments
 (0)
X Tutup