-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathminimal_types.go
More file actions
1802 lines (1605 loc) · 56.1 KB
/
Copy pathminimal_types.go
File metadata and controls
1802 lines (1605 loc) · 56.1 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package github
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/go-github/v87/github"
"github.com/github/github-mcp-server/pkg/sanitize"
)
// codeSearchItemFieldEnum lists the selectable fields for search_code result
// items, matching the JSON field names of MinimalCodeResult. The repository and
// text_matches fields are the heaviest, so omitting them is the main lever for
// shrinking large result sets.
var codeSearchItemFieldEnum = []any{"name", "path", "sha", "repository", "text_matches"}
// fileContentFieldEnum lists the selectable fields for get_file_contents
// directory listings, matching the JSON field names of
// github.RepositoryContent that appear for directory entries. Only applied when
// the requested path is a directory; ignored for single files.
var fileContentFieldEnum = []any{"type", "name", "path", "size", "sha", "url", "git_url", "html_url", "download_url"}
// filterFields marshals v to a JSON object and returns a map containing only the
// requested fields. Fields that are unknown or absent from the JSON (for example
// empty values dropped via omitempty) are skipped.
func filterFields(v any, fields []string) (map[string]any, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber() // preserve integer precision for fields such as IDs
var object map[string]any
if err := decoder.Decode(&object); err != nil {
return nil, err
}
picked := make(map[string]any, len(fields))
for _, field := range fields {
if value, ok := object[field]; ok {
picked[field] = value
}
}
return picked, nil
}
// filterEachField applies filterFields to every item, returning a slice in which
// each element contains only the requested fields.
func filterEachField[T any](items []T, fields []string) ([]map[string]any, error) {
filtered := make([]map[string]any, 0, len(items))
for _, item := range items {
picked, err := filterFields(item, fields)
if err != nil {
return nil, err
}
filtered = append(filtered, picked)
}
return filtered, nil
}
// MinimalUser is the output type for user and organization search results.
type MinimalUser struct {
Login string `json:"login"`
ID int64 `json:"id,omitempty"`
ProfileURL string `json:"profile_url,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Details *UserDetails `json:"details,omitempty"` // Optional field for additional user details
}
// MinimalSearchUsersResult is the trimmed output type for user search results.
type MinimalSearchUsersResult struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []MinimalUser `json:"items"`
}
// MinimalRepository is the trimmed output type for repository objects to reduce verbosity.
type MinimalRepository struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description,omitempty"`
HTMLURL string `json:"html_url"`
Language string `json:"language,omitempty"`
Stars int `json:"stargazers_count"`
Forks int `json:"forks_count"`
OpenIssues int `json:"open_issues_count"`
UpdatedAt string `json:"updated_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Topics []string `json:"topics,omitempty"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Archived bool `json:"archived"`
DefaultBranch string `json:"default_branch,omitempty"`
}
// MinimalSearchRepositoriesResult is the trimmed output type for repository search results.
type MinimalSearchRepositoriesResult struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []MinimalRepository `json:"items"`
}
// MinimalDiscussionComment is the trimmed output type for discussion comment objects.
type MinimalDiscussionComment struct {
ID string `json:"id"`
Body string `json:"body"`
IsAnswer bool `json:"isAnswer,omitempty"`
Replies []MinimalDiscussionComment `json:"replies,omitempty"`
ReplyTotalCount int `json:"replyTotalCount,omitempty"`
}
// MinimalCodeSearchResult is the trimmed output type for code search results.
type MinimalCodeSearchResult struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []MinimalCodeResult `json:"items"`
}
// MinimalCodeResult is the trimmed output type for a single code search hit.
type MinimalCodeResult struct {
Name string `json:"name"`
Path string `json:"path"`
SHA string `json:"sha"`
Repository string `json:"repository"`
TextMatches []*github.TextMatch `json:"text_matches,omitempty"`
}
// MinimalCommitAuthor represents commit author information.
type MinimalCommitAuthor struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Date string `json:"date,omitempty"`
}
// MinimalCommitInfo represents core commit information.
type MinimalCommitInfo struct {
Message string `json:"message"`
Author *MinimalCommitAuthor `json:"author,omitempty"`
Committer *MinimalCommitAuthor `json:"committer,omitempty"`
}
// MinimalCommitStats represents commit statistics.
type MinimalCommitStats struct {
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
Total int `json:"total,omitempty"`
}
// MinimalCommitFile represents a file changed in a commit.
type MinimalCommitFile struct {
Filename string `json:"filename"`
Status string `json:"status,omitempty"`
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
Changes int `json:"changes,omitempty"`
Patch string `json:"patch,omitempty"`
}
// MinimalPRFile represents a file changed in a pull request.
// Compared to MinimalCommitFile, it includes the patch diff and previous filename for renames.
type MinimalPRFile struct {
Filename string `json:"filename"`
Status string `json:"status,omitempty"`
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
Changes int `json:"changes,omitempty"`
Patch string `json:"patch,omitempty"`
PreviousFilename string `json:"previous_filename,omitempty"`
}
// MinimalPullRequestCommit is the trimmed output type for commits listed on a pull request.
type MinimalPullRequestCommit struct {
SHA string `json:"sha"`
HTMLURL string `json:"html_url,omitempty"`
Message string `json:"message,omitempty"`
Author *MinimalCommitAuthor `json:"author,omitempty"`
}
// MinimalCommit is the trimmed output type for commit objects.
type MinimalCommit struct {
SHA string `json:"sha"`
HTMLURL string `json:"html_url"`
Commit *MinimalCommitInfo `json:"commit,omitempty"`
Author *MinimalUser `json:"author,omitempty"`
Committer *MinimalUser `json:"committer,omitempty"`
Stats *MinimalCommitStats `json:"stats,omitempty"`
Files []MinimalCommitFile `json:"files,omitempty"`
}
// MinimalRepoRef is a lightweight reference to a repository, used when a
// result needs to identify which repository it belongs to (for example, in
// cross-repo commit search results).
type MinimalRepoRef struct {
FullName string `json:"full_name"`
HTMLURL string `json:"html_url,omitempty"`
Private bool `json:"private,omitempty"`
}
// MinimalCommitSearchItem extends MinimalCommit with the containing
// repository, since commit search spans repositories and callers need to
// know which repo each result came from.
type MinimalCommitSearchItem struct {
MinimalCommit
Repository *MinimalRepoRef `json:"repository,omitempty"`
}
// MinimalRelease is the trimmed output type for release objects.
type MinimalRelease struct {
ID int64 `json:"id"`
TagName string `json:"tag_name"`
Name string `json:"name,omitempty"`
Body string `json:"body,omitempty"`
HTMLURL string `json:"html_url"`
PublishedAt string `json:"published_at,omitempty"`
Prerelease bool `json:"prerelease"`
Draft bool `json:"draft"`
Author *MinimalUser `json:"author,omitempty"`
}
// MinimalBranch is the trimmed output type for branch objects.
type MinimalBranch struct {
Name string `json:"name"`
SHA string `json:"sha"`
Protected bool `json:"protected"`
}
// MinimalTag is the trimmed output type for tag objects.
type MinimalTag struct {
Name string `json:"name"`
SHA string `json:"sha"`
}
// MinimalResponse represents a minimal response for all CRUD operations.
// Success is implicit in the HTTP response status, and all other information
// can be derived from the URL or fetched separately if needed.
type MinimalResponse struct {
ID string `json:"id"`
URL string `json:"url"`
}
// MinimalCollaborator is the trimmed output type for repository collaborators.
type MinimalCollaborator struct {
Login string `json:"login"`
ID int64 `json:"id"`
RoleName string `json:"role_name"`
}
type MinimalProject struct {
ID *int64 `json:"id,omitempty"`
NodeID *string `json:"node_id,omitempty"`
Owner *MinimalUser `json:"owner,omitempty"`
Creator *MinimalUser `json:"creator,omitempty"`
Title *string `json:"title,omitempty"`
Description *string `json:"description,omitempty"`
Public *bool `json:"public,omitempty"`
ClosedAt *github.Timestamp `json:"closed_at,omitempty"`
CreatedAt *github.Timestamp `json:"created_at,omitempty"`
UpdatedAt *github.Timestamp `json:"updated_at,omitempty"`
DeletedAt *github.Timestamp `json:"deleted_at,omitempty"`
Number *int `json:"number,omitempty"`
ShortDescription *string `json:"short_description,omitempty"`
DeletedBy *MinimalUser `json:"deleted_by,omitempty"`
OwnerType string `json:"owner_type,omitempty"`
}
type MinimalProjectItem struct {
ID int64 `json:"id"`
NodeID string `json:"node_id,omitempty"`
ContentType string `json:"content_type,omitempty"`
Content *MinimalProjectItemContent `json:"content,omitempty"`
Fields []MinimalProjectItemFieldValue `json:"fields,omitempty"`
ArchivedAt string `json:"archived_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Creator string `json:"creator,omitempty"`
}
type MinimalProjectItemContent struct {
ID int64 `json:"id,omitempty"`
NodeID string `json:"node_id,omitempty"`
Number int `json:"number,omitempty"`
Title string `json:"title,omitempty"`
State string `json:"state,omitempty"`
StateReason string `json:"state_reason,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Repository string `json:"repository,omitempty"`
Author string `json:"author,omitempty"`
Assignees []string `json:"assignees,omitempty"`
Labels []string `json:"labels,omitempty"`
Milestone string `json:"milestone,omitempty"`
Comments int `json:"comments,omitempty"`
Draft bool `json:"draft,omitempty"`
Merged bool `json:"merged,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
ClosedAt string `json:"closed_at,omitempty"`
MergedAt string `json:"merged_at,omitempty"`
}
type MinimalProjectItemFieldValue struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
DataType string `json:"data_type,omitempty"`
Value any `json:"value,omitempty"`
}
type minimalProjectOptionValue struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
}
type minimalProjectIterationValue struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
StartDate string `json:"start_date,omitempty"`
Duration int `json:"duration,omitempty"`
}
type minimalProjectPullRequestRef struct {
Number int `json:"number,omitempty"`
Title string `json:"title,omitempty"`
State string `json:"state,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Repository string `json:"repository,omitempty"`
}
// MinimalReactions is the trimmed output type for reaction summaries, dropping the API URL.
type MinimalReactions struct {
TotalCount int `json:"total_count"`
PlusOne int `json:"+1"`
MinusOne int `json:"-1"`
Laugh int `json:"laugh"`
Confused int `json:"confused"`
Heart int `json:"heart"`
Hooray int `json:"hooray"`
Rocket int `json:"rocket"`
Eyes int `json:"eyes"`
}
// MinimalIssueFieldValueSingleSelectOption is the trimmed output type for a single-select option of an issue field value.
type MinimalIssueFieldValueSingleSelectOption struct {
ID int64 `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
}
// MinimalIssueFieldValue is the trimmed output type for a custom field value attached to an issue,
// populated from REST API responses (e.g. get_issue). For GraphQL-sourced field values see MinimalFieldValue.
type MinimalIssueFieldValue struct {
IssueFieldID int64 `json:"issue_field_id,omitempty"`
NodeID string `json:"node_id,omitempty"`
DataType string `json:"data_type,omitempty"`
Value any `json:"value,omitempty"`
SingleSelectOption *MinimalIssueFieldValueSingleSelectOption `json:"single_select_option,omitempty"`
}
// MinimalFieldValue is the trimmed output type for a custom field value resolved via GraphQL
// (e.g. list_issues, search_issues). Single-value variants populate Value; Values is reserved for multi-select.
type MinimalFieldValue struct {
Field string `json:"field"`
Value string `json:"value,omitempty"`
Values []string `json:"values,omitempty"`
}
// MinimalIssue is the trimmed output type for issue objects to reduce verbosity.
type MinimalIssue struct {
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body,omitempty"`
State string `json:"state"`
StateReason string `json:"state_reason,omitempty"`
Draft bool `json:"draft,omitempty"`
Locked bool `json:"locked,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
User *MinimalUser `json:"user,omitempty"`
AuthorAssociation string `json:"author_association,omitempty"`
Labels []string `json:"labels,omitempty"`
Assignees []string `json:"assignees,omitempty"`
Milestone string `json:"milestone,omitempty"`
Comments int `json:"comments,omitempty"`
Reactions *MinimalReactions `json:"reactions,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
ClosedAt string `json:"closed_at,omitempty"`
ClosedBy string `json:"closed_by,omitempty"`
IssueType string `json:"issue_type,omitempty"`
IssueFieldValues []MinimalIssueFieldValue `json:"issue_field_values,omitempty"`
FieldValues []MinimalFieldValue `json:"field_values,omitempty"`
}
// MinimalIssuesResponse is the trimmed output for a paginated list of issues.
type MinimalIssuesResponse struct {
Issues []MinimalIssue `json:"issues"`
TotalCount int `json:"totalCount"`
PageInfo MinimalPageInfo `json:"pageInfo"`
}
// MinimalIssueComment is the trimmed output type for issue comment objects to reduce verbosity.
type MinimalIssueComment struct {
ID int64 `json:"id"`
Body string `json:"body,omitempty"`
HTMLURL string `json:"html_url"`
User *MinimalUser `json:"user,omitempty"`
AuthorAssociation string `json:"author_association,omitempty"`
Reactions *MinimalReactions `json:"reactions,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// MinimalSearchCommitsResult is the trimmed output type for commit search results.
type MinimalSearchCommitsResult struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []MinimalCommitSearchItem `json:"items"`
}
// MinimalFileContentResponse is the trimmed output type for create/update/delete file responses.
type MinimalFileContentResponse struct {
Content *MinimalFileContent `json:"content,omitempty"`
Commit *MinimalFileCommit `json:"commit,omitempty"`
}
// MinimalFileContent is the trimmed content portion of a file operation response.
type MinimalFileContent struct {
Name string `json:"name"`
Path string `json:"path"`
SHA string `json:"sha"`
Size int `json:"size,omitempty"`
HTMLURL string `json:"html_url"`
}
// MinimalFileCommit is the trimmed commit portion of a file operation response.
type MinimalFileCommit struct {
SHA string `json:"sha"`
Message string `json:"message,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Author *MinimalCommitAuthor `json:"author,omitempty"`
}
// MinimalPullRequest is the trimmed output type for pull request objects to reduce verbosity.
type MinimalPullRequest struct {
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body,omitempty"`
State string `json:"state"`
Draft bool `json:"draft"`
Merged bool `json:"merged"`
MergeableState string `json:"mergeable_state,omitempty"`
HTMLURL string `json:"html_url"`
User *MinimalUser `json:"user,omitempty"`
Labels []string `json:"labels,omitempty"`
Assignees []string `json:"assignees,omitempty"`
RequestedReviewers []string `json:"requested_reviewers,omitempty"`
MergedBy string `json:"merged_by,omitempty"`
Head *MinimalPRBranch `json:"head,omitempty"`
Base *MinimalPRBranch `json:"base,omitempty"`
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
ChangedFiles int `json:"changed_files,omitempty"`
Commits int `json:"commits,omitempty"`
Comments int `json:"comments,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
ClosedAt string `json:"closed_at,omitempty"`
MergedAt string `json:"merged_at,omitempty"`
Milestone string `json:"milestone,omitempty"`
}
// MinimalPRBranch is the trimmed output type for pull request branch references.
type MinimalPRBranch struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
Repo *MinimalPRBranchRepo `json:"repo,omitempty"`
}
// MinimalPRBranchRepo is the trimmed repo info nested inside a PR branch.
type MinimalPRBranchRepo struct {
FullName string `json:"full_name"`
Description string `json:"description,omitempty"`
}
type MinimalProjectStatusUpdate struct {
ID string `json:"id"`
Body string `json:"body,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
StartDate string `json:"start_date,omitempty"`
TargetDate string `json:"target_date,omitempty"`
Creator *MinimalUser `json:"creator,omitempty"`
}
// MinimalPullRequestReview is the trimmed output type for pull request review objects to reduce verbosity.
type MinimalPullRequestReview struct {
ID int64 `json:"id"`
State string `json:"state"`
Body string `json:"body,omitempty"`
HTMLURL string `json:"html_url"`
User *MinimalUser `json:"user,omitempty"`
CommitID string `json:"commit_id,omitempty"`
SubmittedAt string `json:"submitted_at,omitempty"`
AuthorAssociation string `json:"author_association,omitempty"`
}
// Helper functions
func convertToMinimalPullRequestReview(review *github.PullRequestReview) MinimalPullRequestReview {
m := MinimalPullRequestReview{
ID: review.GetID(),
State: review.GetState(),
Body: review.GetBody(),
HTMLURL: review.GetHTMLURL(),
User: convertToMinimalUser(review.GetUser()),
CommitID: review.GetCommitID(),
AuthorAssociation: review.GetAuthorAssociation(),
}
if review.SubmittedAt != nil {
m.SubmittedAt = review.SubmittedAt.Format(time.RFC3339)
}
return m
}
func convertToMinimalIssue(issue *github.Issue) MinimalIssue {
m := MinimalIssue{
Number: issue.GetNumber(),
Title: issue.GetTitle(),
Body: issue.GetBody(),
State: issue.GetState(),
StateReason: issue.GetStateReason(),
Draft: issue.GetDraft(),
Locked: issue.GetLocked(),
HTMLURL: issue.GetHTMLURL(),
User: convertToMinimalUser(issue.GetUser()),
AuthorAssociation: issue.GetAuthorAssociation(),
Comments: issue.GetComments(),
}
if issue.CreatedAt != nil {
m.CreatedAt = issue.CreatedAt.Format(time.RFC3339)
}
if issue.UpdatedAt != nil {
m.UpdatedAt = issue.UpdatedAt.Format(time.RFC3339)
}
if issue.ClosedAt != nil {
m.ClosedAt = issue.ClosedAt.Format(time.RFC3339)
}
for _, label := range issue.Labels {
if label != nil {
m.Labels = append(m.Labels, label.GetName())
}
}
for _, assignee := range issue.Assignees {
if assignee != nil {
m.Assignees = append(m.Assignees, assignee.GetLogin())
}
}
if closedBy := issue.GetClosedBy(); closedBy != nil {
m.ClosedBy = closedBy.GetLogin()
}
if milestone := issue.GetMilestone(); milestone != nil {
m.Milestone = milestone.GetTitle()
}
if issueType := issue.GetType(); issueType != nil {
m.IssueType = issueType.GetName()
}
for _, fv := range issue.IssueFieldValues {
if fv == nil {
continue
}
mfv := MinimalIssueFieldValue{
IssueFieldID: fv.IssueFieldID,
NodeID: fv.NodeID,
DataType: fv.DataType,
Value: fv.Value,
}
if opt := fv.SingleSelectOption; opt != nil {
mfv.SingleSelectOption = &MinimalIssueFieldValueSingleSelectOption{
ID: opt.ID,
Name: opt.Name,
Color: opt.Color,
}
}
m.IssueFieldValues = append(m.IssueFieldValues, mfv)
}
if r := issue.Reactions; r != nil {
m.Reactions = &MinimalReactions{
TotalCount: r.GetTotalCount(),
PlusOne: r.GetPlusOne(),
MinusOne: r.GetMinusOne(),
Laugh: r.GetLaugh(),
Confused: r.GetConfused(),
Heart: r.GetHeart(),
Hooray: r.GetHooray(),
Rocket: r.GetRocket(),
Eyes: r.GetEyes(),
}
}
return m
}
func fragmentToMinimalIssue(fragment IssueFragment) MinimalIssue {
m := MinimalIssue{
Number: int(fragment.Number),
Title: sanitize.Sanitize(string(fragment.Title)),
Body: sanitize.Sanitize(string(fragment.Body)),
State: string(fragment.State),
Comments: int(fragment.Comments.TotalCount),
CreatedAt: fragment.CreatedAt.Format(time.RFC3339),
UpdatedAt: fragment.UpdatedAt.Format(time.RFC3339),
User: &MinimalUser{
Login: string(fragment.Author.Login),
},
}
for _, label := range fragment.Labels.Nodes {
m.Labels = append(m.Labels, string(label.Name))
}
for _, fv := range fragment.IssueFieldValues.Nodes {
if mfv, ok := fragmentToMinimalFieldValue(fv); ok {
m.FieldValues = append(m.FieldValues, mfv)
}
}
return m
}
// fragmentToMinimalFieldValue flattens the union value fragment into a single
// {field, value} pair. Returns ok=false if the typename is unrecognised.
func fragmentToMinimalFieldValue(fv IssueFieldValueFragment) (MinimalFieldValue, bool) {
switch fv.TypeName {
case "IssueFieldDateValue":
return MinimalFieldValue{
Field: fv.DateValue.Field.Name(),
Value: string(fv.DateValue.Value),
}, true
case "IssueFieldNumberValue":
return MinimalFieldValue{
Field: fv.NumberValue.Field.Name(),
Value: strconv.FormatFloat(float64(fv.NumberValue.Value), 'f', -1, 64),
}, true
case "IssueFieldSingleSelectValue":
return MinimalFieldValue{
Field: fv.SingleSelectValue.Field.Name(),
Value: string(fv.SingleSelectValue.Value),
}, true
case "IssueFieldTextValue":
return MinimalFieldValue{
Field: fv.TextValue.Field.Name(),
Value: string(fv.TextValue.Value),
}, true
}
return MinimalFieldValue{}, false
}
func convertToMinimalIssuesResponse(fragment IssueQueryFragment) MinimalIssuesResponse {
minimalIssues := make([]MinimalIssue, 0, len(fragment.Nodes))
for _, issue := range fragment.Nodes {
minimalIssues = append(minimalIssues, fragmentToMinimalIssue(issue))
}
return MinimalIssuesResponse{
Issues: minimalIssues,
TotalCount: fragment.TotalCount,
PageInfo: MinimalPageInfo{
HasNextPage: bool(fragment.PageInfo.HasNextPage),
HasPreviousPage: bool(fragment.PageInfo.HasPreviousPage),
StartCursor: string(fragment.PageInfo.StartCursor),
EndCursor: string(fragment.PageInfo.EndCursor),
},
}
}
func convertToMinimalIssueComment(comment *github.IssueComment) MinimalIssueComment {
m := MinimalIssueComment{
ID: comment.GetID(),
Body: comment.GetBody(),
HTMLURL: comment.GetHTMLURL(),
User: convertToMinimalUser(comment.GetUser()),
AuthorAssociation: comment.GetAuthorAssociation(),
}
if comment.CreatedAt != nil {
m.CreatedAt = comment.CreatedAt.Format(time.RFC3339)
}
if comment.UpdatedAt != nil {
m.UpdatedAt = comment.UpdatedAt.Format(time.RFC3339)
}
if r := comment.Reactions; r != nil {
m.Reactions = &MinimalReactions{
TotalCount: r.GetTotalCount(),
PlusOne: r.GetPlusOne(),
MinusOne: r.GetMinusOne(),
Laugh: r.GetLaugh(),
Confused: r.GetConfused(),
Heart: r.GetHeart(),
Hooray: r.GetHooray(),
Rocket: r.GetRocket(),
Eyes: r.GetEyes(),
}
}
return m
}
func convertToMinimalFileContentResponse(resp *github.RepositoryContentResponse) MinimalFileContentResponse {
m := MinimalFileContentResponse{}
if resp == nil {
return m
}
if c := resp.Content; c != nil {
m.Content = &MinimalFileContent{
Name: c.GetName(),
Path: c.GetPath(),
SHA: c.GetSHA(),
Size: c.GetSize(),
HTMLURL: c.GetHTMLURL(),
}
}
m.Commit = &MinimalFileCommit{
SHA: resp.Commit.GetSHA(),
Message: resp.Commit.GetMessage(),
HTMLURL: resp.Commit.GetHTMLURL(),
}
if author := resp.Commit.Author; author != nil {
m.Commit.Author = &MinimalCommitAuthor{
Name: author.GetName(),
Email: author.GetEmail(),
}
if author.Date != nil {
m.Commit.Author.Date = author.Date.Format(time.RFC3339)
}
}
return m
}
func convertToMinimalPullRequest(pr *github.PullRequest) MinimalPullRequest {
m := MinimalPullRequest{
Number: pr.GetNumber(),
Title: pr.GetTitle(),
Body: pr.GetBody(),
State: pr.GetState(),
Draft: pr.GetDraft(),
Merged: pr.GetMerged(),
MergeableState: pr.GetMergeableState(),
HTMLURL: pr.GetHTMLURL(),
User: convertToMinimalUser(pr.GetUser()),
Additions: pr.GetAdditions(),
Deletions: pr.GetDeletions(),
ChangedFiles: pr.GetChangedFiles(),
Commits: pr.GetCommits(),
Comments: pr.GetComments(),
}
if pr.CreatedAt != nil {
m.CreatedAt = pr.CreatedAt.Format(time.RFC3339)
}
if pr.UpdatedAt != nil {
m.UpdatedAt = pr.UpdatedAt.Format(time.RFC3339)
}
if pr.ClosedAt != nil {
m.ClosedAt = pr.ClosedAt.Format(time.RFC3339)
}
if pr.MergedAt != nil {
m.MergedAt = pr.MergedAt.Format(time.RFC3339)
}
for _, label := range pr.Labels {
if label != nil {
m.Labels = append(m.Labels, label.GetName())
}
}
for _, assignee := range pr.Assignees {
if assignee != nil {
m.Assignees = append(m.Assignees, assignee.GetLogin())
}
}
for _, reviewer := range pr.RequestedReviewers {
if reviewer != nil {
m.RequestedReviewers = append(m.RequestedReviewers, reviewer.GetLogin())
}
}
if mergedBy := pr.GetMergedBy(); mergedBy != nil {
m.MergedBy = mergedBy.GetLogin()
}
if head := pr.Head; head != nil {
m.Head = convertToMinimalPRBranch(head)
}
if base := pr.Base; base != nil {
m.Base = convertToMinimalPRBranch(base)
}
if milestone := pr.GetMilestone(); milestone != nil {
m.Milestone = milestone.GetTitle()
}
return m
}
func convertToMinimalPRBranch(branch *github.PullRequestBranch) *MinimalPRBranch {
if branch == nil {
return nil
}
b := &MinimalPRBranch{
Ref: branch.GetRef(),
SHA: branch.GetSHA(),
}
if repo := branch.GetRepo(); repo != nil {
b.Repo = &MinimalPRBranchRepo{
FullName: repo.GetFullName(),
Description: repo.GetDescription(),
}
}
return b
}
func convertToMinimalProject(fullProject *github.ProjectV2) *MinimalProject {
if fullProject == nil {
return nil
}
return &MinimalProject{
ID: github.Ptr(fullProject.GetID()),
NodeID: github.Ptr(fullProject.GetNodeID()),
Owner: convertToMinimalUser(fullProject.GetOwner()),
Creator: convertToMinimalUser(fullProject.GetCreator()),
Title: github.Ptr(fullProject.GetTitle()),
Description: github.Ptr(fullProject.GetDescription()),
Public: github.Ptr(fullProject.GetPublic()),
ClosedAt: github.Ptr(fullProject.GetClosedAt()),
CreatedAt: github.Ptr(fullProject.GetCreatedAt()),
UpdatedAt: github.Ptr(fullProject.GetUpdatedAt()),
DeletedAt: github.Ptr(fullProject.GetDeletedAt()),
Number: github.Ptr(fullProject.GetNumber()),
ShortDescription: github.Ptr(fullProject.GetShortDescription()),
DeletedBy: convertToMinimalUser(fullProject.GetDeletedBy()),
}
}
func convertToMinimalProjectItem(item *github.ProjectV2Item) MinimalProjectItem {
if item == nil {
return MinimalProjectItem{}
}
contentType := ""
if item.ContentType != nil {
contentType = string(*item.ContentType)
}
creator := ""
if item.Creator != nil {
creator = item.Creator.GetLogin()
}
return MinimalProjectItem{
ID: item.GetID(),
NodeID: item.GetNodeID(),
ContentType: contentType,
Content: convertToMinimalProjectItemContent(item.GetContent()),
Fields: convertToMinimalProjectItemFields(item.GetFields()),
ArchivedAt: formatProjectTimestamp(item.ArchivedAt),
CreatedAt: formatProjectTimestamp(item.CreatedAt),
UpdatedAt: formatProjectTimestamp(item.UpdatedAt),
Creator: creator,
}
}
func convertToMinimalProjectItemContent(content *github.ProjectV2ItemContent) *MinimalProjectItemContent {
if content == nil {
return nil
}
if issue := content.GetIssue(); issue != nil {
return convertIssueToMinimalProjectItemContent(issue)
}
if pr := content.GetPullRequest(); pr != nil {
return convertPullRequestToMinimalProjectItemContent(pr)
}
if draftIssue := content.GetDraftIssue(); draftIssue != nil {
return convertDraftIssueToMinimalProjectItemContent(draftIssue)
}
return nil
}
func convertIssueToMinimalProjectItemContent(issue *github.Issue) *MinimalProjectItemContent {
m := &MinimalProjectItemContent{
ID: issue.GetID(),
NodeID: issue.GetNodeID(),
Number: issue.GetNumber(),
Title: issue.GetTitle(),
State: issue.GetState(),
StateReason: issue.GetStateReason(),
HTMLURL: issue.GetHTMLURL(),
Repository: issueRepositoryFullName(issue),
Comments: issue.GetComments(),
Draft: issue.GetDraft(),
CreatedAt: formatProjectTimestamp(issue.CreatedAt),
UpdatedAt: formatProjectTimestamp(issue.UpdatedAt),
ClosedAt: formatProjectTimestamp(issue.ClosedAt),
}
if user := issue.GetUser(); user != nil {
m.Author = user.GetLogin()
}
for _, assignee := range issue.Assignees {
if assignee != nil {
m.Assignees = append(m.Assignees, assignee.GetLogin())
}
}
for _, label := range issue.Labels {
if label != nil {
m.Labels = append(m.Labels, label.GetName())
}
}
if milestone := issue.GetMilestone(); milestone != nil {
m.Milestone = milestone.GetTitle()
}
return m
}
func convertPullRequestToMinimalProjectItemContent(pr *github.PullRequest) *MinimalProjectItemContent {
m := &MinimalProjectItemContent{
ID: pr.GetID(),
NodeID: pr.GetNodeID(),
Number: pr.GetNumber(),
Title: pr.GetTitle(),
State: pr.GetState(),
HTMLURL: pr.GetHTMLURL(),
Repository: pullRequestRepositoryFullName(pr),
Comments: pr.GetComments(),
Draft: pr.GetDraft(),
Merged: pr.GetMerged(),
CreatedAt: formatProjectTimestamp(pr.CreatedAt),
UpdatedAt: formatProjectTimestamp(pr.UpdatedAt),
ClosedAt: formatProjectTimestamp(pr.ClosedAt),
MergedAt: formatProjectTimestamp(pr.MergedAt),
}
if user := pr.GetUser(); user != nil {
m.Author = user.GetLogin()
}
for _, assignee := range pr.Assignees {
if assignee != nil {
m.Assignees = append(m.Assignees, assignee.GetLogin())
}
}
for _, label := range pr.Labels {
if label != nil {
m.Labels = append(m.Labels, label.GetName())
}
}
if milestone := pr.GetMilestone(); milestone != nil {
m.Milestone = milestone.GetTitle()
}
return m
}
func convertDraftIssueToMinimalProjectItemContent(draftIssue *github.ProjectV2DraftIssue) *MinimalProjectItemContent {
m := &MinimalProjectItemContent{
ID: draftIssue.GetID(),
NodeID: draftIssue.GetNodeID(),
Title: draftIssue.GetTitle(),
CreatedAt: formatProjectTimestamp(draftIssue.CreatedAt),
UpdatedAt: formatProjectTimestamp(draftIssue.UpdatedAt),
}