...

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

Documentation: github.com/google/go-github/v47/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/v47/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  
    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  	// determine the owner to use if one wasn't specified
    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  	// create random repo name that does not currently exist
    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  				// found a non-existent repo, perfect
    72  				break
    73  			}
    74  
    75  			return nil, err
    76  		}
    77  	}
    78  
    79  	// create the repository
    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