...

Source file src/github.com/google/go-github/v55/test/integration/repos_test.go

Documentation: github.com/google/go-github/v55/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  	"io"
    14  	"net/http"
    15  	"testing"
    16  
    17  	"github.com/google/go-cmp/cmp"
    18  	"github.com/google/go-github/v55/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  		BlockCreations:   github.Bool(false),
   117  		LockBranch:       github.Bool(false),
   118  		AllowForkSyncing: github.Bool(false),
   119  	}
   120  
   121  	protection, _, err := client.Repositories.UpdateBranchProtection(context.Background(), *repo.Owner.Login, *repo.Name, "master", protectionRequest)
   122  	if err != nil {
   123  		t.Fatalf("Repositories.UpdateBranchProtection() returned error: %v", err)
   124  	}
   125  
   126  	want := &github.Protection{
   127  		RequiredStatusChecks: &github.RequiredStatusChecks{
   128  			Strict:   true,
   129  			Contexts: []string{"continuous-integration"},
   130  		},
   131  		RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{
   132  			DismissStaleReviews:          true,
   133  			RequiredApprovingReviewCount: 0,
   134  		},
   135  		EnforceAdmins: &github.AdminEnforcement{
   136  			URL:     github.String("https://api.github.com/repos/" + *repo.Owner.Login + "/" + *repo.Name + "/branches/master/protection/enforce_admins"),
   137  			Enabled: true,
   138  		},
   139  		Restrictions: nil,
   140  		BlockCreations: &github.BlockCreations{
   141  			Enabled: github.Bool(false),
   142  		},
   143  		LockBranch: &github.LockBranch{
   144  			Enabled: github.Bool(false),
   145  		},
   146  		AllowForkSyncing: &github.AllowForkSyncing{
   147  			Enabled: github.Bool(false),
   148  		},
   149  	}
   150  	if !cmp.Equal(protection, want) {
   151  		t.Errorf("Repositories.UpdateBranchProtection() returned %+v, want %+v", protection, want)
   152  	}
   153  
   154  	_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
   155  	if err != nil {
   156  		t.Fatalf("Repositories.Delete() returned error: %v", err)
   157  	}
   158  }
   159  
   160  func TestRepositories_List(t *testing.T) {
   161  	if !checkAuth("TestRepositories_List") {
   162  		return
   163  	}
   164  
   165  	_, _, err := client.Repositories.List(context.Background(), "", nil)
   166  	if err != nil {
   167  		t.Fatalf("Repositories.List('') returned error: %v", err)
   168  	}
   169  
   170  	_, _, err = client.Repositories.List(context.Background(), "google", nil)
   171  	if err != nil {
   172  		t.Fatalf("Repositories.List('google') returned error: %v", err)
   173  	}
   174  
   175  	opt := github.RepositoryListOptions{Sort: "created"}
   176  	repos, _, err := client.Repositories.List(context.Background(), "google", &opt)
   177  	if err != nil {
   178  		t.Fatalf("Repositories.List('google') with Sort opt returned error: %v", err)
   179  	}
   180  	for i, repo := range repos {
   181  		if i > 0 && (*repos[i-1].CreatedAt).Time.Before((*repo.CreatedAt).Time) {
   182  			t.Fatalf("Repositories.List('google') with default descending Sort returned incorrect order")
   183  		}
   184  	}
   185  }
   186  
   187  func TestRepositories_DownloadReleaseAsset(t *testing.T) {
   188  	if !checkAuth("TestRepositories_DownloadReleaseAsset") {
   189  		return
   190  	}
   191  
   192  	rc, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "andersjanmyr", "goose", 484892, http.DefaultClient)
   193  	if err != nil {
   194  		t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
   195  	}
   196  	defer func() { _ = rc.Close() }()
   197  	_, err = io.Copy(io.Discard, rc)
   198  	if err != nil {
   199  		t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
   200  	}
   201  }
   202  
   203  func TestRepositories_Autolinks(t *testing.T) {
   204  	if !checkAuth("TestRepositories_Autolinks") {
   205  		return
   206  	}
   207  
   208  	// create a random repository
   209  	repo, err := createRandomTestRepository("", true)
   210  	if err != nil {
   211  		t.Fatalf("createRandomTestRepository returned error: %v", err)
   212  	}
   213  
   214  	opts := &github.AutolinkOptions{
   215  		KeyPrefix:      github.String("TICKET-"),
   216  		URLTemplate:    github.String("https://example.com/TICKET?query=<num>"),
   217  		IsAlphanumeric: github.Bool(false),
   218  	}
   219  
   220  	actionlink, _, err := client.Repositories.AddAutolink(context.Background(), *repo.Owner.Login, *repo.Name, opts)
   221  	if err != nil {
   222  		t.Fatalf("Repositories.AddAutolink() returned error: %v", err)
   223  	}
   224  
   225  	if !cmp.Equal(actionlink.KeyPrefix, opts.KeyPrefix) ||
   226  		!cmp.Equal(actionlink.URLTemplate, opts.URLTemplate) ||
   227  		!cmp.Equal(actionlink.IsAlphanumeric, opts.IsAlphanumeric) {
   228  		t.Errorf("Repositories.AddAutolink() returned %+v, want %+v", actionlink, opts)
   229  	}
   230  
   231  	_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
   232  	if err != nil {
   233  		t.Fatalf("Repositories.Delete() returned error: %v", err)
   234  	}
   235  }
   236  

View as plain text