| 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/v60/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, 0) |
| 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", 0) |
| 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_ListByAuthenticatedUser(t *testing.T) { |
| 161 |
if !checkAuth("TestRepositories_ListByAuthenticatedUser") { |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
_, _, err := client.Repositories.ListByAuthenticatedUser(context.Background(), nil) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("Repositories.ListByAuthenticatedUser() returned error: %v", err) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestRepositories_ListByUser(t *testing.T) { |
| 172 |
_, _, err := client.Repositories.ListByUser(context.Background(), "google", nil) |
| 173 |
if err != nil { |
| 174 |
t.Fatalf("Repositories.ListByUser('google') returned error: %v", err) |
| 175 |
} |
| 176 |
|
| 177 |
opt := github.RepositoryListByUserOptions{Sort: "created"} |
| 178 |
repos, _, err := client.Repositories.ListByUser(context.Background(), "google", &opt) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("Repositories.List('google') with Sort opt returned error: %v", err) |
| 181 |
} |
| 182 |
for i, repo := range repos { |
| 183 |
if i > 0 && (*repos[i-1].CreatedAt).Time.Before((*repo.CreatedAt).Time) { |
| 184 |
t.Fatalf("Repositories.ListByUser('google') with default descending Sort returned incorrect order") |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestRepositories_DownloadReleaseAsset(t *testing.T) { |
| 190 |
if !checkAuth("TestRepositories_DownloadReleaseAsset") { |
| 191 |
return |
| 192 |
} |
| 193 |
|
| 194 |
rc, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "andersjanmyr", "goose", 484892, http.DefaultClient) |
| 195 |
if err != nil { |
| 196 |
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err) |
| 197 |
} |
| 198 |
defer func() { _ = rc.Close() }() |
| 199 |
_, err = io.Copy(io.Discard, rc) |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestRepositories_Autolinks(t *testing.T) { |
| 206 |
if !checkAuth("TestRepositories_Autolinks") { |
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
// create a random repository |
| 211 |
repo, err := createRandomTestRepository("", true) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("createRandomTestRepository returned error: %v", err) |
| 214 |
} |
| 215 |
|
| 216 |
opts := &github.AutolinkOptions{ |
| 217 |
KeyPrefix: github.String("TICKET-"), |
| 218 |
URLTemplate: github.String("https://example.com/TICKET?query=<num>"), |
| 219 |
IsAlphanumeric: github.Bool(false), |
| 220 |
} |
| 221 |
|
| 222 |
actionlink, _, err := client.Repositories.AddAutolink(context.Background(), *repo.Owner.Login, *repo.Name, opts) |
| 223 |
if err != nil { |
| 224 |
t.Fatalf("Repositories.AddAutolink() returned error: %v", err) |
| 225 |
} |
| 226 |
|
| 227 |
if !cmp.Equal(actionlink.KeyPrefix, opts.KeyPrefix) || |
| 228 |
!cmp.Equal(actionlink.URLTemplate, opts.URLTemplate) || |
| 229 |
!cmp.Equal(actionlink.IsAlphanumeric, opts.IsAlphanumeric) { |
| 230 |
t.Errorf("Repositories.AddAutolink() returned %+v, want %+v", actionlink, opts) |
| 231 |
} |
| 232 |
|
| 233 |
_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name) |
| 234 |
if err != nil { |
| 235 |
t.Fatalf("Repositories.Delete() returned error: %v", err) |
| 236 |
} |
| 237 |
} |