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