forked from itchyny/github-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (96 loc) · 2.58 KB
/
main.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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/itchyny/github-migrator/github"
"github.com/itchyny/github-migrator/migrator"
"github.com/itchyny/github-migrator/repo"
)
const name = "github-migrator"
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
os.Exit(1)
}
}
func run(args []string) error {
if len(args) != 2 {
return fmt.Errorf("usage: %s <source> <target>", name)
}
mig, err := createMigrator(args[0], args[1])
if err != nil {
return err
}
return mig.Migrate()
}
func createGitHubClient(tokenEnv, endpointEnv, proxyEnv string) (github.Client, error) {
token := os.Getenv(tokenEnv)
if token == "" {
return nil, fmt.Errorf("GitHub token not found (specify %s)", tokenEnv)
}
endpoint := os.Getenv(endpointEnv)
if endpoint == "" {
endpoint = "https://api.github.com"
}
proxy := os.Getenv(proxyEnv)
cli := github.New(
token, endpoint, proxy,
github.ClientLogger(
github.NewLogger(
github.LoggerPreRequest(func(req *http.Request) {
fmt.Printf("===> %s: %s\n", req.Method, req.URL)
}),
github.LoggerPostRequest(func(res *http.Response, err error) {
if err != nil {
var suffix string
if res != nil {
suffix = fmt.Sprintf(": %s: %s", res.Request.Method, res.Request.URL)
}
fmt.Printf("<=== %s%s\n", err, suffix)
return
}
fmt.Printf("<=== %s: %s: %s\n", res.Status, res.Request.Method, res.Request.URL)
}),
),
),
)
user, err := cli.GetLogin()
if err != nil {
return nil, fmt.Errorf("%s (or you may want to set %s)", err, endpointEnv)
}
fmt.Printf("[<>] login succeeded: %s\n", user.Login)
return cli, nil
}
func createMigrator(sourcePath, targetPath string) (migrator.Migrator, error) {
sourceCli, err := createGitHubClient(
"GITHUB_MIGRATOR_SOURCE_API_TOKEN",
"GITHUB_MIGRATOR_SOURCE_API_ENDPOINT",
"GITHUB_MIGRATOR_SOURCE_PROXY_URL",
)
if err != nil {
return nil, err
}
targetCli, err := createGitHubClient(
"GITHUB_MIGRATOR_TARGET_API_TOKEN",
"GITHUB_MIGRATOR_TARGET_API_ENDPOINT",
"GITHUB_MIGRATOR_TARGET_PROXY_URL",
)
if err != nil {
return nil, err
}
source := repo.New(sourceCli, sourcePath)
target := repo.New(targetCli, targetPath)
return migrator.New(source, target, createUserMapping()), nil
}
func createUserMapping() map[string]string {
m := make(map[string]string)
for _, src := range strings.Split(os.Getenv("GITHUB_MIGRATOR_USER_MAPPING"), ",") {
xs := strings.Split(strings.TrimSpace(src), ":")
if len(xs) == 2 && len(xs[0]) > 0 && len(xs[1]) > 0 {
m[xs[0]] = xs[1]
}
}
return m
}