...
1
2
3
4
5
6
7
8
9 package integration
10
11 import (
12 "context"
13 "fmt"
14 "math/rand"
15 "net/http"
16 "os"
17
18 "github.com/google/go-github/v47/github"
19 "golang.org/x/oauth2"
20 )
21
22 var (
23 client *github.Client
24
25
26
27 auth bool
28 )
29
30 func init() {
31 token := os.Getenv("GITHUB_AUTH_TOKEN")
32 if token == "" {
33 print("!!! No OAuth token. Some tests won't run. !!!\n\n")
34 client = github.NewClient(nil)
35 } else {
36 tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
37 &oauth2.Token{AccessToken: token},
38 ))
39 client = github.NewClient(tc)
40 auth = true
41 }
42 }
43
44 func checkAuth(name string) bool {
45 if !auth {
46 fmt.Printf("No auth - skipping portions of %v\n", name)
47 }
48 return auth
49 }
50
51 func createRandomTestRepository(owner string, autoinit bool) (*github.Repository, error) {
52
53 if owner == "" {
54 owner = os.Getenv("GITHUB_OWNER")
55 if owner == "" {
56 me, _, err := client.Users.Get(context.Background(), "")
57 if err != nil {
58 return nil, err
59 }
60 owner = *me.Login
61 }
62 }
63
64
65 var repoName string
66 for {
67 repoName = fmt.Sprintf("test-%d", rand.Int())
68 _, resp, err := client.Repositories.Get(context.Background(), owner, repoName)
69 if err != nil {
70 if resp.StatusCode == http.StatusNotFound {
71
72 break
73 }
74
75 return nil, err
76 }
77 }
78
79
80 repo, _, err := client.Repositories.Create(
81 context.Background(),
82 owner,
83 &github.Repository{
84 Name: github.String(repoName),
85 AutoInit: github.Bool(autoinit),
86 },
87 )
88 if err != nil {
89 return nil, err
90 }
91
92 return repo, nil
93 }
94
View as plain text