-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathuser_data.go
300 lines (257 loc) · 8.02 KB
/
user_data.go
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
package main
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/google/go-github/v34/github"
)
// Using vars insted of const, since we cannot take & of a const
// For now, these are all specific to github
var (
migrationStatePending = "pending"
//migrationStateExporting = "exporting"
migrationStateExported = "exported"
migrationStateFailed = "failed"
orgRoleMember = "member"
orgRoleMaintainer = "maintainer"
orgRoleAdmin = "admin"
)
func getLocalMigrationFilepath(backupDir string, migrationID int64) string {
return path.Join(backupDir, fmt.Sprintf("user-migration-%d.tar.gz", migrationID))
}
func getLocalOrgMigrationFilepath(backupDir, org string, migrationID int64) string {
return path.Join(backupDir, fmt.Sprintf("%s-migration-%d.tar.gz", org, migrationID))
}
func createGithubUserMigration(ctx context.Context, client interface{}, repos []*Repository, retry bool, maxNumRetries int) (*github.UserMigration, error) {
var m *github.UserMigration
var err error
var resp *github.Response
migrationOpts := github.UserMigrationOptions{
LockRepositories: false,
ExcludeAttachments: false,
}
var repoPaths []string
for _, repo := range repos {
repoPaths = append(repoPaths, fmt.Sprintf("%s/%s", repo.Namespace, repo.Name))
}
numAttempts := 1
if retry {
numAttempts += maxNumRetries
}
var errResponse []byte
for i := 1; i <= numAttempts; i++ {
m, resp, err = client.(*github.Client).Migrations.StartUserMigration(ctx, repoPaths, &migrationOpts)
if err == nil {
return m, nil
}
if resp != nil {
errResponse, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
log.Printf("Attempt #%d: Error creating user migration: %v", i, string(errResponse))
}
return m, err
}
func createGithubOrgMigration(ctx context.Context, client interface{}, org string, repos []*Repository) (*github.Migration, error) {
migrationOpts := github.MigrationOptions{
LockRepositories: false,
ExcludeAttachments: false,
}
var repoPaths []string
for _, repo := range repos {
repoPaths = append(repoPaths, fmt.Sprintf("%s/%s", repo.Namespace, repo.Name))
}
m, resp, err := client.(*github.Client).Migrations.StartMigration(ctx, org, repoPaths, &migrationOpts)
if err != nil {
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
log.Printf("%v", string(data))
}
return m, err
}
func downloadGithubUserMigrationData(
ctx context.Context, client interface{}, backupDir string, id *int64, migrationStatePollingDuration time.Duration,
) error {
var ms *github.UserMigration
ms, _, err := client.(*github.Client).Migrations.UserMigrationStatus(ctx, *id)
if err != nil {
return err
}
for {
switch *ms.State {
case migrationStateFailed:
return errors.New("migration failed")
case migrationStateExported:
archiveURL, err := client.(*github.Client).Migrations.UserMigrationArchiveURL(ctx, *ms.ID)
if err != nil {
return err
}
archiveFilepath := getLocalMigrationFilepath(backupDir, *ms.ID)
log.Printf("Downloading file to: %s\n", archiveFilepath)
resp, err := http.Get(archiveURL)
if err != nil {
return fmt.Errorf("error downloading archive:%v", err)
}
defer resp.Body.Close()
out, err := os.Create(archiveFilepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
default:
log.Printf("Waiting for migration state to be exported: %s\n", *ms.State)
time.Sleep(migrationStatePollingDuration)
ms, _, err = client.(*github.Client).Migrations.UserMigrationStatus(ctx, *ms.ID)
if err != nil {
return err
}
}
}
}
func downloadGithubOrgMigrationData(
ctx context.Context, client interface{}, org string, backupDir string, id *int64, migrationStatePollingDuration time.Duration,
) error {
var ms *github.Migration
ms, _, err := client.(*github.Client).Migrations.MigrationStatus(ctx, org, *id)
if err != nil {
return err
}
for {
switch *ms.State {
case migrationStateFailed:
return errors.New("org migration failed")
case migrationStateExported:
archiveURL, err := client.(*github.Client).Migrations.MigrationArchiveURL(ctx, org, *ms.ID)
if err != nil {
return err
}
archiveFilepath := getLocalOrgMigrationFilepath(backupDir, org, *ms.ID)
log.Printf("Downloading file to: %s\n", archiveFilepath)
resp, err := http.Get(archiveURL)
if err != nil {
return fmt.Errorf("error downloading archive:%v", err)
}
defer resp.Body.Close()
out, err := os.Create(archiveFilepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
default:
log.Printf("Waiting for migration state to be exported: %s\n", *ms.State)
time.Sleep(migrationStatePollingDuration)
ms, _, err = client.(*github.Client).Migrations.MigrationStatus(ctx, org, *ms.ID)
if err != nil {
return err
}
}
}
}
// ListGithubUserMigrationsResult type is for listing migration result
type ListGithubUserMigrationsResult struct {
GUID *string `json:"guid"`
ID *int64 `json:"id"`
State *string `json:"state"`
}
// List Github user migrations
func getGithubUserMigrations(client interface{}) ([]ListGithubUserMigrationsResult, error) {
ctx := context.Background()
migrations, _, err := client.(*github.Client).Migrations.ListUserMigrations(ctx)
if err != nil {
return nil, err
}
var result []ListGithubUserMigrationsResult
for _, m := range migrations {
r := ListGithubUserMigrationsResult{}
r.GUID = m.GUID
r.ID = m.ID
r.State = m.State
result = append(result, r)
}
return result, nil
}
// GetGithubUserMigration to Get the status of a migration
func GetGithubUserMigration(client interface{}, id *int64) (*github.UserMigration, error) {
ctx := context.Background()
ms, _, err := client.(*github.Client).Migrations.UserMigrationStatus(ctx, *id)
return ms, err
}
// GithubUserMigrationDeleteResult is a type for deletion result
type GithubUserMigrationDeleteResult struct {
GhStatusCode int `json:"status_code"`
GhResponseBody string `json:"mesage"`
}
// DeleteGithubUserMigration deletes an existing migration
func DeleteGithubUserMigration(id *int64) GithubUserMigrationDeleteResult {
client := newClient("github", "https://github.com")
ctx := context.Background()
response, err := client.(*github.Client).Migrations.DeleteUserMigration(ctx, *id)
result := GithubUserMigrationDeleteResult{}
result.GhStatusCode = response.StatusCode
if err != nil {
result.GhResponseBody = err.Error()
return result
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
result.GhResponseBody = string(data)
return result
}
func getGithubUserOwnedOrgs(ctx context.Context, client interface{}) ([]*github.Organization, error) {
var ownedOrgs []*github.Organization
opts := github.ListOrgMembershipsOptions{State: "active"}
mShips, _, err := client.(*github.Client).Organizations.ListOrgMemberships(ctx, &opts)
if err != nil {
return nil, err
}
for _, m := range mShips {
if *m.Role == orgRoleAdmin {
ownedOrgs = append(ownedOrgs, m.Organization)
}
}
return ownedOrgs, nil
}
func getGithubOrgRepositories(ctx context.Context, client interface{}, o *github.Organization) ([]*Repository, error) {
var repositories []*Repository
var cloneURL string
// TODO: Allow customization for org repo types
options := github.RepositoryListByOrgOptions{}
for {
// Login seems to be the safer attribute to use than organization Name
repos, resp, err := client.(*github.Client).Repositories.ListByOrg(ctx, *o.Login, &options)
if err != nil {
return nil, err
}
for _, repo := range repos {
namespace := strings.Split(*repo.FullName, "/")[0]
if useHTTPSClone != nil && *useHTTPSClone {
cloneURL = *repo.CloneURL
} else {
cloneURL = *repo.SSHURL
}
repositories = append(repositories, &Repository{CloneURL: cloneURL, Name: *repo.Name, Namespace: namespace, Private: *repo.Private})
}
if resp.NextPage == 0 {
break
}
options.ListOptions.Page = resp.NextPage
}
return repositories, nil
}