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