Skip to content

Commit 17f09f7

Browse files
authored
Merge pull request #46 from shanep/add-logging-clone
Add logging clone for issue #4
2 parents 191cf21 + 4646580 commit 17f09f7

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ func NewCmdStudentRepo(f *cmdutil.Factory) *cobra.Command {
8888
}
8989
}
9090

91+
totalCloned := 0
9192
for _, acceptAssignment := range acceptedAssignmentList.AcceptedAssignments {
9293
clonePath := filepath.Join(fullPath, acceptAssignment.Repository.Name())
9394
if _, err := os.Stat(clonePath); os.IsNotExist(err) {
9495
fmt.Printf("Cloning into: %v\n", clonePath)
9596
_, _, err := gh.Exec("repo", "clone", acceptAssignment.Repository.FullName, "--", clonePath)
97+
totalCloned++
9698
if err != nil {
9799
log.Fatal(err)
98100
return
@@ -101,6 +103,12 @@ func NewCmdStudentRepo(f *cmdutil.Factory) *cobra.Command {
101103
fmt.Printf("Skip existing repo: %v use gh classroom pull to get new commits\n", clonePath)
102104
}
103105
}
106+
if getAll {
107+
fmt.Printf("Cloned %v repos.\n", totalCloned)
108+
} else {
109+
numPages, _ := shared.NumberOfAcceptedAssignmentsAndPages(client, assignmentId, perPage)
110+
fmt.Printf("Cloned %v repos. There are %v more pages of repos to clone.\n", totalCloned, numPages-page)
111+
}
104112
},
105113
}
106114

cmd/gh-classroom/shared/shared.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package shared
22

33
import (
44
"errors"
5+
"log"
56
"math"
67
"sync"
78

@@ -97,6 +98,18 @@ func PromptForAssignment(client api.RESTClient, classroomId int) (assignment cla
9798
return optionMap[answer.Assignment], nil
9899
}
99100

101+
// Get the total number of accepted assignments and the number of pages necessary to get them all
102+
// Calculate the number of pages necessary to get all of the assignments with a given perPage
103+
func NumberOfAcceptedAssignmentsAndPages(client api.RESTClient, assignmentID int, perPage int) (numPages, totalAccepted int) {
104+
assignment, err := classroom.GetAssignment(client, assignmentID)
105+
if err != nil {
106+
log.Fatal(err)
107+
}
108+
numPages = int(math.Ceil(float64(assignment.Accepted) / float64(perPage)))
109+
totalAccepted = assignment.Accepted
110+
return
111+
}
112+
100113
func ListAcceptedAssignments(client api.RESTClient, assignmentID int, page int, perPage int) (classroom.AcceptedAssignmentList, error) {
101114
response, err := classroom.GetAssignmentList(client, assignmentID, page, perPage)
102115
if err != nil {
@@ -118,17 +131,11 @@ type assignmentList struct {
118131

119132
func ListAllAcceptedAssignments(client api.RESTClient, assignmentID int, perPage int) (classroom.AcceptedAssignmentList, error) {
120133

121-
//Calculate the number of go channels to create. We will assign 1 channel per page
122-
//so we don't put too much pressure on the API all at once
123-
assignment, err := classroom.GetAssignment(client, assignmentID)
124-
if err != nil {
125-
return classroom.AcceptedAssignmentList{}, err
126-
}
127-
numChannels := int(math.Ceil(float64(assignment.Accepted) / float64(perPage)))
134+
numPages, totalAccepted := NumberOfAcceptedAssignmentsAndPages(client, assignmentID, perPage)
128135

129136
ch := make(chan assignmentList)
130137
var wg sync.WaitGroup
131-
for page := 1; page <= numChannels; page++ {
138+
for page := 1; page <= numPages; page++ {
132139
wg.Add(1)
133140
go func(pg int) {
134141
defer wg.Done()
@@ -141,9 +148,9 @@ func ListAllAcceptedAssignments(client api.RESTClient, assignmentID int, perPage
141148
}
142149

143150
var mu sync.Mutex
144-
assignments := make([]classroom.AcceptedAssignment, 0, assignment.Accepted)
151+
assignments := make([]classroom.AcceptedAssignment, 0, totalAccepted)
145152
var hadErr error = nil
146-
for page := 1; page <= numChannels; page++ {
153+
for page := 1; page <= numPages; page++ {
147154
wg.Add(1)
148155
go func() {
149156
defer wg.Done()
@@ -162,7 +169,7 @@ func ListAllAcceptedAssignments(client api.RESTClient, assignmentID int, perPage
162169
close(ch)
163170

164171
if hadErr != nil {
165-
return classroom.AcceptedAssignmentList{}, err
172+
return classroom.AcceptedAssignmentList{}, hadErr
166173
}
167174

168175
return classroom.NewAcceptedAssignmentList(assignments), nil

cmd/gh-classroom/shared/shared_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
package shared
22

33
import (
4+
"sort"
45
"testing"
56

67
"github.com/cli/go-gh"
78
"github.com/stretchr/testify/assert"
89
"gopkg.in/h2non/gock.v1"
910
)
1011

12+
func TestNumberOfAcceptedAssignmentsAndPages(t *testing.T) {
13+
t.Setenv("GITHUB_TOKEN", "999")
14+
defer gock.Off()
15+
16+
gock.New("https://api.github.com").
17+
Get("/assignments/1").
18+
Reply(200).
19+
JSON(`{"id": 1,
20+
"title": "Assignment 1",
21+
"description": "This is the first assignment",
22+
"due_date": "2018-01-01",
23+
"accepted": 2,
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+
client, err := gh.RESTClient(nil)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
numPages, totalAccepted := NumberOfAcceptedAssignmentsAndPages(client, 1, 1)
39+
assert.Equal(t, 2, numPages)
40+
assert.Equal(t, 2, totalAccepted)
41+
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
}
46+
1147
func TestListAcceptedAssignments(t *testing.T) {
1248
t.Setenv("GITHUB_TOKEN", "999")
1349
defer gock.Off()
@@ -192,6 +228,12 @@ func TestListAllAcceptedAssignments(t *testing.T) {
192228
t.Fatal(err)
193229
}
194230

231+
//Due to ListAllAcceptedAssignments using channels we can not guarantee the order of the results
232+
//so we need to sort the results before we can compare them to the expected results
233+
sort.Slice(actual.AcceptedAssignments, func(i, j int) bool {
234+
return actual.AcceptedAssignments[i].Id < actual.AcceptedAssignments[j].Id
235+
})
236+
195237
assert.Equal(t, 2, actual.Count)
196238
assert.Equal(t, 1, actual.AcceptedAssignments[0].Id)
197239
assert.Equal(t, "org1/student1-repo", actual.AcceptedAssignments[0].Repository.FullName)

0 commit comments

Comments
 (0)