Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shuntaka9576 committed Mar 19, 2024
1 parent 286f8db commit 985c3f6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cli/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cli

type ViewParamas struct {
ProjectId string
Field string
}

func (c *Cmd) View(params *ViewParamas) error {

return nil
}
17 changes: 16 additions & 1 deletion cmd/p2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/alecthomas/kong"
"github.com/k0kubun/pp"
ghp2 "github.com/shuntaka9576/gh-p2"
"github.com/shuntaka9576/gh-p2/cli"
)
Expand All @@ -28,6 +29,10 @@ var CLI struct {
Draft bool `short:"d" name:"draft" help:"Due to GitHub specifications, the --label and --repo options cannot be used together."`
Assignees []string `short:"a" name:"assignees" help:"Specify the GitHub account ID to be assigned."`
} `cmd:"" help:"Option to create an issue or draft issue directly in Project V2."`
View struct {
ProjectTitle string `short:"p" required:"" name:"project-title" help:"Specify the title of ProjectV2."`
Field string `short:"f" name:"Specify ProjectV2 custom fields in the format {keyName}:{valueName}. e.g. Status:Todo, Point:3, date:2022-08-29."`
} `cmd:"" help:"Option to view an issue or draft issue in Project V2. Support only SingleSelectValue."`
}

func main() {
Expand Down Expand Up @@ -58,12 +63,17 @@ func main() {
}

var projectId string
pp.Println(res)
pp.Println(CLI.Create.ProjectTitle)

for _, project := range res.Projects() {
if project.Title == CLI.Create.ProjectTitle {
projectId = project.Id
}
}

fmt.Printf("projectId: %s\n", projectId)

if projectId == "" {
fmt.Fprintf(os.Stderr, "Not found project name: %s\n", CLI.Create.ProjectTitle)
if len(res.Projects()) > 0 {
Expand All @@ -90,8 +100,13 @@ func main() {
Draft: CLI.Create.Draft,
Assignees: CLI.Create.Assignees,
})
case "view":
// TODO
err = c.View(&cli.ViewParamas{
ProjectId: projectId,
Field: CLI.View.Field,
})
}

if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)

Expand Down
43 changes: 43 additions & 0 deletions gh/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,49 @@ func GetListQuery(clientType ClientType, name string) string {
return query
}

func GetProjectItemsQuery(projectId string) string {
query := fmt.Sprintf(`query{
node(id: "%s") {
... on ProjectV2 {
items(first: 20) {
nodes {
id
createdAt
updatedAt
isArchived
content {
__typename
}
fieldValues(first: 20) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
name
optionId
field {
... on ProjectV2SingleSelectField {
id
name
}
}
}
}
}
content {
... on Issue {
title
state
url
}
}
}
}
}
}
}`, projectId)

return query
}

func GetProjectFieldsQuery(projectId string) string {
query := fmt.Sprintf(`query{
node(id: "%s") {
Expand Down
7 changes: 7 additions & 0 deletions gh/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ const (
TITLE PROJECT_V2_DATA_TYPE = "TITLE"
TRACKS PROJECT_V2_DATA_TYPE = "TRACKS"
)

type ITEM_TYPE = string

const (
ISSUE ITEM_TYPE = "ISSUE"
DRAFT_ISSUE ITEM_TYPE = "DRAFT_ISSUE"
)
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ require (
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.2 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/termenv v0.12.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
87 changes: 87 additions & 0 deletions p2_items.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ghp2

import (
"encoding/json"

"github.com/shuntaka9576/gh-p2/gh"
)

type GetProjectItemsParams struct {
ProjectId string
}

type GetProjectItemsGhRes struct {
Data struct {
Node struct {
Items struct {
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 []map[string]interface{} `json:"nodes"`
} `json:"fieldValues"`
} `json:"nodes"`
} `json:"items"`
} `json:"node"`
} `json:"data"`
}

type GetProjectItemsRes struct {
IssueItems []IssueItem
}

type IssueItem struct {
Title string
SingleSelectValues map[string]string
ItemType gh.ITEM_TYPE
Body string
}

func (c *Client) GetProjectItems(params *GetProjectItemsParams) (*GetProjectFieldsRes, error) {
payload, err := gh.GetProjectFields(&gh.GetProjectFieldsParams{
ProjectId: params.ProjectId,
})
if err != nil {
return nil, err
}

parsed := &GetProjectItemsGhRes{}
err = json.Unmarshal(*payload, parsed)
if err != nil {
return nil, err
}

res := &GetProjectItemsRes{}

for _, node := range parsed.Data.Node.Items.Nodes {
item := IssueItem{
Title: node.Content.Title,
Body: node.Content.Body,
}

if node.Content.TypeName == "DraftIssue" {
item.ItemType = gh.DRAFT_ISSUE
} else if node.Content.TypeName == "Issue" {
item.ItemType = gh.ISSUE
} else {
return nil, err
}

for _, filedValue := range node.FieldValues.Nodes {

Check failure on line 79 in p2_items.go

View workflow job for this annotation

GitHub Actions / Run CI (1.22.1)

filedValue declared and not used

Check failure on line 79 in p2_items.go

View workflow job for this annotation

GitHub Actions / Run CI (1.21.8)

filedValue declared and not used
// TODO
}

res.IssueItems = append(res.IssueItems, item)
}

return res, nil

Check failure on line 86 in p2_items.go

View workflow job for this annotation

GitHub Actions / Run CI (1.22.1)

cannot use res (variable of type *GetProjectItemsRes) as *GetProjectFieldsRes value in return statement

Check failure on line 86 in p2_items.go

View workflow job for this annotation

GitHub Actions / Run CI (1.21.8)

cannot use res (variable of type *GetProjectItemsRes) as *GetProjectFieldsRes value in return statement
}

0 comments on commit 985c3f6

Please sign in to comment.