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, opts *ListOptions) ([]*UserMigration, *Response, error) {
101 u := "user/migrations"
102 u, err := addOptions(u, opts)
103 if err != nil {
104 return nil, nil, err
105 }
106
107 req, err := s.client.NewRequest("GET", u, nil)
108 if err != nil {
109 return nil, nil, err
110 }
111
112
113 req.Header.Set("Accept", mediaTypeMigrationsPreview)
114
115 var m []*UserMigration
116 resp, err := s.client.Do(ctx, req, &m)
117 if err != nil {
118 return nil, resp, err
119 }
120
121 return m, resp, nil
122 }
123
124
125
126
127
128 func (s *MigrationService) UserMigrationStatus(ctx context.Context, id int64) (*UserMigration, *Response, error) {
129 u := fmt.Sprintf("user/migrations/%v", id)
130
131 req, err := s.client.NewRequest("GET", u, nil)
132 if err != nil {
133 return nil, nil, err
134 }
135
136
137 req.Header.Set("Accept", mediaTypeMigrationsPreview)
138
139 m := &UserMigration{}
140 resp, err := s.client.Do(ctx, req, m)
141 if err != nil {
142 return nil, resp, err
143 }
144
145 return m, resp, nil
146 }
147
148
149
150
151
152 func (s *MigrationService) UserMigrationArchiveURL(ctx context.Context, id int64) (string, error) {
153 url := fmt.Sprintf("user/migrations/%v/archive", id)
154
155 req, err := s.client.NewRequest("GET", url, nil)
156 if err != nil {
157 return "", err
158 }
159
160
161 req.Header.Set("Accept", mediaTypeMigrationsPreview)
162
163 m := &UserMigration{}
164
165 var loc string
166 originalRedirect := s.client.client.CheckRedirect
167 s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
168 loc = req.URL.String()
169 return http.ErrUseLastResponse
170 }
171 defer func() {
172 s.client.client.CheckRedirect = originalRedirect
173 }()
174 resp, err := s.client.Do(ctx, req, m)
175 if err == nil {
176 return "", errors.New("expected redirect, none provided")
177 }
178 loc = resp.Header.Get("Location")
179 return loc, nil
180 }
181
182
183
184
185
186 func (s *MigrationService) DeleteUserMigration(ctx context.Context, id int64) (*Response, error) {
187 url := fmt.Sprintf("user/migrations/%v/archive", id)
188
189 req, err := s.client.NewRequest("DELETE", url, nil)
190 if err != nil {
191 return nil, err
192 }
193
194
195 req.Header.Set("Accept", mediaTypeMigrationsPreview)
196
197 return s.client.Do(ctx, req, nil)
198 }
199
200
201
202
203
204
205
206 func (s *MigrationService) UnlockUserRepo(ctx context.Context, id int64, repo string) (*Response, error) {
207 url := fmt.Sprintf("user/migrations/%v/repos/%v/lock", id, repo)
208
209 req, err := s.client.NewRequest("DELETE", url, nil)
210 if err != nil {
211 return nil, err
212 }
213
214
215 req.Header.Set("Accept", mediaTypeMigrationsPreview)
216
217 return s.client.Do(ctx, req, nil)
218 }
219
View as plain text