// makeBugChomperPage builds and serves the BugChomper page. func makeBugChomperPage(w http.ResponseWriter, r *http.Request) { // Redirect for login if needed. user := login.LoggedInAs(r) if user == "" { http.Redirect(w, r, login.LoginURL(w, r), http.StatusFound) return } glog.Infof("Logged in as %s", user) issueTracker := issue_tracker.New(login.GetHttpClient(r)) w.Header().Set("Content-Type", "text/html") glog.Info("Loading bugs for " + user) bugList, err := issueTracker.GetBugs(PROJECT_NAME, user) if err != nil { reportError(w, err.Error(), http.StatusInternalServerError) return } bugsById := make(map[string]*issue_tracker.Issue) bugsByPriority := make(map[string][]*issue_tracker.Issue) for _, bug := range bugList.Items { bugsById[strconv.Itoa(bug.Id)] = bug var bugPriority string for _, label := range bug.Labels { if strings.HasPrefix(label, PRIORITY_PREFIX) { bugPriority = label[len(PRIORITY_PREFIX):] } } if _, ok := bugsByPriority[bugPriority]; !ok { bugsByPriority[bugPriority] = make( []*issue_tracker.Issue, 0) } bugsByPriority[bugPriority] = append( bugsByPriority[bugPriority], bug) } bugsJson, err := json.Marshal(bugsById) if err != nil { reportError(w, err.Error(), http.StatusInternalServerError) return } data := struct { Title string User string BugsJson template.JS BugsByPriority *map[string][]*issue_tracker.Issue Priorities []string PriorityPrefix string }{ Title: "BugChomper", User: user, BugsJson: template.JS(string(bugsJson)), BugsByPriority: &bugsByPriority, Priorities: issue_tracker.BugPriorities, PriorityPrefix: PRIORITY_PREFIX, } if err := templates.ExecuteTemplate(w, "bug_chomper.html", data); err != nil { reportError(w, err.Error(), http.StatusInternalServerError) return } }
// submitData attempts to submit data from a POST request to the IssueTracker. func submitData(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") issueTracker := issue_tracker.New(login.GetHttpClient(r)) edits := r.FormValue("all_edits") var editsMap map[string]*issue_tracker.Issue if err := json.Unmarshal([]byte(edits), &editsMap); err != nil { errMsg := "Could not parse edits from form response: " + err.Error() reportError(w, errMsg, http.StatusInternalServerError) return } data := struct { Title string Message string BackLink string }{} if len(editsMap) == 0 { data.Title = "No Changes Submitted" data.Message = "You didn't change anything!" data.BackLink = "" if err := templates.ExecuteTemplate(w, "submitted.html", data); err != nil { reportError(w, err.Error(), http.StatusInternalServerError) return } return } errorList := make([]error, 0) for issueId, newIssue := range editsMap { glog.Info("Editing issue " + issueId) if err := issueTracker.SubmitIssueChanges(newIssue, ISSUE_COMMENT); err != nil { errorList = append(errorList, err) } } if len(errorList) > 0 { errorStrings := "" for _, err := range errorList { errorStrings += err.Error() + "\n" } errMsg := "Not all changes could be submitted: \n" + errorStrings reportError(w, errMsg, http.StatusInternalServerError) return } data.Title = "Submitted Changes" data.Message = "Your changes were submitted to the issue tracker." data.BackLink = "" if err := templates.ExecuteTemplate(w, "submitted.html", data); err != nil { reportError(w, err.Error(), http.StatusInternalServerError) return } return }