github.com/google/go-github/v33@v33.0.0/test/integration/repos_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  // +build integration
     7  
     8  package integration
     9  
    10  import (
    11  	"context"
    12  	"io"
    13  	"io/ioutil"
    14  	"net/http"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/google/go-github/v33/github"
    19  )
    20  
    21  func TestRepositories_CRUD(t *testing.T) {
    22  	if !checkAuth("TestRepositories_CRUD") {
    23  		return
    24  	}
    25  
    26  	// get authenticated user
    27  	me, _, err := client.Users.Get(context.Background(), "")
    28  	if err != nil {
    29  		t.Fatalf("Users.Get('') returned error: %v", err)
    30  	}
    31  
    32  	repo, err := createRandomTestRepository(*me.Login, false)
    33  	if err != nil {
    34  		t.Fatalf("createRandomTestRepository returned error: %v", err)
    35  	}
    36  
    37  	// update the repository description
    38  	repo.Description = github.String("description")
    39  	repo.DefaultBranch = nil // FIXME: this shouldn't be necessary
    40  	_, _, err = client.Repositories.Edit(context.Background(), *repo.Owner.Login, *repo.Name, repo)
    41  	if err != nil {
    42  		t.Fatalf("Repositories.Edit() returned error: %v", err)
    43  	}
    44  
    45  	// delete the repository
    46  	_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
    47  	if err != nil {
    48  		t.Fatalf("Repositories.Delete() returned error: %v", err)
    49  	}
    50  
    51  	// verify that the repository was deleted
    52  	_, resp, err := client.Repositories.Get(context.Background(), *repo.Owner.Login, *repo.Name)
    53  	if err == nil {
    54  		t.Fatalf("Test repository still exists after deleting it.")
    55  	}
    56  	if err != nil && resp.StatusCode != http.StatusNotFound {
    57  		t.Fatalf("Repositories.Get() returned error: %v", err)
    58  	}
    59  }
    60  
    61  func TestRepositories_BranchesTags(t *testing.T) {
    62  	// branches
    63  	branches, _, err := client.Repositories.ListBranches(context.Background(), "git", "git", nil)
    64  	if err != nil {
    65  		t.Fatalf("Repositories.ListBranches() returned error: %v", err)
    66  	}
    67  
    68  	if len(branches) == 0 {
    69  		t.Fatalf("Repositories.ListBranches('git', 'git') returned no branches")
    70  	}
    71  
    72  	_, _, err = client.Repositories.GetBranch(context.Background(), "git", "git", *branches[0].Name)
    73  	if err != nil {
    74  		t.Fatalf("Repositories.GetBranch() returned error: %v", err)
    75  	}
    76  
    77  	// tags
    78  	tags, _, err := client.Repositories.ListTags(context.Background(), "git", "git", nil)
    79  	if err != nil {
    80  		t.Fatalf("Repositories.ListTags() returned error: %v", err)
    81  	}
    82  
    83  	if len(tags) == 0 {
    84  		t.Fatalf("Repositories.ListTags('git', 'git') returned no tags")
    85  	}
    86  }
    87  
    88  func TestRepositories_EditBranches(t *testing.T) {
    89  	if !checkAuth("TestRepositories_EditBranches") {
    90  		return
    91  	}
    92  
    93  	// get authenticated user
    94  	me, _, err := client.Users.Get(context.Background(), "")
    95  	if err != nil {
    96  		t.Fatalf("Users.Get('') returned error: %v", err)
    97  	}
    98  
    99  	repo, err := createRandomTestRepository(*me.Login, true)
   100  	if err != nil {
   101  		t.Fatalf("createRandomTestRepository returned error: %v", err)
   102  	}
   103  
   104  	branch, _, err := client.Repositories.GetBranch(context.Background(), *repo.Owner.Login, *repo.Name, "master")
   105  	if err != nil {
   106  		t.Fatalf("Repositories.GetBranch() returned error: %v", err)
   107  	}
   108  
   109  	if *branch.Protected {
   110  		t.Fatalf("Branch %v of repo %v is already protected", "master", *repo.Name)
   111  	}
   112  
   113  	protectionRequest := &github.ProtectionRequest{
   114  		RequiredStatusChecks: &github.RequiredStatusChecks{
   115  			Strict:   true,
   116  			Contexts: []string{"continuous-integration"},
   117  		},
   118  		RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
   119  			DismissStaleReviews: true,
   120  		},
   121  		EnforceAdmins: true,
   122  		// TODO: Only organization repositories can have users and team restrictions.
   123  		//       In order to be able to test these Restrictions, need to add support
   124  		//       for creating temporary organization repositories.
   125  		Restrictions: nil,
   126  	}
   127  
   128  	protection, _, err := client.Repositories.UpdateBranchProtection(context.Background(), *repo.Owner.Login, *repo.Name, "master", protectionRequest)
   129  	if err != nil {
   130  		t.Fatalf("Repositories.UpdateBranchProtection() returned error: %v", err)
   131  	}
   132  
   133  	want := &github.Protection{
   134  		RequiredStatusChecks: &github.RequiredStatusChecks{
   135  			Strict:   true,
   136  			Contexts: []string{"continuous-integration"},
   137  		},
   138  		RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{
   139  			DismissStaleReviews:          true,
   140  			RequiredApprovingReviewCount: 0,
   141  		},
   142  		EnforceAdmins: &github.AdminEnforcement{
   143  			URL:     github.String("https://api.github.com/repos/" + *repo.Owner.Login + "/" + *repo.Name + "/branches/master/protection/enforce_admins"),
   144  			Enabled: true,
   145  		},
   146  		Restrictions: nil,
   147  	}
   148  	if !reflect.DeepEqual(protection, want) {
   149  		t.Errorf("Repositories.UpdateBranchProtection() returned %+v, want %+v", protection, want)
   150  	}
   151  
   152  	_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
   153  	if err != nil {
   154  		t.Fatalf("Repositories.Delete() returned error: %v", err)
   155  	}
   156  }
   157  
   158  func TestRepositories_List(t *testing.T) {
   159  	if !checkAuth("TestRepositories_List") {
   160  		return
   161  	}
   162  
   163  	_, _, err := client.Repositories.List(context.Background(), "", nil)
   164  	if err != nil {
   165  		t.Fatalf("Repositories.List('') returned error: %v", err)
   166  	}
   167  
   168  	_, _, err = client.Repositories.List(context.Background(), "google", nil)
   169  	if err != nil {
   170  		t.Fatalf("Repositories.List('google') returned error: %v", err)
   171  	}
   172  
   173  	opt := github.RepositoryListOptions{Sort: "created"}
   174  	repos, _, err := client.Repositories.List(context.Background(), "google", &opt)
   175  	if err != nil {
   176  		t.Fatalf("Repositories.List('google') with Sort opt returned error: %v", err)
   177  	}
   178  	for i, repo := range repos {
   179  		if i > 0 && (*repos[i-1].CreatedAt).Time.Before((*repo.CreatedAt).Time) {
   180  			t.Fatalf("Repositories.List('google') with default descending Sort returned incorrect order")
   181  		}
   182  	}
   183  }
   184  
   185  func TestRepositories_DownloadReleaseAsset(t *testing.T) {
   186  	if !checkAuth("TestRepositories_DownloadReleaseAsset") {
   187  		return
   188  	}
   189  
   190  	rc, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "andersjanmyr", "goose", 484892, http.DefaultClient)
   191  	if err != nil {
   192  		t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
   193  	}
   194  	defer func() { _ = rc.Close() }()
   195  	_, err = io.Copy(ioutil.Discard, rc)
   196  	if err != nil {
   197  		t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
   198  	}
   199  }