-
Notifications
You must be signed in to change notification settings - Fork 3
/
migrationStatusCmd.go
112 lines (90 loc) · 3.28 KB
/
migrationStatusCmd.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
package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"strings"
"github.com/gateixeira/gei-migration-helper/internal/github"
"github.com/spf13/cobra"
)
var migrationStatusCmd = &cobra.Command{
Use: "migration-status",
Short: "Check status for organization migration",
Run: func(cmd *cobra.Command, args []string) {
sourceOrg, _ := cmd.Flags().GetString(sourceOrgFlagName)
targetOrg, _ := cmd.Flags().GetString(targetOrgFlagName)
sourceToken, _ := cmd.Flags().GetString(sourceTokenFlagName)
targetToken, _ := cmd.Flags().GetString(targetTokenFlagName)
ctx := context.Background()
sourceGC, err := github.NewGitHubClient(ctx, slog.Default(), sourceToken)
if err != nil {
slog.Info("error initializing source GitHub Client", err)
os.Exit(1)
}
targetGC, err := github.NewGitHubClient(ctx, slog.Default(), targetToken)
if err != nil {
slog.Info("error initializing source GitHub Client", err)
os.Exit(1)
}
slog.Info(fmt.Sprintf("checking migration status from %s to %s", sourceOrg, targetOrg))
statusRepoName := "migration-status"
migrationRepository, _ := targetGC.GetRepository(ctx, statusRepoName, targetOrg)
if migrationRepository == nil {
slog.Info("there is no migration-status repository in the target organization, likely the migration has not been started")
os.Exit(0)
}
migrationIssue, _ := targetGC.GetIssue(ctx, targetOrg, statusRepoName, 1)
if migrationIssue != nil {
slog.Info(fmt.Sprintf("migration finished. Check https://github.com/%s/%s/issues/1 for details", targetOrg, statusRepoName))
os.Exit(0)
}
sourceRepositories, err := sourceGC.GetRepositories(ctx, sourceOrg)
if err != nil {
slog.Error("error fetching repositories from source organization")
os.Exit(1)
}
destinationRepositories, err := targetGC.GetRepositories(ctx, targetOrg)
if err != nil {
slog.Error("error fetching repositories from target organization")
os.Exit(1)
}
sourceRepos := make(map[string]bool)
for _, repo := range sourceRepositories {
sourceRepos[*repo.Name] = true
}
intersection := make(map[string]bool)
for _, repo := range destinationRepositories {
if sourceRepos[*repo.Name] {
intersection[*repo.Name] = true
}
}
slog.Info("=========================================================")
slog.Info("a migration is ongoing (or finished in error)", "from", sourceOrg, "to", targetOrg)
slog.Info("=========================================================")
slog.Info(fmt.Sprintf("%d/%d repositories are migrated", len(intersection), len(sourceRepositories)))
slog.Info("=========================================================")
slog.Info("migrated repositories:")
var migrated []string
for repo := range intersection {
migrated = append(migrated, repo)
}
slog.Info(strings.Join(migrated, ", "))
slog.Info("=========================================================")
slog.Info("repositories to be migrated:")
var toMigrate []string
for _, repo := range sourceRepositories {
if !intersection[*repo.Name] {
toMigrate = append(toMigrate, *repo.Name)
}
}
if len(toMigrate) == 0 {
slog.Info("no repositories to migrate. Finishing the last repository migration")
os.Exit(0)
}
slog.Info(strings.Join(toMigrate, ", "))
},
}
func init() {
rootCmd.AddCommand(migrationStatusCmd)
}