Skip to content

Commit c71505f

Browse files
authored
Merge pull request #37 from shanep/clone-all-repos
Implementation for issue #4
2 parents 850fcae + 6db9e64 commit c71505f

File tree

7 files changed

+277
-21
lines changed

7 files changed

+277
-21
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
*.swp
2222

2323
vendor/
24+
25+
# Windows
26+
gh-classroom.exe

cmd/gh-classroom/accepted-assignments/accepted_assignments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewCmdAcceptedAssignments(f *cmdutil.Factory) *cobra.Command {
6363
return
6464
}
6565

66-
acceptedAssignments, err := classroom.ListAcceptedAssignments(client, assignmentId, page, perPage)
66+
acceptedAssignments, err := shared.ListAcceptedAssignments(client, assignmentId, page, perPage)
6767
if err != nil {
6868
log.Fatal(err)
6969
}

cmd/gh-classroom/clone/student-repos/student-repos.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func NewCmdStudentRepo(f *cmdutil.Factory) *cobra.Command {
2020
var directory string
2121
var page int
2222
var perPage int
23+
var getAll bool
2324

2425
cmd := &cobra.Command{
2526
Use: "student-repos",
@@ -51,8 +52,12 @@ func NewCmdStudentRepo(f *cmdutil.Factory) *cobra.Command {
5152
log.Fatal(err)
5253
}
5354
}
54-
55-
acceptedAssignmentList, err := classroom.ListAcceptedAssignments(client, assignmentId, page, perPage)
55+
var acceptedAssignmentList classroom.AcceptedAssignmentList
56+
if getAll {
57+
acceptedAssignmentList, err = shared.ListAllAcceptedAssignments(client, assignmentId, perPage)
58+
} else {
59+
acceptedAssignmentList, err = shared.ListAcceptedAssignments(client, assignmentId, page, perPage)
60+
}
5661

5762
if err != nil {
5863
log.Fatal(err)
@@ -97,7 +102,8 @@ func NewCmdStudentRepo(f *cmdutil.Factory) *cobra.Command {
97102
cmd.Flags().IntVarP(&assignmentId, "assignment-id", "a", 0, "ID of the assignment")
98103
cmd.Flags().StringVarP(&directory, "directory", "d", ".", "Directory to clone into")
99104
cmd.Flags().IntVar(&page, "page", 1, "Page number")
100-
cmd.Flags().IntVar(&perPage, "per-page", 30, "Number of accepted assignments per page")
105+
cmd.Flags().IntVar(&perPage, "per-page", 15, "Number of accepted assignments per page")
106+
cmd.Flags().BoolVar(&getAll, "all", true, "Clone All assignments by default")
101107

102108
return cmd
103109
}

cmd/gh-classroom/shared/shared.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,45 @@ func PromptForAssignment(client api.RESTClient, classroomId int) (assignment cla
9494

9595
return optionMap[answer.Assignment], nil
9696
}
97+
98+
func ListAcceptedAssignments(client api.RESTClient, assignmentID int, page int, perPage int) (classroom.AcceptedAssignmentList, error) {
99+
response, err := classroom.GetAssignmentList(client, assignmentID, page, perPage)
100+
if err != nil {
101+
return classroom.AcceptedAssignmentList{}, err
102+
}
103+
104+
if len(response) == 0 {
105+
return classroom.AcceptedAssignmentList{}, nil
106+
}
107+
acceptedAssignmentList := classroom.NewAcceptedAssignmentList(response)
108+
109+
return acceptedAssignmentList, nil
110+
}
111+
112+
func ListAllAcceptedAssignments(client api.RESTClient, assignmentID int, perPage int) (classroom.AcceptedAssignmentList, error) {
113+
var page = 1
114+
response, err := classroom.GetAssignmentList(client, assignmentID, page, perPage)
115+
if err != nil {
116+
return classroom.AcceptedAssignmentList{}, err
117+
}
118+
119+
if len(response) == 0 {
120+
return classroom.AcceptedAssignmentList{}, nil
121+
}
122+
123+
//keep calling getAssignmentList until we get them all
124+
var nextList []classroom.AcceptedAssignment
125+
for hasNext := true; hasNext; {
126+
page += 1
127+
nextList, err = classroom.GetAssignmentList(client, assignmentID, page, perPage)
128+
if err != nil {
129+
return classroom.AcceptedAssignmentList{}, err
130+
}
131+
hasNext = len(nextList) > 0
132+
response = append(response, nextList...)
133+
}
134+
135+
acceptedAssignmentList := classroom.NewAcceptedAssignmentList(response)
136+
137+
return acceptedAssignmentList, nil
138+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package shared
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cli/go-gh"
7+
"github.com/stretchr/testify/assert"
8+
"gopkg.in/h2non/gock.v1"
9+
)
10+
11+
func TestListAcceptedAssignments(t *testing.T) {
12+
t.Setenv("GITHUB_TOKEN", "999")
13+
defer gock.Off()
14+
15+
gock.New("https://api.github.com").
16+
Get("/assignments/1/accepted_assignments").
17+
Reply(200).
18+
JSON(`[{"id": 1,
19+
"assignment": {
20+
"id": 1,
21+
"title": "Assignment 1",
22+
"description": "This is the first assignment",
23+
"due_date": "2018-01-01",
24+
"classroom": {
25+
"id": 1,
26+
"name": "Classroom Name"
27+
},
28+
"starter_code_repository": {
29+
"id": 1,
30+
"full_name": "org1/starter-code-repo"
31+
}
32+
},
33+
"students": [{
34+
"id": 1,
35+
"login": "student1"
36+
}],
37+
"repository": {
38+
"id": 1,
39+
"full_name": "org1/student1-repo"
40+
}
41+
}]`)
42+
43+
gock.New("https://api.github.com").
44+
Path("/assignments/1/accepted_assignments").
45+
Reply(200).
46+
JSON(`[{"id": 2,
47+
"assignment": {
48+
"id": 2,
49+
"title": "Assignment 1",
50+
"description": "This is the first assignment",
51+
"due_date": "2018-01-01",
52+
"classroom": {
53+
"id": 1,
54+
"name": "Classroom Name"
55+
},
56+
"starter_code_repository": {
57+
"id": 1,
58+
"full_name": "org1/starter-code-repo"
59+
}
60+
},
61+
"students": [{
62+
"id": 2,
63+
"login": "student2"
64+
}],
65+
"repository": {
66+
"id": 2,
67+
"full_name": "org1/student2-repo"
68+
}
69+
}]`)
70+
71+
client, err := gh.RESTClient(nil)
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
//Ask for page 1 and 1 per page
77+
actual, err := ListAcceptedAssignments(client, 1, 1, 1)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
assert.Equal(t, 1, actual.Count)
82+
assert.Equal(t, 1, actual.AcceptedAssignments[0].Id)
83+
assert.Equal(t, "org1/student1-repo", actual.AcceptedAssignments[0].Repository.FullName)
84+
assert.Equal(t, "student1", actual.AcceptedAssignments[0].Students[0].Login)
85+
86+
//Ask for page 2 and 1 per page
87+
actual, err = ListAcceptedAssignments(client, 1, 2, 1)
88+
if err != nil {
89+
t.Fatal(err)
90+
}
91+
assert.Equal(t, 1, actual.Count)
92+
assert.Equal(t, 2, actual.AcceptedAssignments[0].Id)
93+
assert.Equal(t, "org1/student2-repo", actual.AcceptedAssignments[0].Repository.FullName)
94+
assert.Equal(t, "student2", actual.AcceptedAssignments[0].Students[0].Login)
95+
96+
}
97+
98+
func TestListAllAcceptedAssignments(t *testing.T) {
99+
t.Setenv("GITHUB_TOKEN", "999")
100+
defer gock.Off()
101+
102+
gock.New("https://api.github.com").
103+
Get("/assignments/1/accepted_assignments").
104+
Reply(200).
105+
JSON(`[{"id": 1,
106+
"assignment": {
107+
"id": 1,
108+
"title": "Assignment 1",
109+
"description": "This is the first assignment",
110+
"due_date": "2018-01-01",
111+
"classroom": {
112+
"id": 1,
113+
"name": "Classroom Name"
114+
},
115+
"starter_code_repository": {
116+
"id": 1,
117+
"full_name": "org1/starter-code-repo"
118+
}
119+
},
120+
"students": [{
121+
"id": 1,
122+
"login": "student1"
123+
}],
124+
"repository": {
125+
"id": 1,
126+
"full_name": "org1/student1-repo"
127+
}
128+
}]`)
129+
130+
gock.New("https://api.github.com").
131+
Path("/assignments/1/accepted_assignments").
132+
Reply(200).
133+
JSON(`[{"id": 2,
134+
"assignment": {
135+
"id": 2,
136+
"title": "Assignment 1",
137+
"description": "This is the first assignment",
138+
"due_date": "2018-01-01",
139+
"classroom": {
140+
"id": 1,
141+
"name": "Classroom Name"
142+
},
143+
"starter_code_repository": {
144+
"id": 1,
145+
"full_name": "org1/starter-code-repo"
146+
}
147+
},
148+
"students": [{
149+
"id": 2,
150+
"login": "student2"
151+
}],
152+
"repository": {
153+
"id": 2,
154+
"full_name": "org1/student2-repo"
155+
}
156+
}]`)
157+
158+
gock.New("https://api.github.com").
159+
Get("/assignments/1/accepted_assignments").
160+
Reply(200).
161+
JSON(`[]`)
162+
163+
client, err := gh.RESTClient(nil)
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
168+
actual, err := ListAllAcceptedAssignments(client, 1, 1)
169+
170+
if err != nil {
171+
t.Fatal(err)
172+
}
173+
174+
assert.Equal(t, 2, actual.Count)
175+
assert.Equal(t, 1, actual.AcceptedAssignments[0].Id)
176+
assert.Equal(t, "org1/student1-repo", actual.AcceptedAssignments[0].Repository.FullName)
177+
assert.Equal(t, "student1", actual.AcceptedAssignments[0].Students[0].Login)
178+
assert.Equal(t, 2, actual.AcceptedAssignments[1].Id)
179+
assert.Equal(t, "org1/student2-repo", actual.AcceptedAssignments[1].Repository.FullName)
180+
assert.Equal(t, "student2", actual.AcceptedAssignments[1].Students[0].Login)
181+
}

pkg/classroom/http.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,14 @@ func ListClassrooms(client api.RESTClient, page int, perPage int) ([]Classroom,
3333
return response, nil
3434
}
3535

36-
func ListAcceptedAssignments(client api.RESTClient, assignmentID int, page int, perPage int) (AcceptedAssignmentList, error) {
36+
func GetAssignmentList(client api.RESTClient, assignmentID int, page int, perPage int) ([]AcceptedAssignment, error) {
3737
var response []AcceptedAssignment
38+
3839
err := client.Get(fmt.Sprintf("assignments/%v/accepted_assignments?page=%v&per_page=%v", assignmentID, page, perPage), &response)
3940
if err != nil {
40-
return AcceptedAssignmentList{}, err
41-
}
42-
43-
if len(response) == 0 {
44-
return AcceptedAssignmentList{}, nil
41+
return nil, err
4542
}
46-
47-
acceptedAssignmentList := NewAcceptedAssignmentList(response)
48-
49-
return acceptedAssignmentList, nil
43+
return response, nil
5044
}
5145

5246
func GetAssignment(client api.RESTClient, assignmentID int) (Assignment, error) {

pkg/classroom/http_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestListClassrooms(t *testing.T) {
6767
assert.Equal(t, "Classroom Name", actual[0].Name)
6868
}
6969

70-
func TestListAcceptedAssignments(t *testing.T) {
70+
func TestGetAssignmentList(t *testing.T) {
7171
t.Setenv("GITHUB_TOKEN", "999")
7272
defer gock.Off()
7373

@@ -97,23 +97,53 @@ func TestListAcceptedAssignments(t *testing.T) {
9797
"id": 1,
9898
"full_name": "org1/student1-repo"
9999
}
100+
},
101+
{"id": 2,
102+
"assignment": {
103+
"id": 1,
104+
"title": "Assignment 1",
105+
"description": "This is the first assignment",
106+
"due_date": "2018-01-01",
107+
"classroom": {
108+
"id": 1,
109+
"name": "Classroom Name"
110+
},
111+
"starter_code_repository": {
112+
"id": 1,
113+
"full_name": "org1/starter-code-repo"
114+
}
115+
},
116+
"students": [{
117+
"id": 2,
118+
"login": "student2"
119+
}],
120+
"repository": {
121+
"id": 2,
122+
"full_name": "org1/student2-repo"
123+
}
100124
}]`)
101125

102126
client, err := gh.RESTClient(nil)
103127
if err != nil {
104128
t.Fatal(err)
105129
}
106130

107-
actual, err := ListAcceptedAssignments(client, 1, 1, 30)
108-
131+
//Ask for page 1 and 15 per page
132+
actual, err := GetAssignmentList(client, 1, 1, 15)
109133
if err != nil {
110134
t.Fatal(err)
111135
}
136+
assert.Equal(t, 2, len(actual))
137+
//check 1st entry
138+
assert.Equal(t, 1, actual[0].Id)
139+
assert.Equal(t, "org1/student1-repo", actual[0].Repository.FullName)
140+
assert.Equal(t, "student1", actual[0].Students[0].Login)
141+
142+
//check 2nd entry
143+
assert.Equal(t, 2, actual[1].Id)
144+
assert.Equal(t, "org1/student2-repo", actual[1].Repository.FullName)
145+
assert.Equal(t, "student2", actual[1].Students[0].Login)
112146

113-
assert.Equal(t, 1, actual.Count)
114-
assert.Equal(t, 1, actual.AcceptedAssignments[0].Id)
115-
assert.Equal(t, "org1/student1-repo", actual.AcceptedAssignments[0].Repository.FullName)
116-
assert.Equal(t, "student1", actual.AcceptedAssignments[0].Students[0].Login)
117147
}
118148

119149
func TestGetAssignment(t *testing.T) {

0 commit comments

Comments
 (0)