forked from amitsaha/gitbackup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
156 lines (133 loc) · 3.58 KB
/
client.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"golang.org/x/oauth2"
"github.com/google/go-github/v34/github"
bitbucket "github.com/ktrysmt/go-bitbucket"
gitlab "github.com/xanzy/go-gitlab"
"github.com/99designs/keyring"
"github.com/cli/oauth/device"
)
var keyringServiceName = "gitbackup-cli"
var gitbackupClientID = "7b56a77c7dfba0800524"
func startOAuthFlow() string {
clientID := gitbackupClientID
scopes := []string{"repo", "user", "admin:org"}
httpClient := http.DefaultClient
code, err := device.RequestCode(httpClient, "https://github.com/login/device/code", clientID, scopes)
if err != nil {
panic(err)
}
fmt.Printf("Copy code: %s\n", code.UserCode)
fmt.Printf("then open: %s\n", code.VerificationURI)
accessToken, err := device.PollToken(httpClient, "https://github.com/login/oauth/access_token", clientID, code)
if err != nil {
panic(err)
}
return accessToken.Token
}
func saveToken(service string, token string) error {
ring, err := keyring.Open(keyring.Config{
ServiceName: keyringServiceName,
})
if err != nil {
return err
}
err = ring.Set(keyring.Item{
Key: service + "_TOKEN",
Data: []byte(token),
})
return err
}
func getToken(service string) (string, error) {
ring, err := keyring.Open(keyring.Config{
ServiceName: keyringServiceName,
})
if err != nil {
return "", err
}
i, err := ring.Get(service + "_TOKEN")
if err != nil {
return "", err
}
return string(i.Data), nil
}
func newClient(service string, gitHostURL string) interface{} {
var gitHostURLParsed *url.URL
var err error
// If a git host URL has been passed in, we assume it's
// a gitlab installation
if len(gitHostURL) != 0 {
gitHostURLParsed, err = url.Parse(gitHostURL)
if err != nil {
log.Fatalf("Invalid gitlab URL: %s", gitHostURL)
}
api, _ := url.Parse("api/v4/")
gitHostURLParsed = gitHostURLParsed.ResolveReference(api)
}
if service == "github" {
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
githubToken, err = getToken("GITHUB")
if err != nil {
githubToken = startOAuthFlow()
}
if githubToken == "" {
log.Fatal("GitHub token not available")
} else {
err := saveToken("GITHUB", githubToken)
if err != nil {
log.Fatal("Error saving token")
}
}
}
gitHostToken = githubToken
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
if gitHostURLParsed != nil {
client.BaseURL = gitHostURLParsed
}
return client
}
if service == "gitlab" {
gitlabToken := os.Getenv("GITLAB_TOKEN")
if gitlabToken == "" {
log.Fatal("GITLAB_TOKEN environment variable not set")
}
gitHostToken = gitlabToken
var baseUrlOption gitlab.ClientOptionFunc
if gitHostURLParsed != nil {
baseUrlOption = gitlab.WithBaseURL(gitHostURLParsed.String())
}
client, err := gitlab.NewClient(gitlabToken, baseUrlOption)
if err != nil {
log.Fatalf("Error creating gitlab client: %v", err)
}
return client
}
if service == "bitbucket" {
bitbucketUsername := os.Getenv("BITBUCKET_USERNAME")
if bitbucketUsername == "" {
log.Fatal("BITBUCKET_USERNAME environment variable not set")
}
bitbucketPassword := os.Getenv("BITBUCKET_PASSWORD")
if bitbucketPassword == "" {
log.Fatal("BITBUCKET_PASSWORD environment variable not set")
}
gitHostToken = bitbucketPassword
client := bitbucket.NewBasicAuth(bitbucketUsername, bitbucketPassword)
if gitHostURLParsed != nil {
client.SetApiBaseURL(gitHostURLParsed.String())
}
return client
}
return nil
}