...
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/v45/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 vars := []string{envKeyGitHubUsername, envKeyGitHubPassword, envKeyClientID, envKeyClientSecret}
45
46 for _, v := range vars {
47 value := os.Getenv(v)
48 if value == "" {
49 print("!!! " + fmt.Sprintf(msgEnvMissing, v) + " !!!\n\n")
50 }
51 }
52
53 }
54
55 func checkAuth(name string) bool {
56 if !auth {
57 fmt.Printf("No auth - skipping portions of %v\n", name)
58 }
59 return auth
60 }
61
62 func createRandomTestRepository(owner string, autoinit bool) (*github.Repository, error) {
63
64 var repoName string
65 for {
66 repoName = fmt.Sprintf("test-%d", rand.Int())
67 _, resp, err := client.Repositories.Get(context.Background(), owner, repoName)
68 if err != nil {
69 if resp.StatusCode == http.StatusNotFound {
70
71 break
72 }
73
74 return nil, err
75 }
76 }
77
78
79 repo, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{Name: github.String(repoName), AutoInit: github.Bool(autoinit)})
80 if err != nil {
81 return nil, err
82 }
83
84 return repo, nil
85 }
86
View as plain text