diff options
Diffstat (limited to 'routers/repo/issue.go')
-rw-r--r-- | routers/repo/issue.go | 364 |
1 files changed, 134 insertions, 230 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 20a6331f..c4903a92 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -172,13 +172,17 @@ func Issues(ctx *middleware.Context) { ctx.HTML(200, ISSUES) } +func renderAttachmentSettings(ctx *middleware.Context) { + ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled + ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes + ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles +} + func NewIssue(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true ctx.Data["RequireDropzone"] = true - ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled - ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes - ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles + renderAttachmentSettings(ctx) if ctx.User.IsAdmin { var ( @@ -216,9 +220,7 @@ func NewIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true ctx.Data["RequireDropzone"] = true - ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled - ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes - ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles + renderAttachmentSettings(ctx) var ( repo = ctx.Repo.Repository @@ -286,7 +288,7 @@ func NewIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) { } if setting.AttachmentEnabled { - attachments = ctx.QueryStrings("attachments") + attachments = form.Attachments } if ctx.HasError() { @@ -409,116 +411,104 @@ func checkLabels(labels, allLabels []*models.Label) { } func ViewIssue(ctx *middleware.Context) { - ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled - - idx := com.StrTo(ctx.Params(":index")).MustInt64() - if idx == 0 { - ctx.Handle(404, "issue.ViewIssue", nil) - return - } + ctx.Data["PageIsIssueList"] = true + ctx.Data["RequireDropzone"] = true + renderAttachmentSettings(ctx) - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, idx) + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { - if err == models.ErrIssueNotExist { + if models.IsErrIssueNotExist(err) { ctx.Handle(404, "GetIssueByIndex", err) } else { ctx.Handle(500, "GetIssueByIndex", err) } return } + ctx.Data["Title"] = issue.Name - // Get labels. - if err = issue.GetLabels(); err != nil { - ctx.Handle(500, "GetLabels", err) - return - } - labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) - if err != nil { - ctx.Handle(500, "GetLabels.2", err) - return - } - checkLabels(issue.Labels, labels) - ctx.Data["Labels"] = labels - - // Get assigned milestone. - if issue.MilestoneID > 0 { - ctx.Data["Milestone"], err = models.GetMilestoneByID(issue.MilestoneID) - if err != nil { - if models.IsErrMilestoneNotExist(err) { - log.Warn("GetMilestoneById: %v", err) - } else { - ctx.Handle(500, "GetMilestoneById", err) - return - } - } - } - - // Get all milestones. - ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.ID, -1, false) - if err != nil { - ctx.Handle(500, "GetMilestones.1: %v", err) - return - } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.ID, -1, true) - if err != nil { - ctx.Handle(500, "GetMilestones.2: %v", err) + if err = issue.GetPoster(); err != nil { + ctx.Handle(500, "GetPoster", err) return } + issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)) - // Get all collaborators. - ctx.Data["Collaborators"], err = ctx.Repo.Repository.GetCollaborators() - if err != nil { - ctx.Handle(500, "GetCollaborators", err) + // Metas. + if err = issue.GetLabels(); err != nil { + ctx.Handle(500, "GetLabels", err) return } if ctx.IsSigned { // Update issue-user. - if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.ID); err != nil { - ctx.Handle(500, "UpdateIssueUserPairByRead: %v", err) + if err = issue.ReadBy(ctx.User.Id); err != nil { + ctx.Handle(500, "ReadBy", err) return } - } - // Get poster and Assignee. - if err = issue.GetPoster(); err != nil { - ctx.Handle(500, "GetPoster: %v", err) - return - } else if err = issue.GetAssignee(); err != nil { - ctx.Handle(500, "GetAssignee: %v", err) - return + if ctx.User.IsAdmin { + // labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) + // if err != nil { + // ctx.Handle(500, "GetLabels.2", err) + // return + // } + // checkLabels(issue.Labels, labels) + // ctx.Data["Labels"] = labels + + // // Get all milestones. + // ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.ID, -1, false) + // if err != nil { + // ctx.Handle(500, "GetMilestones.1: %v", err) + // return + // } + // ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.ID, -1, true) + // if err != nil { + // ctx.Handle(500, "GetMilestones.2: %v", err) + // return + // } + + // // Get all collaborators. + // ctx.Data["Collaborators"], err = ctx.Repo.Repository.GetCollaborators() + // if err != nil { + // ctx.Handle(500, "GetCollaborators", err) + // return + // } + } } - issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)) - // Get comments. - comments, err := models.GetIssueComments(issue.ID) - if err != nil { - ctx.Handle(500, "GetIssueComments: %v", err) - return - } + var ( + repo = ctx.Repo.Repository + tag models.CommentTag + ok bool + marked = make(map[int64]models.CommentTag) + comment *models.Comment + ) + // Render comments. + for _, comment = range issue.Comments { + if comment.Type == models.COMMENT_TYPE_COMMENT { + comment.RenderedContent = string(base.RenderMarkdown([]byte(comment.Content), ctx.Repo.RepoLink)) + + // Check tag. + tag, ok = marked[comment.PosterID] + if ok { + comment.ShowTag = tag + continue + } - // Get posters. - for i := range comments { - u, err := models.GetUserByID(comments[i].PosterId) - if err != nil { - ctx.Handle(500, "GetUserById.2: %v", err) - return - } - comments[i].Poster = u + if repo.IsOwnedBy(comment.PosterID) || + (repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) { + comment.ShowTag = models.COMMENT_TAG_OWNER + } else if comment.Poster.IsAdminOfRepo(repo) { + comment.ShowTag = models.COMMENT_TAG_ADMIN + } else if comment.PosterID == issue.PosterID { + comment.ShowTag = models.COMMENT_TAG_POSTER + } - if comments[i].Type == models.COMMENT_TYPE_COMMENT { - comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink)) + marked[comment.PosterID] = comment.ShowTag } } - ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes - - ctx.Data["Title"] = issue.Name ctx.Data["Issue"] = issue - ctx.Data["Comments"] = comments - ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner() || (ctx.IsSigned && issue.PosterID == ctx.User.Id) - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = false + // ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner() || (ctx.IsSigned && issue.PosterID == ctx.User.Id) ctx.HTML(200, ISSUE_VIEW) } @@ -531,7 +521,7 @@ func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, idx) if err != nil { - if err == models.ErrIssueNotExist { + if models.IsErrIssueNotExist(err) { ctx.Handle(404, "issue.UpdateIssue", err) } else { ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err) @@ -579,7 +569,7 @@ func UpdateIssueLabel(ctx *middleware.Context) { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, idx) if err != nil { - if err == models.ErrIssueNotExist { + if models.IsErrIssueNotExist(err) { ctx.Handle(404, "issue.UpdateIssueLabel(GetIssueByIndex)", err) } else { ctx.Handle(500, "issue.UpdateIssueLabel(GetIssueByIndex)", err) @@ -659,12 +649,12 @@ func UpdateIssueMilestone(ctx *middleware.Context) { return } - issue, err := models.GetIssueById(issueId) + issue, err := models.GetIssueByID(issueId) if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err) + if models.IsErrIssueNotExist(err) { + ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueByID)", err) } else { - ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err) + ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueByID)", err) } return } @@ -705,12 +695,12 @@ func UpdateAssignee(ctx *middleware.Context) { return } - issue, err := models.GetIssueById(issueId) + issue, err := models.GetIssueByID(issueId) if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "GetIssueById", err) + if models.IsErrIssueNotExist(err) { + ctx.Handle(404, "GetIssueByID", err) } else { - ctx.Handle(500, "GetIssueById", err) + ctx.Handle(500, "GetIssueByID", err) } return } @@ -731,163 +721,76 @@ func UpdateAssignee(ctx *middleware.Context) { }) } -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(), - }) +func NewComment(ctx *middleware.Context, form auth.CreateCommentForm) { + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrIssueNotExist(err) { + ctx.Handle(404, "GetIssueByIndex", err) } else { - ctx.JSON(status, map[string]interface{}{ - "ok": true, - "status": status, - "data": data, - }) + ctx.Handle(500, "GetIssueByIndex", err) } + return } - index := com.StrTo(ctx.Query("issueIndex")).MustInt64() - if index == 0 { - ctx.Error(404) + var attachments []string + if setting.AttachmentEnabled { + attachments = form.Attachments + } + + if ctx.HasError() { + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) + ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index)) return } - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, index) - if err != nil { - if err == models.ErrIssueNotExist { - send(404, nil, err) - } else { - send(200, nil, err) + // Check if issue owner/poster changes the status of issue. + if (ctx.Repo.IsOwner() || issue.IsPoster(ctx.User.Id)) && + (form.Status == "reopen" || form.Status == "close") { + issue.Repo = ctx.Repo.Repository + if err = issue.ChangeStatus(ctx.User, form.Status == "close"); err != nil { + ctx.Handle(500, "ChangeStatus", err) + return } + log.Trace("%s Issue[%d] status changed: %v", ctx.Req.RequestURI, issue.ID, !issue.IsClosed) + } + // Fix #321: Allow empty comments, as long as we have attachments. + if len(form.Content) == 0 && len(attachments) == 0 { + ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index)) return } - // Check if issue owner changes the status of issue. - var newStatus string - if ctx.Repo.IsOwner() || issue.PosterID == ctx.User.Id { - newStatus = ctx.Query("change_status") + comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments) + if err != nil { + ctx.Handle(500, "CreateIssueComment", err) + return } - if len(newStatus) > 0 { - if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) || - (strings.Contains(newStatus, "Close") && !issue.IsClosed) { - issue.IsClosed = !issue.IsClosed - if err = models.UpdateIssue(issue); err != nil { - send(500, nil, err) - return - } else if err = models.UpdateIssueUserPairsByStatus(issue.ID, issue.IsClosed); err != nil { - send(500, nil, err) - return - } - - if err = issue.GetLabels(); err != nil { - send(500, nil, err) - return - } - - for _, label := range issue.Labels { - if issue.IsClosed { - label.NumClosedIssues++ - } else { - label.NumClosedIssues-- - } - - if err = models.UpdateLabel(label); err != nil { - 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 { - send(500, nil, err) - } - } - - cmtType := models.COMMENT_TYPE_CLOSE - if !issue.IsClosed { - cmtType = models.COMMENT_TYPE_REOPEN - } - - if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.ID, issue.ID, 0, 0, cmtType, "", nil); err != nil { - send(200, nil, err) - return - } - log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.ID, !issue.IsClosed) + // Update mentions. + mentions := base.MentionPattern.FindAllString(comment.Content, -1) + if len(mentions) > 0 { + for i := range mentions { + mentions[i] = mentions[i][1:] } - } - - var comment *models.Comment - - var ms []string - content := ctx.Query("content") - // 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_TYPE_COMMENT, content, nil); err != nil { - send(500, nil, err) - return - } - // Update mentions. - ms = base.MentionPattern.FindAllString(issue.Content, -1) - if len(ms) > 0 { - for i := range ms { - ms[i] = ms[i][1:] - } - - if err := models.UpdateMentions(ms, issue.ID); err != nil { - send(500, nil, err) - return - } - } - - log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.ID) - default: - ctx.Handle(404, "issue.Comment", err) + if err := models.UpdateMentions(mentions, issue.ID); err != nil { + ctx.Handle(500, "UpdateMentions", err) return } } - if comment != nil { - // uploadFiles(ctx, issue.ID, comment.Id) - } - - // Notify watchers. - act := &models.Action{ - ActUserID: ctx.User.Id, - ActUserName: ctx.User.LowerName, - ActEmail: ctx.User.Email, - 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, - IsPrivate: ctx.Repo.Repository.IsPrivate, - } - if err = models.NotifyWatchers(act); err != nil { - send(500, nil, err) - return - } - // Mail watchers and mentions. if setting.Service.EnableNotifyMail { - issue.Content = content + issue.Content = form.Content tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) if err != nil { - send(500, nil, err) + ctx.Handle(500, "SendIssueNotifyMail", err) return } tos = append(tos, ctx.User.LowerName) - newTos := make([]string, 0, len(ms)) - for _, m := range ms { + newTos := make([]string, 0, len(mentions)) + for _, m := range mentions { if com.IsSliceContainsStr(tos, m) { continue } @@ -896,12 +799,13 @@ func Comment(ctx *middleware.Context) { } if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { - send(500, nil, err) + ctx.Handle(500, "SendIssueMentionMail", err) return } } - send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil) + log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID) + ctx.Redirect(fmt.Sprintf("%s/issues/%d#%s", ctx.Repo.RepoLink, issue.Index, comment.HashTag())) } func Labels(ctx *middleware.Context) { |