-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
199 lines (175 loc) · 4.27 KB
/
Copy pathtypes.go
File metadata and controls
199 lines (175 loc) · 4.27 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
package gitcode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
type Timestamp struct {
time.Time
}
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if s == "" {
return nil
}
tt, err := time.Parse(time.RFC3339, s)
if err != nil {
return err
}
t.Time = tt
return nil
}
type NullableTime struct {
time.Time
Valid bool
}
func (nt *NullableTime) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if s == "" {
nt.Valid = false
return nil
}
tt, err := time.Parse(time.RFC3339, s)
if err != nil {
return err
}
nt.Time = tt
nt.Valid = true
return nil
}
type FlexInt int
func (fi *FlexInt) UnmarshalJSON(data []byte) error {
var n int
if err := json.Unmarshal(data, &n); err == nil {
*fi = FlexInt(n)
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
parsed, err := strconv.Atoi(s)
if err != nil {
return err
}
*fi = FlexInt(parsed)
return nil
}
type FlexString string
func (fs *FlexString) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err == nil {
*fs = FlexString(s)
return nil
}
var n int64
if err := json.Unmarshal(data, &n); err != nil {
return err
}
*fs = FlexString(strconv.FormatInt(n, 10))
return nil
}
type Error struct {
Message string `json:"message"`
Errors []struct {
Resource string `json:"resource"`
Field string `json:"field"`
Code string `json:"code"`
} `json:"errors"`
}
func (e *Error) Error() string {
return e.Message
}
type SearchOptions struct {
ListOptions
Query string `json:"q"`
Order string `json:"order,omitempty"`
}
type SearchResult struct {
TotalCount int `json:"total_count"`
Items []*Repository `json:"items"`
}
type Notification struct {
ID string `json:"id"`
Reason string `json:"reason"`
Unread bool `json:"unread"`
Subject struct {
Title string `json:"title"`
URL string `json:"url"`
Type string `json:"type"`
} `json:"subject"`
CreatedAt time.Time `json:"created_at"`
}
type Star struct {
StarredAt time.Time `json:"starred_at"`
}
type Member struct {
ID string `json:"id"`
Login string `json:"login"`
Role string `json:"role"`
}
type Organization struct {
ID int64 `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
Description string `json:"description"`
AvatarURL string `json:"avatar_url"`
}
func (c *Client) ListOrganizations(ctx context.Context) ([]*Organization, error) {
var orgs []*Organization
err := c.doRequest(ctx, http.MethodGet, "/user/orgs", nil, &orgs)
if err != nil {
return nil, fmt.Errorf("ListOrganizations not supported by this GitCode instance: %w", err)
}
return orgs, nil
}
func (c *Client) GetOrganization(ctx context.Context, org string) (*Organization, error) {
var o Organization
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/orgs/%s", org), nil, &o)
if err != nil {
return nil, err
}
return &o, nil
}
func (c *Client) ListOrganizationMembers(ctx context.Context, org string) ([]*Member, error) {
var members []*Member
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/orgs/%s/members", org), nil, &members)
if err != nil {
return nil, err
}
return members, nil
}
func (c *Client) ListNotifications(ctx context.Context) ([]*Notification, error) {
var notifications []*Notification
err := c.doRequest(ctx, http.MethodGet, "/notifications", nil, ¬ifications)
if err != nil {
return nil, err
}
return notifications, nil
}
func (c *Client) StarRepository(ctx context.Context, owner, repo string) error {
return c.doRequest(ctx, http.MethodPut, fmt.Sprintf("/user/starred/%s/%s", owner, repo), nil, nil)
}
func (c *Client) UnstarRepository(ctx context.Context, owner, repo string) error {
return c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/user/starred/%s/%s", owner, repo), nil, nil)
}
func (c *Client) IsRepositoryStarred(ctx context.Context, owner, repo string) (bool, error) {
_, err := c.doRawRequest(ctx, http.MethodGet, fmt.Sprintf("/user/starred/%s/%s", owner, repo))
if err != nil {
if strings.Contains(err.Error(), "404") {
return false, nil
}
return false, err
}
return true, nil
}