From d833ffe6cd5efd97b00f907ed426a88275a8c8db Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 23:08:08 +0200 Subject: Add an option to list "Modules" (sprints & their IDs) Sprints will change with time while the other IDs of a project will stay the same. Thus sprints, which live in the `Module` field, must be updated regularly. In order to facilitate that updating, instead of requiring users to get those IDs directly from the TimeTask website every time, have them use this command to get the names of sprints and their IDs. They can then update the ID manually in their config file. This code makes a request to the endpoint that returns module IDs for the site (the site queries this via AJAX to update its interface). The result of the request is some XML containing the modules and their IDs. For now I'm just printing this out. We'll want to parse the XML and display it in a nicer way. --- timetask/http.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'timetask/http.go') diff --git a/timetask/http.go b/timetask/http.go index 8c41b4f..3ff6f4f 100644 --- a/timetask/http.go +++ b/timetask/http.go @@ -1,6 +1,7 @@ package timetask import ( + "io/ioutil" "net/http" "net/http/cookiejar" "net/url" @@ -126,3 +127,38 @@ func buildSubmissionParams(time_entry TimeEntry) url.Values { return v } + +func RequestModules( + client http.Client, + time_entry TimeEntry, +) (string, error) { + params := url.Values{ + "module": {"projects"}, + "action": {"listmodulesxref"}, + "f_ID": {strconv.Itoa(time_entry.Project)}, + "f_active": {"t"}, + "f_clientID": {strconv.Itoa(time_entry.Client)}, + "f_personID": {strconv.Itoa(time_entry.PersonID)}, + "f_milestoneID": {""}, + } + modules_url, err := url.Parse(baseURL) + if err != nil { + return "", err + } + + modules_url.RawQuery = params.Encode() + + resp, err := client.Get(modules_url.String()) + if err != nil { + return "", err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + response_body := string(body) + + return response_body, nil +} -- cgit v1.2.3 From eb82e5e447dff67d5bcd1a2d5c9c52990a840e29 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 4 Jun 2017 00:19:35 +0200 Subject: RequestModules(): Pretty print modules Parse the module XML with `ModuleParseXML()`. Take the resulting `[]Module` slice and use it to generate a string of the following format: ID Module 55555 R&D 77777 Sprint 1 222222 Sprint 2 This string is what gets printed to the console, which makes it rather easy to read the modules that are available for the given project and grab the appropriate ID to put into your config file. --- timetask/http.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'timetask/http.go') diff --git a/timetask/http.go b/timetask/http.go index 3ff6f4f..575ceae 100644 --- a/timetask/http.go +++ b/timetask/http.go @@ -1,6 +1,8 @@ package timetask import ( + "bytes" + "fmt" "io/ioutil" "net/http" "net/http/cookiejar" @@ -160,5 +162,18 @@ func RequestModules( } response_body := string(body) - return response_body, nil + modules, err := ModuleParseXML(response_body) + if err != nil { + return "", err + } + + var module_buf bytes.Buffer + module_buf.WriteString("ID\tModule\n") + for _, module := range modules { + module_buf.WriteString( + fmt.Sprintf("%d\t%s\n", module.ID, module.Name), + ) + } + + return module_buf.String(), nil } -- cgit v1.2.3