github.com/google/go-github/v71@v71.0.0/test/integration/github_test.go (about) 1 // Copyright 2014 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 //go:build integration 7 8 package integration 9 10 import ( 11 "context" 12 "fmt" 13 "math/rand" 14 "net/http" 15 "os" 16 17 "github.com/google/go-github/v71/github" 18 ) 19 20 var ( 21 client *github.Client 22 23 // auth indicates whether tests are being run with an OAuth token. 24 // Tests can use this flag to skip certain tests when run without auth. 25 auth bool 26 ) 27 28 func init() { 29 token := os.Getenv("GITHUB_AUTH_TOKEN") 30 if token == "" { 31 fmt.Print("!!! No OAuth token. Some tests won't run. !!!\n\n") 32 client = github.NewClient(nil) 33 } else { 34 client = github.NewClient(nil).WithAuthToken(token) 35 auth = true 36 } 37 } 38 39 func checkAuth(name string) bool { 40 if !auth { 41 fmt.Printf("No auth - skipping portions of %v\n", name) 42 } 43 return auth 44 } 45 46 func createRandomTestRepository(owner string, autoinit bool) (*github.Repository, error) { 47 // determine the owner to use if one wasn't specified 48 if owner == "" { 49 owner = os.Getenv("GITHUB_OWNER") 50 if owner == "" { 51 me, _, err := client.Users.Get(context.Background(), "") 52 if err != nil { 53 return nil, err 54 } 55 owner = *me.Login 56 } 57 } 58 59 // create random repo name that does not currently exist 60 var repoName string 61 for { 62 repoName = fmt.Sprintf("test-%d", rand.Int()) 63 _, resp, err := client.Repositories.Get(context.Background(), owner, repoName) 64 if err != nil { 65 if resp.StatusCode == http.StatusNotFound { 66 // found a non-existent repo, perfect 67 break 68 } 69 70 return nil, err 71 } 72 } 73 74 // create the repository 75 repo, _, err := client.Repositories.Create( 76 context.Background(), 77 owner, 78 &github.Repository{ 79 Name: github.Ptr(repoName), 80 AutoInit: github.Ptr(autoinit), 81 }, 82 ) 83 if err != nil { 84 return nil, err 85 } 86 87 return repo, nil 88 }