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