-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
286f8db
commit 8240e36
Showing
8 changed files
with
292 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
ghp2 "github.com/shuntaka9576/gh-p2" | ||
) | ||
|
||
type LsParamas struct { | ||
ProjectId string | ||
Field string | ||
} | ||
|
||
func (c *Cmd) Ls(params *LsParamas) error { | ||
res, err := c.Client.GetProjectItems(&ghp2.GetProjectItemsParams{ | ||
ProjectId: params.ProjectId, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
json, err := json.Marshal(res) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "%s\n", json) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package gh | ||
|
||
import ( | ||
"github.com/cli/go-gh" | ||
) | ||
|
||
type GetProjectItemsParams struct { | ||
ProjectId string | ||
Cursor *string | ||
} | ||
|
||
func GetProjectItems(params *GetProjectItemsParams) (*[]byte, error) { | ||
ghql := "query=" + GetProjectItemsQuery(params.ProjectId, params.Cursor) | ||
args := append(graphqlArgs, ghql) | ||
|
||
stdOut, _, err := gh.Exec(args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes := stdOut.Bytes() | ||
|
||
return &bytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package ghp2 | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/shuntaka9576/gh-p2/gh" | ||
) | ||
|
||
type GetProjectItemsParams struct { | ||
ProjectId string | ||
} | ||
|
||
type GetProjectItemsGhRes struct { | ||
Data struct { | ||
Node struct { | ||
Title string `json:"title"` | ||
Items struct { | ||
PageInfo struct { | ||
HasNextPage bool `json:"hasNextPage"` | ||
EndCursor string `json:"endCursor"` | ||
} `json:"pageInfo"` | ||
Nodes []struct { | ||
ID string `json:"id"` | ||
CreatedAt string `json:"createdAt"` | ||
UpdatedAt string `json:"updatedAt"` | ||
IsArchived bool `json:"isArchived"` | ||
Content struct { | ||
TypeName string `json:"__typename,omitempty"` | ||
Title string `json:"title,omitempty"` | ||
State string `json:"state,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
Body string `json:"body,omitempty"` | ||
} `json:"content"` | ||
FieldValues struct { | ||
Nodes []struct { | ||
TypeName string `json:"__typename,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Filed struct { | ||
Name string `json:"name,omitempty"` | ||
} `json:"field"` | ||
} `json:"nodes"` | ||
} `json:"fieldValues"` | ||
} `json:"nodes"` | ||
} `json:"items"` | ||
} `json:"node"` | ||
} `json:"data"` | ||
} | ||
|
||
type GetProjectItemsRes struct { | ||
Title string `json:"title"` | ||
IssueItems []IssueItem `json:"items"` | ||
} | ||
|
||
type IssueItem struct { | ||
Title string `json:"title"` | ||
SingleSelectValues map[string]string `json:"singleFiledValues"` | ||
ItemType gh.ITEM_TYPE `json:"type"` | ||
Body string `json:"body"` | ||
} | ||
|
||
func (c *Client) GetProjectItems(params *GetProjectItemsParams) (*GetProjectItemsRes, error) { | ||
var cursor *string | ||
res := &GetProjectItemsRes{} | ||
|
||
for { | ||
payload, err := gh.GetProjectItems(&gh.GetProjectItemsParams{ | ||
ProjectId: params.ProjectId, | ||
Cursor: cursor, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parsed := &GetProjectItemsGhRes{} | ||
err = json.Unmarshal(*payload, parsed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res.Title == "" { // Set the title only once | ||
res.Title = parsed.Data.Node.Title | ||
} | ||
|
||
for _, node := range parsed.Data.Node.Items.Nodes { | ||
if node.IsArchived { | ||
continue | ||
} | ||
|
||
item := IssueItem{ | ||
Title: node.Content.Title, | ||
Body: node.Content.Body, | ||
} | ||
|
||
switch node.Content.TypeName { | ||
case "DraftIssue": | ||
item.ItemType = gh.DRAFT_ISSUE | ||
case "Issue": | ||
item.ItemType = gh.ISSUE | ||
default: | ||
return nil, err | ||
} | ||
|
||
singleSelectValues := map[string]string{} | ||
for _, fieldValue := range node.FieldValues.Nodes { | ||
if fieldValue.TypeName == "ProjectV2ItemFieldSingleSelectValue" { | ||
singleSelectValues[fieldValue.Filed.Name] = fieldValue.Name | ||
} | ||
} | ||
item.SingleSelectValues = singleSelectValues | ||
|
||
res.IssueItems = append(res.IssueItems, item) | ||
} | ||
|
||
if !parsed.Data.Node.Items.PageInfo.HasNextPage { | ||
break | ||
} | ||
cursor = &parsed.Data.Node.Items.PageInfo.EndCursor | ||
} | ||
|
||
return res, nil | ||
} |