...

Source file src/github.com/google/go-github/v45/test/integration/github_test.go

Documentation: github.com/google/go-github/v45/test/integration

     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  // +build integration
     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  	// auth indicates whether tests are being run with an OAuth token.
    26  	// Tests can use this flag to skip certain tests when run without auth.
    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  	// Environment variables required for Authorization integration tests
    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  	// create random repo name that does not currently exist
    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  				// found a non-existent repo, perfect
    71  				break
    72  			}
    73  
    74  			return nil, err
    75  		}
    76  	}
    77  
    78  	// create the repository
    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