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