1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "errors"
11 "fmt"
12 "net/http"
13 )
14
15
16 type UserMigration struct {
17 ID *int64 `json:"id,omitempty"`
18 GUID *string `json:"guid,omitempty"`
19
20
21
22
23
24
25 State *string `json:"state,omitempty"`
26
27
28 LockRepositories *bool `json:"lock_repositories,omitempty"`
29
30
31 ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
32 URL *string `json:"url,omitempty"`
33 CreatedAt *string `json:"created_at,omitempty"`
34 UpdatedAt *string `json:"updated_at,omitempty"`
35 Repositories []*Repository `json:"repositories,omitempty"`
36 }
37
38 func (m UserMigration) String() string {
39 return Stringify(m)
40 }
41
42
43 type UserMigrationOptions struct {
44
45
46 LockRepositories bool
47
48
49
50 ExcludeAttachments bool
51 }
52
53
54 type startUserMigration struct {
55
56 Repositories []string `json:"repositories,omitempty"`
57
58
59
60 LockRepositories *bool `json:"lock_repositories,omitempty"`
61
62
63
64 ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
65 }
66
67
68
69
70
71 func (s *MigrationService) StartUserMigration(ctx context.Context, repos []string, opts *UserMigrationOptions) (*UserMigration, *Response, error) {
72 u := "user/migrations"
73
74 body := &startUserMigration{Repositories: repos}
75 if opts != nil {
76 body.LockRepositories = Bool(opts.LockRepositories)
77 body.ExcludeAttachments = Bool(opts.ExcludeAttachments)
78 }
79
80 req, err := s.client.NewRequest("POST", u, body)
81 if err != nil {
82 return nil, nil, err
83 }
84
85
86 req.Header.Set("Accept", mediaTypeMigrationsPreview)
87
88 m := &UserMigration{}
89 resp, err := s.client.Do(ctx, req, m)
90 if err != nil {
91 return nil, resp, err
92 }
93
94 return m, resp, nil
95 }
96
97
98
99
100 func (s *MigrationService) ListUserMigrations(ctx context.Context) ([]*UserMigration, *Response, error) {
101 u := "user/migrations"
102
103 req, err := s.client.NewRequest("GET", u, nil)
104 if err != nil {
105 return nil, nil, err
106 }
107
108
109 req.Header.Set("Accept", mediaTypeMigrationsPreview)
110
111 var m []*UserMigration
112 resp, err := s.client.Do(ctx, req, &m)
113 if err != nil {
114 return nil, resp, err
115 }
116
117 return m, resp, nil
118 }
119
120
121
122
123
124 func (s *MigrationService) UserMigrationStatus(ctx context.Context, id int64) (*UserMigration, *Response, error) {
125 u := fmt.Sprintf("user/migrations/%v", id)
126
127 req, err := s.client.NewRequest("GET", u, nil)
128 if err != nil {
129 return nil, nil, err
130 }
131
132
133 req.Header.Set("Accept", mediaTypeMigrationsPreview)
134
135 m := &UserMigration{}
136 resp, err := s.client.Do(ctx, req, m)
137 if err != nil {
138 return nil, resp, err
139 }
140
141 return m, resp, nil
142 }
143
144
145
146
147
148 func (s *MigrationService) UserMigrationArchiveURL(ctx context.Context, id int64) (string, error) {
149 url := fmt.Sprintf("user/migrations/%v/archive", id)
150
151 req, err := s.client.NewRequest("GET", url, nil)
152 if err != nil {
153 return "", err
154 }
155
156
157 req.Header.Set("Accept", mediaTypeMigrationsPreview)
158
159 m := &UserMigration{}
160
161 var loc string
162 originalRedirect := s.client.client.CheckRedirect
163 s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
164 loc = req.URL.String()
165 return http.ErrUseLastResponse
166 }
167 defer func() {
168 s.client.client.CheckRedirect = originalRedirect
169 }()
170 resp, err := s.client.Do(ctx, req, m)
171 if err == nil {
172 return "", errors.New("expected redirect, none provided")
173 }
174 loc = resp.Header.Get("Location")
175 return loc, nil
176 }
177
178
179
180
181
182 func (s *MigrationService) DeleteUserMigration(ctx context.Context, id int64) (*Response, error) {
183 url := fmt.Sprintf("user/migrations/%v/archive", id)
184
185 req, err := s.client.NewRequest("DELETE", url, nil)
186 if err != nil {
187 return nil, err
188 }
189
190
191 req.Header.Set("Accept", mediaTypeMigrationsPreview)
192
193 return s.client.Do(ctx, req, nil)
194 }
195
196
197
198
199
200
201
202 func (s *MigrationService) UnlockUserRepo(ctx context.Context, id int64, repo string) (*Response, error) {
203 url := fmt.Sprintf("user/migrations/%v/repos/%v/lock", id, repo)
204
205 req, err := s.client.NewRequest("DELETE", url, nil)
206 if err != nil {
207 return nil, err
208 }
209
210
211 req.Header.Set("Accept", mediaTypeMigrationsPreview)
212
213 return s.client.Do(ctx, req, nil)
214 }
215
View as plain text