Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,20 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
return err
}

team, err := flags.GetString("team")
if err != nil {
return err
}

if uuid == "" {
q := req.URL.Query()
q.Add("exercise_id", exercise)
if track != "" {
q.Add("track_id", track)
}
if team != "" {
q.Add("team_id", team)
}
req.URL.RawQuery = q.Encode()
}

Expand Down Expand Up @@ -127,6 +135,7 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
solution := workspace.Solution{
AutoApprove: payload.Solution.Exercise.AutoApprove,
Track: payload.Solution.Exercise.Track.ID,
Team: payload.Solution.Team.Slug,
Exercise: payload.Solution.Exercise.ID,
ID: payload.Solution.ID,
URL: payload.Solution.URL,
Expand All @@ -135,6 +144,9 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
}

dir := usrCfg.GetString("workspace")
if solution.Team != "" {
dir = filepath.Join(dir, "teams", solution.Team)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing. In payload it is a struct here Team is a string. I understand the difference but maybe it should be TeamSlug. Or maybe have a different name in the Solution struct

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think TeamSlug makes sense.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it back, actually.

In the solution we're storing just the minimal metadata. The payload has more data about both the track and the team, which we can use to communicate about it if we want to.

I would rather change the solution type later to have a more fleshed out team than to call this TeamSlug now.

}
if !solution.IsRequester {
dir = filepath.Join(dir, "users", solution.Handle)
}
Expand Down Expand Up @@ -205,6 +217,10 @@ type downloadPayload struct {
Solution struct {
ID string `json:"id"`
URL string `json:"url"`
Team struct {
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"team"`
User struct {
Handle string `json:"handle"`
IsRequester bool `json:"is_requester"`
Expand Down Expand Up @@ -235,6 +251,7 @@ func setupDownloadFlags(flags *pflag.FlagSet) {
flags.StringP("uuid", "u", "", "the solution UUID")
flags.StringP("track", "t", "", "the track ID")
flags.StringP("exercise", "e", "", "the exercise slug")
flags.StringP("team", "T", "", "the team slug")
}

func init() {
Expand Down
23 changes: 17 additions & 6 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ func TestDownload(t *testing.T) {
expectedDir: filepath.Join("users", "alice"),
flags: map[string]string{"uuid": "bogus-id"},
},
{
requester: true,
expectedDir: filepath.Join("teams", "bogus-team"),
flags: map[string]string{"exercise": "bogus-exercise", "track": "bogus-track", "team": "bogus-team"},
},
}

for _, tc := range testCases {
tmpDir, err := ioutil.TempDir("", "download-cmd")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

ts := fakeDownloadServer(strconv.FormatBool(tc.requester))
ts := fakeDownloadServer(strconv.FormatBool(tc.requester), tc.flags["team"])
defer ts.Close()

v := viper.New()
Expand Down Expand Up @@ -149,7 +154,7 @@ func TestDownload(t *testing.T) {
}
}

func fakeDownloadServer(requestor string) *httptest.Server {
func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
mux := http.NewServeMux()
server := httptest.NewServer(mux)

Expand All @@ -165,11 +170,16 @@ func fakeDownloadServer(requestor string) *httptest.Server {
fmt.Fprint(w, "")
})

payloadBody := fmt.Sprintf(payloadTemplate, requestor, server.URL+"/")
mux.HandleFunc("/solutions/latest", func(w http.ResponseWriter, r *http.Request) {
team := "null"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about the Team field for the Solution type.

if teamSlug := r.FormValue("team_id"); teamSlug != "" {
team = fmt.Sprintf(`{"name": "Bogus Team", "slug": "%s"}`, teamSlug)
}
payloadBody := fmt.Sprintf(payloadTemplate, requestor, team, server.URL+"/")
fmt.Fprint(w, payloadBody)
})
mux.HandleFunc("/solutions/bogus-id", func(w http.ResponseWriter, r *http.Request) {
payloadBody := fmt.Sprintf(payloadTemplate, requestor, "null", server.URL+"/")
fmt.Fprint(w, payloadBody)
})

Expand Down Expand Up @@ -215,6 +225,7 @@ const payloadTemplate = `
"handle": "alice",
"is_requester": %s
},
"team": %s,
"exercise": {
"id": "bogus-exercise",
"instructions_url": "http://example.com/bogus-exercise",
Expand All @@ -226,9 +237,9 @@ const payloadTemplate = `
},
"file_download_base_url": "%s",
"files": [
"/file-1.txt",
"/subdir/file-2.txt",
"/file-3.txt"
"/file-1.txt",
"/subdir/file-2.txt",
"/file-3.txt"
],
"iteration": {
"submitted_at": "2017-08-21t10:11:12.130z"
Expand Down
1 change: 1 addition & 0 deletions workspace/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Solution struct {
Track string `json:"track"`
Exercise string `json:"exercise"`
ID string `json:"id"`
Team string `json:"team,omitempty"`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the intended empty state for Team “”, null? I ask because I am wondering if this should be of type *string.

If the empty state is “” then your example of “null” in the test should be changed.

Copy link
Copy Markdown
Member Author

@kytrinyx kytrinyx Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty state of the solution metadata is "". The empty state of the payload we receive from the server is null.

URL string `json:"url"`
Handle string `json:"handle"`
IsRequester bool `json:"is_requester"`
Expand Down