-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp2_list.go
95 lines (81 loc) · 1.74 KB
/
p2_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
59
60
61
62
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
package ghp2
import (
"encoding/json"
"github.com/shuntaka9576/gh-p2/gh"
)
type ListProjectResItem struct {
Id string
Title string
}
type ListProjectItemList = []ListProjectResItem
type ListProjectRes interface {
Projects() ListProjectItemList
}
type ListProjectOrgRes struct {
Data struct {
Organization struct {
ProjectsV2 struct {
Nodes []struct {
Id string `json:"id"`
Title string `json:"title"`
} `json:"nodes"`
} `json:"projectsV2"`
} `json:"organization"`
} `json:"data"`
}
type ListProjectUserRes struct {
Data struct {
User struct {
ProjectsV2 struct {
Nodes []struct {
Id string `json:"id"`
Title string `json:"title"`
} `json:"nodes"`
} `json:"projectsV2"`
} `json:"user"`
} `json:"data"`
}
func (l ListProjectOrgRes) Projects() (res ListProjectItemList) {
for _, pj := range l.Data.Organization.ProjectsV2.Nodes {
res = append(res, ListProjectResItem{
Id: pj.Id,
Title: pj.Title,
})
}
return res
}
func (l ListProjectUserRes) Projects() (res ListProjectItemList) {
for _, pj := range l.Data.User.ProjectsV2.Nodes {
res = append(res, ListProjectResItem{
Id: pj.Id,
Title: pj.Title,
})
}
return res
}
func (c *Client) ListProject() (ListProjectRes, error) {
res, err := gh.ListProject(&gh.ListProjectParams{
ClientType: c.ClientType,
Name: c.Name,
})
if err != nil {
return nil, err
}
var project ListProjectRes
if c.ClientType == gh.ORGANIZATION {
org := ListProjectOrgRes{}
err = json.Unmarshal(*res, &org)
if err != nil {
return nil, err
}
project = org
} else {
user := ListProjectUserRes{}
err = json.Unmarshal(*res, &user)
if err != nil {
return nil, err
}
project = user
}
return project, nil
}