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