diff options
Diffstat (limited to 'routers/repo/issue.go')
-rw-r--r-- | routers/repo/issue.go | 210 |
1 files changed, 123 insertions, 87 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go index c033e0f3..ab9b2b64 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -9,13 +9,12 @@ import ( "fmt" "io" "io/ioutil" - "mime" + "net/http" "net/url" "strings" "time" "github.com/Unknwon/com" - "github.com/go-martini/martini" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -74,7 +73,7 @@ func Issues(ctx *middleware.Context) { } var mid int64 - midx, _ := base.StrTo(ctx.Query("milestone")).Int64() + midx, _ := com.StrTo(ctx.Query("milestone")).Int64() if midx > 0 { mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx) if err != nil { @@ -95,7 +94,7 @@ func Issues(ctx *middleware.Context) { } ctx.Data["Labels"] = labels - page, _ := base.StrTo(ctx.Query("page")).Int() + page, _ := com.StrTo(ctx.Query("page")).Int() // Get issues. issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page, @@ -115,7 +114,7 @@ func Issues(ctx *middleware.Context) { // Get posters. for i := range issues { if err = issues[i].GetLabels(); err != nil { - ctx.Handle(500, "issue.Issues(GetLabels)", fmt.Errorf("[#%d]%v", issues[i].Id, err)) + ctx.Handle(500, "GetLabels", fmt.Errorf("[#%d]%v", issues[i].Id, err)) return } @@ -143,7 +142,7 @@ func Issues(ctx *middleware.Context) { } issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode) ctx.Data["IssueStats"] = issueStats - ctx.Data["SelectLabels"], _ = base.StrTo(selectLabels).Int64() + ctx.Data["SelectLabels"], _ = com.StrTo(selectLabels).Int64() ctx.Data["ViewType"] = viewType ctx.Data["Issues"] = issues ctx.Data["IsShowClosed"] = isShowClosed @@ -156,7 +155,7 @@ func Issues(ctx *middleware.Context) { ctx.HTML(200, ISSUES) } -func CreateIssue(ctx *middleware.Context, params martini.Params) { +func CreateIssue(ctx *middleware.Context) { ctx.Data["Title"] = "Create issue" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false @@ -187,34 +186,46 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, ISSUE_CREATE) } -func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - ctx.Data["Title"] = "Create issue" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = false - ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled +func CreateIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) { + send := func(status int, data interface{}, err error) { + if err != nil { + log.Error(4, "issue.CreateIssuePost(?): %s", err) + + ctx.JSON(status, map[string]interface{}{ + "ok": false, + "status": status, + "error": err.Error(), + }) + } else { + ctx.JSON(status, map[string]interface{}{ + "ok": true, + "status": status, + "data": data, + }) + } + } var err error // Get all milestones. - ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) + _, err = models.GetMilestones(ctx.Repo.Repository.Id, false) if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) + send(500, nil, err) return } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) + _, err = models.GetMilestones(ctx.Repo.Repository.Id, true) if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) + send(500, nil, err) return } - us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) + _, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) if err != nil { - ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) + send(500, nil, err) return } - ctx.Data["Collaborators"] = us if ctx.HasError() { - ctx.HTML(200, ISSUE_CREATE) + send(400, nil, errors.New(ctx.Flash.ErrorMsg)) return } @@ -233,11 +244,11 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C Content: form.Content, } if err := models.NewIssue(issue); err != nil { - ctx.Handle(500, "issue.CreateIssue(NewIssue)", err) + send(500, nil, err) return } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id, ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil { - ctx.Handle(500, "issue.CreateIssue(NewIssueUserPairs)", err) + send(500, nil, err) return } @@ -252,9 +263,8 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C ms[i] = ms[i][1:] } - ids := models.GetUserIdsByNames(ms) - if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil { - ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err) + if err := models.UpdateMentions(ms, issue.Id); err != nil { + send(500, nil, err) return } } @@ -263,7 +273,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email, - OpType: models.OP_CREATE_ISSUE, + OpType: models.CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), RepoId: ctx.Repo.Repository.Id, RepoUserName: ctx.Repo.Owner.Name, @@ -273,7 +283,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C } // Notify watchers. if err := models.NotifyWatchers(act); err != nil { - ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err) + send(500, nil, err) return } @@ -281,7 +291,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C if setting.Service.EnableNotifyMail { tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) if err != nil { - ctx.Handle(500, "issue.CreateIssue(SendIssueNotifyMail)", err) + send(500, nil, err) return } @@ -296,13 +306,13 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C } if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { - ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err) + send(500, nil, err) return } } log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) - ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index)) + send(200, fmt.Sprintf("/%s/%s/issues/%d", ctx.Params(":username"), ctx.Params(":reponame"), issue.Index), nil) } func checkLabels(labels, allLabels []*models.Label) { @@ -316,10 +326,10 @@ func checkLabels(labels, allLabels []*models.Label) { } } -func ViewIssue(ctx *middleware.Context, params martini.Params) { +func ViewIssue(ctx *middleware.Context) { ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled - idx, _ := base.StrTo(params["index"]).Int64() + idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx == 0 { ctx.Handle(404, "issue.ViewIssue", nil) return @@ -430,8 +440,8 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, ISSUE_VIEW) } -func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - idx, _ := base.StrTo(params["index"]).Int64() +func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) { + idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx <= 0 { ctx.Error(404) return @@ -473,13 +483,13 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat }) } -func UpdateIssueLabel(ctx *middleware.Context, params martini.Params) { +func UpdateIssueLabel(ctx *middleware.Context) { if !ctx.Repo.IsOwner { ctx.Error(403) return } - idx, _ := base.StrTo(params["index"]).Int64() + idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx <= 0 { ctx.Error(404) return @@ -497,7 +507,7 @@ func UpdateIssueLabel(ctx *middleware.Context, params martini.Params) { isAttach := ctx.Query("action") == "attach" labelStrId := ctx.Query("id") - labelId, _ := base.StrTo(labelStrId).Int64() + labelId := com.StrTo(labelStrId).MustInt64() label, err := models.GetLabelById(labelId) if err != nil { if err == models.ErrLabelNotExist { @@ -555,8 +565,8 @@ func UpdateIssueMilestone(ctx *middleware.Context) { return } - issueId, err := base.StrTo(ctx.Query("issue")).Int64() - if err != nil { + issueId := com.StrTo(ctx.Params(":issue")).MustInt64() + if issueId == 0 { ctx.Error(404) return } @@ -572,7 +582,7 @@ func UpdateIssueMilestone(ctx *middleware.Context) { } oldMid := issue.MilestoneId - mid, _ := base.StrTo(ctx.Query("milestone")).Int64() + mid := com.StrTo(ctx.Params(":milestone")).MustInt64() if oldMid == mid { ctx.JSON(200, map[string]interface{}{ "ok": true, @@ -601,8 +611,8 @@ func UpdateAssignee(ctx *middleware.Context) { return } - issueId, err := base.StrTo(ctx.Query("issue")).Int64() - if err != nil { + issueId := com.StrTo(ctx.Params(":index")).MustInt64() + if issueId == 0 { ctx.Error(404) return } @@ -610,21 +620,21 @@ func UpdateAssignee(ctx *middleware.Context) { issue, err := models.GetIssueById(issueId) if err != nil { if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateAssignee(GetIssueById)", err) + ctx.Handle(404, "GetIssueById", err) } else { - ctx.Handle(500, "issue.UpdateAssignee(GetIssueById)", err) + ctx.Handle(500, "GetIssueById", err) } return } - aid, _ := base.StrTo(ctx.Query("assigneeid")).Int64() + aid := com.StrTo(ctx.Params(":assigneeid")).MustInt64() // Not check for invalid assignne id and give responsibility to owners. issue.AssigneeId = aid if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil { - ctx.Handle(500, "issue.UpdateAssignee(UpdateIssueUserPairByAssignee): %v", err) + ctx.Handle(500, "UpdateIssueUserPairByAssignee: %v", err) return } else if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.UpdateAssignee(UpdateIssue)", err) + ctx.Handle(500, "UpdateIssue", err) return } @@ -656,8 +666,15 @@ func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { defer file.Close() + buf := make([]byte, 1024) + n, _ := file.Read(buf) + if n > 0 { + buf = buf[:n] + } + fileType := http.DetectContentType(buf) + fmt.Println(fileType) + allowed := false - fileType := mime.TypeByExtension(header.Filename) for _, t := range allowedTypes { t := strings.Trim(t, " ") @@ -676,42 +693,60 @@ func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_") if err != nil { - ctx.Handle(500, "issue.Comment(ioutil.TempFile)", err) + ctx.Handle(500, "ioutil.TempFile", err) return } defer out.Close() + out.Write(buf) _, err = io.Copy(out, file) - if err != nil { - ctx.Handle(500, "issue.Comment(io.Copy)", err) + ctx.Handle(500, "io.Copy", err) return } _, err = models.CreateAttachment(issueId, commentId, header.Filename, out.Name()) - if err != nil { - ctx.Handle(500, "issue.Comment(io.Copy)", err) + ctx.Handle(500, "CreateAttachment", err) return } } } -func Comment(ctx *middleware.Context, params martini.Params) { - index, err := base.StrTo(ctx.Query("issueIndex")).Int64() - if err != nil { - ctx.Handle(404, "issue.Comment(get index)", err) +func Comment(ctx *middleware.Context) { + send := func(status int, data interface{}, err error) { + if err != nil { + log.Error(4, "issue.Comment(?): %s", err) + + ctx.JSON(status, map[string]interface{}{ + "ok": false, + "status": status, + "error": err.Error(), + }) + } else { + ctx.JSON(status, map[string]interface{}{ + "ok": true, + "status": status, + "data": data, + }) + } + } + + index := com.StrTo(ctx.Query("issueIndex")).MustInt64() + if index == 0 { + ctx.Error(404) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index) if err != nil { if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.Comment", err) + send(404, nil, err) } else { - ctx.Handle(200, "issue.Comment(get issue)", err) + send(200, nil, err) } + return } @@ -725,17 +760,17 @@ func Comment(ctx *middleware.Context, params martini.Params) { (strings.Contains(newStatus, "Close") && !issue.IsClosed) { issue.IsClosed = !issue.IsClosed if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.Comment(UpdateIssue)", err) + send(500, nil, err) return } else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil { - ctx.Handle(500, "issue.Comment(UpdateIssueUserPairsByStatus)", err) + send(500, nil, err) return } // Change open/closed issue counter for the associated milestone if issue.MilestoneId > 0 { if err = models.ChangeMilestoneIssueStats(issue); err != nil { - ctx.Handle(500, "issue.Comment(ChangeMilestoneIssueStats)", err) + send(500, nil, err) } } @@ -745,7 +780,7 @@ func Comment(ctx *middleware.Context, params martini.Params) { } if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil { - ctx.Handle(200, "issue.Comment(create status change comment)", err) + send(200, nil, err) return } log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed) @@ -756,11 +791,12 @@ func Comment(ctx *middleware.Context, params martini.Params) { var ms []string content := ctx.Query("content") - if len(content) > 0 { - switch params["action"] { + // Fix #321. Allow empty comments, as long as we have attachments. + if len(content) > 0 || len(ctx.Req.MultipartForm.File["attachments"]) > 0 { + switch ctx.Params(":action") { case "new": if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil { - ctx.Handle(500, "issue.Comment(create comment)", err) + send(500, nil, err) return } @@ -771,9 +807,8 @@ func Comment(ctx *middleware.Context, params martini.Params) { ms[i] = ms[i][1:] } - ids := models.GetUserIdsByNames(ms) - if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil { - ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err) + if err := models.UpdateMentions(ms, issue.Id); err != nil { + send(500, nil, err) return } } @@ -794,14 +829,14 @@ func Comment(ctx *middleware.Context, params martini.Params) { ActUserId: ctx.User.Id, ActUserName: ctx.User.LowerName, ActEmail: ctx.User.Email, - OpType: models.OP_COMMENT_ISSUE, + OpType: models.COMMENT_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]), RepoId: ctx.Repo.Repository.Id, RepoUserName: ctx.Repo.Owner.LowerName, RepoName: ctx.Repo.Repository.LowerName, } if err = models.NotifyWatchers(act); err != nil { - ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err) + send(500, nil, err) return } @@ -810,7 +845,7 @@ func Comment(ctx *middleware.Context, params martini.Params) { issue.Content = content tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) if err != nil { - ctx.Handle(500, "issue.Comment(SendIssueNotifyMail)", err) + send(500, nil, err) return } @@ -825,12 +860,12 @@ func Comment(ctx *middleware.Context, params martini.Params) { } if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { - ctx.Handle(500, "issue.Comment(SendIssueMentionMail)", err) + send(500, nil, err) return } } - ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index)) + send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil) } func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) { @@ -851,8 +886,8 @@ func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) { ctx.Redirect(ctx.Repo.RepoLink + "/issues") } -func UpdateLabel(ctx *middleware.Context, params martini.Params, form auth.CreateLabelForm) { - id, _ := base.StrTo(ctx.Query("id")).Int64() +func UpdateLabel(ctx *middleware.Context, form auth.CreateLabelForm) { + id := com.StrTo(ctx.Query("id")).MustInt64() if id == 0 { ctx.Error(404) return @@ -961,12 +996,12 @@ func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") } -func UpdateMilestone(ctx *middleware.Context, params martini.Params) { +func UpdateMilestone(ctx *middleware.Context) { ctx.Data["Title"] = "Update Milestone" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true - idx, _ := base.StrTo(params["index"]).Int64() + idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx == 0 { ctx.Handle(404, "issue.UpdateMilestone", nil) return @@ -982,7 +1017,7 @@ func UpdateMilestone(ctx *middleware.Context, params martini.Params) { return } - action := params["action"] + action := ctx.Params(":action") if len(action) > 0 { switch action { case "open": @@ -1019,12 +1054,12 @@ func UpdateMilestone(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, MILESTONE_EDIT) } -func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) { +func UpdateMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { ctx.Data["Title"] = "Update Milestone" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true - idx, _ := base.StrTo(params["index"]).Int64() + idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx == 0 { ctx.Handle(404, "issue.UpdateMilestonePost", nil) return @@ -1066,11 +1101,10 @@ func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form au ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") } -func IssueGetAttachment(ctx *middleware.Context, params martini.Params) { - id, err := base.StrTo(params["id"]).Int64() - - if err != nil { - ctx.Handle(400, "issue.IssueGetAttachment(base.StrTo.Int64)", err) +func IssueGetAttachment(ctx *middleware.Context) { + id := com.StrTo(ctx.Params(":id")).MustInt64() + if id == 0 { + ctx.Error(404) return } @@ -1081,5 +1115,7 @@ func IssueGetAttachment(ctx *middleware.Context, params martini.Params) { return } - ctx.ServeFile(attachment.Path, attachment.Name) + // Fix #312. Attachments with , in their name are not handled correctly by Google Chrome. + // We must put the name in " manually. + ctx.ServeFile(attachment.Path, "\""+attachment.Name+"\"") } |