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