-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclient.go
More file actions
217 lines (179 loc) · 5.18 KB
/
client.go
File metadata and controls
217 lines (179 loc) · 5.18 KB
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"golang.org/x/oauth2"
forgejo "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2"
"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{} {
gitHostURLParsed := parseGitHostURL(gitHostURL, service)
switch service {
case "github":
return newGitHubClient(gitHostURLParsed)
case "gitlab":
return newGitLabClient(gitHostURLParsed)
case "bitbucket":
return newBitbucketClient(gitHostURLParsed)
case "forgejo":
return newForgejoClient(gitHostURLParsed)
default:
return nil
}
}
// parseGitHostURL parses the git host URL if provided
func parseGitHostURL(gitHostURL string, service string) *url.URL {
if len(gitHostURL) == 0 {
return nil
}
gitHostURLParsed, err := url.Parse(gitHostURL)
if err != nil {
log.Fatalf("Invalid git host URL: %s", gitHostURL)
}
// Only GitLab requires /api/v4/ to be appended
if service == "gitlab" {
api, _ := url.Parse("api/v4/")
return gitHostURLParsed.ResolveReference(api)
}
return gitHostURLParsed
}
// newGitHubClient creates a new GitHub client
func newGitHubClient(gitHostURLParsed *url.URL) *github.Client {
githubToken := getOrCreateGitHubToken()
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
}
// getOrCreateGitHubToken retrieves or creates a GitHub token
func getOrCreateGitHubToken() string {
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken != "" {
return githubToken
}
githubToken, err := getToken("GITHUB")
if err != nil {
githubToken = startOAuthFlow()
}
if githubToken == "" {
log.Fatal("GitHub token not available")
}
err = saveToken("GITHUB", githubToken)
if err != nil {
log.Fatal("Error saving token")
}
return githubToken
}
// newGitLabClient creates a new GitLab client
func newGitLabClient(gitHostURLParsed *url.URL) *gitlab.Client {
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
}
// newBitbucketClient creates a new Bitbucket client
func newBitbucketClient(gitHostURLParsed *url.URL) *bitbucket.Client {
bitbucketUsername := os.Getenv("BITBUCKET_USERNAME")
if bitbucketUsername == "" {
log.Fatal("BITBUCKET_USERNAME environment variable not set")
}
bitbucketPasswordOrToken := os.Getenv("BITBUCKET_TOKEN")
if bitbucketPasswordOrToken == "" {
bitbucketPasswordOrToken = os.Getenv("BITBUCKET_PASSWORD")
}
if bitbucketPasswordOrToken == "" {
log.Fatal("BITBUCKET_TOKEN or BITBUCKET_PASSWORD environment variable not set")
}
gitHostToken = bitbucketPasswordOrToken
client := bitbucket.NewBasicAuth(bitbucketUsername, bitbucketPasswordOrToken)
if gitHostURLParsed != nil {
client.SetApiBaseURL(*gitHostURLParsed)
}
return client
}
// newForgejoClient creates a new Forgejo client.
func newForgejoClient(gitHostURLParsed *url.URL) *forgejo.Client {
forgejoToken := os.Getenv("FORGEJO_TOKEN")
if forgejoToken == "" {
log.Fatal("FORGEJO_TOKEN environment variable not set")
}
url := "https://" + knownServices["forgejo"]
if gitHostURLParsed != nil {
url = gitHostURLParsed.String()
}
gitHostToken = forgejoToken
log.Println("Creating forgejo client", url)
client, err := forgejo.NewClient(url, forgejo.SetToken(forgejoToken), forgejo.SetForgejoVersion(""))
if err != nil {
log.Fatalf("Error creating forgejo client: %v", err)
}
return client
}