X Tutup
Skip to content

Commit 02a2ed2

Browse files
committed
Add repo view --json export functionality
1 parent 5f0301c commit 02a2ed2

File tree

10 files changed

+450
-49
lines changed

10 files changed

+450
-49
lines changed

api/export_pr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
1313
switch f {
1414
case "milestone":
1515
if issue.Milestone.Title != "" {
16-
data[f] = &issue.Milestone
16+
data[f] = map[string]string{"title": issue.Milestone.Title}
1717
} else {
1818
data[f] = nil
1919
}
@@ -44,7 +44,7 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
4444
data[f] = map[string]string{"name": pr.HeadRepository.Name}
4545
case "milestone":
4646
if pr.Milestone.Title != "" {
47-
data[f] = &pr.Milestone
47+
data[f] = map[string]string{"title": pr.Milestone.Title}
4848
} else {
4949
data[f] = nil
5050
}

api/export_repo.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package api
2+
3+
import (
4+
"reflect"
5+
)
6+
7+
func (repo *Repository) ExportData(fields []string) *map[string]interface{} {
8+
v := reflect.ValueOf(repo).Elem()
9+
data := map[string]interface{}{}
10+
11+
for _, f := range fields {
12+
switch f {
13+
case "parent":
14+
data[f] = miniRepoExport(repo.Parent)
15+
case "templateRepository":
16+
data[f] = miniRepoExport(repo.TemplateRepository)
17+
case "languages":
18+
data[f] = repo.Languages.Edges
19+
case "labels":
20+
data[f] = repo.Labels.Nodes
21+
case "assignableUsers":
22+
data[f] = repo.AssignableUsers.Nodes
23+
case "mentionableUsers":
24+
data[f] = repo.MentionableUsers.Nodes
25+
case "milestones":
26+
data[f] = repo.Milestones.Nodes
27+
case "projects":
28+
data[f] = repo.Projects.Nodes
29+
case "repositoryTopics":
30+
var topics []RepositoryTopic
31+
for _, n := range repo.RepositoryTopics.Nodes {
32+
topics = append(topics, n.Topic)
33+
}
34+
data[f] = topics
35+
default:
36+
sf := fieldByName(v, f)
37+
data[f] = sf.Interface()
38+
}
39+
}
40+
41+
return &data
42+
}
43+
44+
func miniRepoExport(r *Repository) map[string]interface{} {
45+
if r == nil {
46+
return nil
47+
}
48+
return map[string]interface{}{
49+
"id": r.ID,
50+
"name": r.Name,
51+
"owner": r.Owner,
52+
}
53+
}

api/queries_issue.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func (p ProjectCards) ProjectNames() []string {
9191
}
9292

9393
type Milestone struct {
94-
Title string `json:"title"`
94+
Number int `json:"number"`
95+
Title string `json:"title"`
96+
Description string `json:"description"`
97+
DueOn *time.Time `json:"dueOn"`
9598
}
9699

97100
type IssuesDisabledError struct {

api/queries_repo.go

Lines changed: 164 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,177 @@ import (
1616

1717
// Repository contains information about a GitHub repo
1818
type Repository struct {
19-
ID string
20-
Name string
21-
Description string
22-
URL string
23-
CloneURL string
24-
CreatedAt time.Time
25-
Owner RepositoryOwner
26-
27-
IsPrivate bool
28-
HasIssuesEnabled bool
29-
HasWikiEnabled bool
30-
ViewerPermission string
31-
DefaultBranchRef BranchRef
32-
33-
Parent *Repository
34-
35-
MergeCommitAllowed bool
36-
RebaseMergeAllowed bool
37-
SquashMergeAllowed bool
19+
ID string
20+
Name string
21+
NameWithOwner string
22+
Owner RepositoryOwner
23+
Parent *Repository
24+
TemplateRepository *Repository
25+
Description string
26+
HomepageURL string
27+
OpenGraphImageURL string
28+
UsesCustomOpenGraphImage bool
29+
URL string
30+
SSHURL string
31+
MirrorURL string
32+
SecurityPolicyURL string
33+
34+
CreatedAt time.Time
35+
PushedAt *time.Time
36+
UpdatedAt time.Time
37+
38+
IsBlankIssuesEnabled bool
39+
IsSecurityPolicyEnabled bool
40+
HasIssuesEnabled bool
41+
HasProjectsEnabled bool
42+
HasWikiEnabled bool
43+
MergeCommitAllowed bool
44+
SquashMergeAllowed bool
45+
RebaseMergeAllowed bool
46+
47+
ForkCount int
48+
StargazerCount int
49+
Watchers struct {
50+
TotalCount int `json:"totalCount"`
51+
}
52+
Issues struct {
53+
TotalCount int `json:"totalCount"`
54+
}
55+
PullRequests struct {
56+
TotalCount int `json:"totalCount"`
57+
}
58+
59+
CodeOfConduct *CodeOfConduct
60+
ContactLinks []ContactLink
61+
DefaultBranchRef BranchRef
62+
DeleteBranchOnMerge bool
63+
DiskUsage int
64+
FundingLinks []FundingLink
65+
IsArchived bool
66+
IsEmpty bool
67+
IsFork bool
68+
IsInOrganization bool
69+
IsMirror bool
70+
IsPrivate bool
71+
IsTemplate bool
72+
IsUserConfigurationRepository bool
73+
LicenseInfo *RepositoryLicense
74+
ViewerCanAdminister bool
75+
ViewerDefaultCommitEmail string
76+
ViewerDefaultMergeMethod string
77+
ViewerHasStarred bool
78+
ViewerPermission string
79+
ViewerPossibleCommitEmails []string
80+
ViewerSubscription string
81+
82+
RepositoryTopics struct {
83+
Nodes []struct {
84+
Topic RepositoryTopic
85+
}
86+
}
87+
PrimaryLanguage *CodingLanguage
88+
Languages struct {
89+
Edges []struct {
90+
Size int `json:"size"`
91+
Node CodingLanguage `json:"node"`
92+
}
93+
}
94+
IssueTemplates []IssueTemplate
95+
PullRequestTemplates []PullRequestTemplate
96+
Labels struct {
97+
Nodes []IssueLabel
98+
}
99+
Milestones struct {
100+
Nodes []Milestone
101+
}
102+
LatestRelease *RepositoryRelease
103+
104+
AssignableUsers struct {
105+
Nodes []GitHubUser
106+
}
107+
MentionableUsers struct {
108+
Nodes []GitHubUser
109+
}
110+
Projects struct {
111+
Nodes []RepoProject
112+
}
38113

39114
// pseudo-field that keeps track of host name of this repo
40115
hostname string
41116
}
42117

43118
// RepositoryOwner is the owner of a GitHub repository
44119
type RepositoryOwner struct {
45-
Login string
120+
ID string `json:"id"`
121+
Login string `json:"login"`
122+
}
123+
124+
type GitHubUser struct {
125+
ID string `json:"id"`
126+
Login string `json:"login"`
127+
Name string `json:"name"`
46128
}
47129

48130
// BranchRef is the branch name in a GitHub repository
49131
type BranchRef struct {
50-
Name string
132+
Name string `json:"name"`
133+
}
134+
135+
type CodeOfConduct struct {
136+
Key string `json:"key"`
137+
Name string `json:"name"`
138+
URL string `json:"url"`
139+
}
140+
141+
type RepositoryLicense struct {
142+
Key string `json:"key"`
143+
Name string `json:"name"`
144+
Nickname string `json:"nickname"`
145+
}
146+
147+
type ContactLink struct {
148+
About string `json:"about"`
149+
Name string `json:"name"`
150+
URL string `json:"url"`
151+
}
152+
153+
type FundingLink struct {
154+
Platform string `json:"platform"`
155+
URL string `json:"url"`
156+
}
157+
158+
type CodingLanguage struct {
159+
Name string `json:"name"`
160+
}
161+
162+
type IssueTemplate struct {
163+
Name string `json:"name"`
164+
Title string `json:"title"`
165+
Body string `json:"body"`
166+
About string `json:"about"`
167+
}
168+
169+
type PullRequestTemplate struct {
170+
Filename string `json:"filename"`
171+
Body string `json:"body"`
172+
}
173+
174+
type RepositoryTopic struct {
175+
Name string `json:"name"`
176+
}
177+
178+
type RepositoryRelease struct {
179+
Name string `json:"name"`
180+
TagName string `json:"tagName"`
181+
URL string `json:"url"`
182+
PublishedAt time.Time `json:"publishedAt"`
183+
}
184+
185+
type IssueLabel struct {
186+
ID string `json:"id"`
187+
Name string `json:"name"`
188+
Description string `json:"description"`
189+
Color string `json:"color"`
51190
}
52191

53192
// RepoOwner is the login name of the owner
@@ -65,11 +204,6 @@ func (r Repository) RepoHost() string {
65204
return r.hostname
66205
}
67206

68-
// IsFork is true when this repository has a parent repository
69-
func (r Repository) IsFork() bool {
70-
return r.Parent != nil
71-
}
72-
73207
// ViewerCanPush is true when the requesting user has push access
74208
func (r Repository) ViewerCanPush() bool {
75209
switch r.ViewerPermission {
@@ -305,7 +439,6 @@ type repositoryV3 struct {
305439
NodeID string
306440
Name string
307441
CreatedAt time.Time `json:"created_at"`
308-
CloneURL string `json:"clone_url"`
309442
Owner struct {
310443
Login string
311444
}
@@ -324,7 +457,6 @@ func ForkRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
324457
return &Repository{
325458
ID: result.NodeID,
326459
Name: result.Name,
327-
CloneURL: result.CloneURL,
328460
CreatedAt: result.CreatedAt,
329461
Owner: RepositoryOwner{
330462
Login: result.Owner.Login,
@@ -707,9 +839,10 @@ func RepoResolveMetadataIDs(client *Client, repo ghrepo.Interface, input RepoRes
707839
}
708840

709841
type RepoProject struct {
710-
ID string
711-
Name string
712-
ResourcePath string
842+
ID string `json:"id"`
843+
Name string `json:"name"`
844+
Number int `json:"number"`
845+
ResourcePath string `json:"resourcePath"`
713846
}
714847

715848
// RepoProjects fetches all open projects for a repository

api/queries_repo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ func Test_RepoMetadata(t *testing.T) {
144144
func Test_ProjectsToPaths(t *testing.T) {
145145
expectedProjectPaths := []string{"OWNER/REPO/PROJECT_NUMBER", "ORG/PROJECT_NUMBER"}
146146
projects := []RepoProject{
147-
{"id1", "My Project", "/OWNER/REPO/projects/PROJECT_NUMBER"},
148-
{"id2", "Org Project", "/orgs/ORG/projects/PROJECT_NUMBER"},
149-
{"id3", "Project", "/orgs/ORG/projects/PROJECT_NUMBER_2"},
147+
{ID: "id1", Name: "My Project", ResourcePath: "/OWNER/REPO/projects/PROJECT_NUMBER"},
148+
{ID: "id2", Name: "Org Project", ResourcePath: "/orgs/ORG/projects/PROJECT_NUMBER"},
149+
{ID: "id3", Name: "Project", ResourcePath: "/orgs/ORG/projects/PROJECT_NUMBER_2"},
150150
}
151151
projectNames := []string{"My Project", "Org Project"}
152152

0 commit comments

Comments
 (0)
X Tutup