-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub.go
68 lines (57 loc) · 1.6 KB
/
github.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
// File contains functions used to create repositories on GitHub.
package main
import (
"context"
"fmt"
"github.com/google/go-github/v32/github"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)
func createGitHubRepo(name string) error {
ghUser := viper.Get("ghUser").(string)
ghOrg := viper.Get("ghOrg").(string)
ghToken := viper.Get("ghKey").(string)
private := false
description := ""
if ghOrg == "" {
fmt.Printf("Attempting to create %s in %s's GitHub\n", name, ghUser)
} else {
fmt.Printf("Attempting to create %s in the %s GitHub Org\n", name, ghOrg)
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
repo, resp, err := client.Repositories.Create(ctx, ghOrg, &github.Repository{
Name: &name,
Private: &private,
Description: &description,
})
// TODO: mirror GitLab logic for the same interaction steps
if err != nil && resp.StatusCode != 422 {
return err
} else if resp.StatusCode == 422 {
if ghOrg == "" {
fmt.Printf("%s already exists in this GitHub account\n", name)
repo, resp, err = client.Repositories.Get(ctx, ghUser, name)
if err != nil {
return err
}
} else {
fmt.Printf("%s already exists in this GitHub Org\n", name)
repo, resp, err = client.Repositories.Get(ctx, ghOrg, name)
if err != nil {
return err
}
}
} else {
fmt.Printf("%s created successfully at %s\n", repo.GetName(), repo.GetURL())
}
err = pushUpstream(name, "github", *repo.CloneURL, ghUser, ghToken)
if err != nil {
return err
}
return nil
}