123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572 |
- // Copyright 2014 The Gogs Authors. All rights reserved.
- // Copyright 2018 The Gitea Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
-
- package repo
-
- import (
- "bytes"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/auth"
- "code.gitea.io/gitea/modules/base"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/git"
- issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/markup/markdown"
- "code.gitea.io/gitea/modules/notification"
- "code.gitea.io/gitea/modules/setting"
- api "code.gitea.io/gitea/modules/structs"
- "code.gitea.io/gitea/modules/util"
-
- "github.com/Unknwon/com"
- )
-
- const (
- tplIssues base.TplName = "repo/issue/list"
- tplIssueNew base.TplName = "repo/issue/new"
- tplIssueView base.TplName = "repo/issue/view"
-
- tplReactions base.TplName = "repo/issue/view_content/reactions"
-
- issueTemplateKey = "IssueTemplate"
- )
-
- var (
- // ErrFileTypeForbidden not allowed file type error
- ErrFileTypeForbidden = errors.New("File type is not allowed")
- // ErrTooManyFiles upload too many files
- ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
- // IssueTemplateCandidates issue templates
- IssueTemplateCandidates = []string{
- "ISSUE_TEMPLATE.md",
- "issue_template.md",
- ".gitea/ISSUE_TEMPLATE.md",
- ".gitea/issue_template.md",
- ".github/ISSUE_TEMPLATE.md",
- ".github/issue_template.md",
- }
- )
-
- // MustAllowUserComment checks to make sure if an issue is locked.
- // If locked and user has permissions to write to the repository,
- // then the comment is allowed, else it is blocked
- func MustAllowUserComment(ctx *context.Context) {
-
- issue := GetActionIssue(ctx)
- if ctx.Written() {
- return
- }
-
- if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
- ctx.Flash.Error(ctx.Tr("repo.issues.comment_on_locked"))
- ctx.Redirect(issue.HTMLURL())
- return
- }
- }
-
- // MustEnableIssues check if repository enable internal issues
- func MustEnableIssues(ctx *context.Context) {
- if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
- !ctx.Repo.CanRead(models.UnitTypeExternalTracker) {
- ctx.NotFound("MustEnableIssues", nil)
- return
- }
-
- unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker)
- if err == nil {
- ctx.Redirect(unit.ExternalTrackerConfig().ExternalTrackerURL)
- return
- }
- }
-
- // MustAllowPulls check if repository enable pull requests and user have right to do that
- func MustAllowPulls(ctx *context.Context) {
- if !ctx.Repo.Repository.CanEnablePulls() || !ctx.Repo.CanRead(models.UnitTypePullRequests) {
- ctx.NotFound("MustAllowPulls", nil)
- return
- }
-
- // User can send pull request if owns a forked repository.
- if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
- ctx.Repo.PullRequest.Allowed = true
- ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
- }
- }
-
- func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalBool) {
- var err error
- viewType := ctx.Query("type")
- sortType := ctx.Query("sort")
- types := []string{"all", "your_repositories", "assigned", "created_by", "mentioned"}
- if !com.IsSliceContainsStr(types, viewType) {
- viewType = "all"
- }
-
- var (
- assigneeID = ctx.QueryInt64("assignee")
- posterID int64
- mentionedID int64
- forceEmpty bool
- )
-
- if ctx.IsSigned {
- switch viewType {
- case "created_by":
- posterID = ctx.User.ID
- case "mentioned":
- mentionedID = ctx.User.ID
- }
- }
-
- repo := ctx.Repo.Repository
- var labelIDs []int64
- selectLabels := ctx.Query("labels")
- if len(selectLabels) > 0 && selectLabels != "0" {
- labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
- if err != nil {
- ctx.ServerError("StringsToInt64s", err)
- return
- }
- }
- isShowClosed := ctx.Query("state") == "closed"
-
- keyword := strings.Trim(ctx.Query("q"), " ")
- if bytes.Contains([]byte(keyword), []byte{0x00}) {
- keyword = ""
- }
-
- var issueIDs []int64
- if len(keyword) > 0 {
- issueIDs, err = issue_indexer.SearchIssuesByKeyword(repo.ID, keyword)
- if err != nil {
- ctx.ServerError("issueIndexer.Search", err)
- return
- }
- if len(issueIDs) == 0 {
- forceEmpty = true
- }
- }
-
- var issueStats *models.IssueStats
- if forceEmpty {
- issueStats = &models.IssueStats{}
- } else {
- issueStats, err = models.GetIssueStats(&models.IssueStatsOptions{
- RepoID: repo.ID,
- Labels: selectLabels,
- MilestoneID: milestoneID,
- AssigneeID: assigneeID,
- MentionedID: mentionedID,
- PosterID: posterID,
- IsPull: isPullOption,
- IssueIDs: issueIDs,
- })
- if err != nil {
- ctx.ServerError("GetIssueStats", err)
- return
- }
- }
- page := ctx.QueryInt("page")
- if page <= 1 {
- page = 1
- }
-
- var total int
- if !isShowClosed {
- total = int(issueStats.OpenCount)
- } else {
- total = int(issueStats.ClosedCount)
- }
- pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5)
-
- var issues []*models.Issue
- if forceEmpty {
- issues = []*models.Issue{}
- } else {
- issues, err = models.Issues(&models.IssuesOptions{
- RepoIDs: []int64{repo.ID},
- AssigneeID: assigneeID,
- PosterID: posterID,
- MentionedID: mentionedID,
- MilestoneID: milestoneID,
- Page: pager.Paginater.Current(),
- PageSize: setting.UI.IssuePagingNum,
- IsClosed: util.OptionalBoolOf(isShowClosed),
- IsPull: isPullOption,
- LabelIDs: labelIDs,
- SortType: sortType,
- IssueIDs: issueIDs,
- })
- if err != nil {
- ctx.ServerError("Issues", err)
- return
- }
- }
-
- var commitStatus = make(map[int64]*models.CommitStatus, len(issues))
-
- // Get posters.
- for i := range issues {
- // Check read status
- if !ctx.IsSigned {
- issues[i].IsRead = true
- } else if err = issues[i].GetIsRead(ctx.User.ID); err != nil {
- ctx.ServerError("GetIsRead", err)
- return
- }
-
- if issues[i].IsPull {
- if err := issues[i].LoadPullRequest(); err != nil {
- ctx.ServerError("LoadPullRequest", err)
- return
- }
-
- commitStatus[issues[i].PullRequest.ID], _ = issues[i].PullRequest.GetLastCommitStatus()
- }
- }
-
- ctx.Data["Issues"] = issues
- ctx.Data["CommitStatus"] = commitStatus
-
- // Get assignees.
- ctx.Data["Assignees"], err = repo.GetAssignees()
- if err != nil {
- ctx.ServerError("GetAssignees", err)
- return
- }
-
- labels, err := models.GetLabelsByRepoID(repo.ID, "")
- if err != nil {
- ctx.ServerError("GetLabelsByRepoID", err)
- return
- }
- for _, l := range labels {
- l.LoadSelectedLabelsAfterClick(labelIDs)
- }
- ctx.Data["Labels"] = labels
- ctx.Data["NumLabels"] = len(labels)
-
- if ctx.QueryInt64("assignee") == 0 {
- assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
- }
-
- ctx.Data["IssueStats"] = issueStats
- ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
- ctx.Data["ViewType"] = viewType
- ctx.Data["SortType"] = sortType
- ctx.Data["MilestoneID"] = milestoneID
- ctx.Data["AssigneeID"] = assigneeID
- ctx.Data["IsShowClosed"] = isShowClosed
- ctx.Data["Keyword"] = keyword
- if isShowClosed {
- ctx.Data["State"] = "closed"
- } else {
- ctx.Data["State"] = "open"
- }
-
- pager.AddParam(ctx, "q", "Keyword")
- pager.AddParam(ctx, "type", "ViewType")
- pager.AddParam(ctx, "sort", "SortType")
- pager.AddParam(ctx, "state", "State")
- pager.AddParam(ctx, "labels", "SelectLabels")
- pager.AddParam(ctx, "milestone", "MilestoneID")
- pager.AddParam(ctx, "assignee", "AssigneeID")
- ctx.Data["Page"] = pager
- }
-
- // Issues render issues page
- func Issues(ctx *context.Context) {
- isPullList := ctx.Params(":type") == "pulls"
- if isPullList {
- MustAllowPulls(ctx)
- if ctx.Written() {
- return
- }
- ctx.Data["Title"] = ctx.Tr("repo.pulls")
- ctx.Data["PageIsPullList"] = true
- } else {
- MustEnableIssues(ctx)
- if ctx.Written() {
- return
- }
- ctx.Data["Title"] = ctx.Tr("repo.issues")
- ctx.Data["PageIsIssueList"] = true
- }
-
- issues(ctx, ctx.QueryInt64("milestone"), util.OptionalBoolOf(isPullList))
-
- var err error
- // Get milestones.
- ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")))
- if err != nil {
- ctx.ServerError("GetAllRepoMilestones", err)
- return
- }
-
- perm, err := models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User)
- if err != nil {
- ctx.ServerError("GetUserRepoPermission", err)
- return
- }
- ctx.Data["CanWriteIssuesOrPulls"] = perm.CanWriteIssuesOrPulls(isPullList)
-
- ctx.HTML(200, tplIssues)
- }
-
- // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository
- func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) {
- var err error
- ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false, "")
- if err != nil {
- ctx.ServerError("GetMilestones", err)
- return
- }
- ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true, "")
- if err != nil {
- ctx.ServerError("GetMilestones", err)
- return
- }
-
- ctx.Data["Assignees"], err = repo.GetAssignees()
- if err != nil {
- ctx.ServerError("GetAssignees", err)
- return
- }
- }
-
- // RetrieveRepoMetas find all the meta information of a repository
- func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
- if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
- return nil
- }
-
- labels, err := models.GetLabelsByRepoID(repo.ID, "")
- if err != nil {
- ctx.ServerError("GetLabelsByRepoID", err)
- return nil
- }
- ctx.Data["Labels"] = labels
-
- RetrieveRepoMilestonesAndAssignees(ctx, repo)
- if ctx.Written() {
- return nil
- }
-
- brs, err := ctx.Repo.GitRepo.GetBranches()
- if err != nil {
- ctx.ServerError("GetBranches", err)
- return nil
- }
- ctx.Data["Branches"] = brs
-
- // Contains true if the user can create issue dependencies
- ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
-
- return labels
- }
-
- func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) {
- var bytes []byte
-
- if ctx.Repo.Commit == nil {
- var err error
- ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
- if err != nil {
- return "", false
- }
- }
-
- entry, err := ctx.Repo.Commit.GetTreeEntryByPath(filename)
- if err != nil {
- return "", false
- }
- if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
- return "", false
- }
- r, err := entry.Blob().DataAsync()
- if err != nil {
- return "", false
- }
- defer r.Close()
- bytes, err = ioutil.ReadAll(r)
- if err != nil {
- return "", false
- }
- return string(bytes), true
- }
-
- func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) {
- for _, filename := range possibleFiles {
- content, found := getFileContentFromDefaultBranch(ctx, filename)
- if found {
- ctx.Data[ctxDataKey] = content
- return
- }
- }
- }
-
- // NewIssue render creating issue page
- func NewIssue(ctx *context.Context) {
- ctx.Data["Title"] = ctx.Tr("repo.issues.new")
- ctx.Data["PageIsIssueList"] = true
- ctx.Data["RequireHighlightJS"] = true
- ctx.Data["RequireSimpleMDE"] = true
- ctx.Data["RequireTribute"] = true
- ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
- body := ctx.Query("body")
- ctx.Data["BodyQuery"] = body
-
- milestoneID := ctx.QueryInt64("milestone")
- if milestoneID > 0 {
- milestone, err := models.GetMilestoneByID(milestoneID)
- if err != nil {
- log.Error("GetMilestoneByID: %d: %v", milestoneID, err)
- } else {
- ctx.Data["milestone_id"] = milestoneID
- ctx.Data["Milestone"] = milestone
- }
- }
-
- setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
- renderAttachmentSettings(ctx)
-
- RetrieveRepoMetas(ctx, ctx.Repo.Repository)
- if ctx.Written() {
- return
- }
-
- ctx.HTML(200, tplIssueNew)
- }
-
- // ValidateRepoMetas check and returns repository's meta informations
- func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull bool) ([]int64, []int64, int64) {
- var (
- repo = ctx.Repo.Repository
- err error
- )
-
- labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
- if ctx.Written() {
- return nil, nil, 0
- }
-
- var labelIDs []int64
- hasSelected := false
- // Check labels.
- if len(form.LabelIDs) > 0 {
- labelIDs, err = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
- if err != nil {
- return nil, nil, 0
- }
- labelIDMark := base.Int64sToMap(labelIDs)
-
- for i := range labels {
- if labelIDMark[labels[i].ID] {
- labels[i].IsChecked = true
- hasSelected = true
- }
- }
- }
-
- ctx.Data["Labels"] = labels
- ctx.Data["HasSelectedLabel"] = hasSelected
- ctx.Data["label_ids"] = form.LabelIDs
-
- // Check milestone.
- milestoneID := form.MilestoneID
- if milestoneID > 0 {
- ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
- if err != nil {
- ctx.ServerError("GetMilestoneByID", err)
- return nil, nil, 0
- }
- ctx.Data["milestone_id"] = milestoneID
- }
-
- // Check assignees
- var assigneeIDs []int64
- if len(form.AssigneeIDs) > 0 {
- assigneeIDs, err = base.StringsToInt64s(strings.Split(form.AssigneeIDs, ","))
- if err != nil {
- return nil, nil, 0
- }
-
- // Check if the passed assignees actually exists and has write access to the repo
- for _, aID := range assigneeIDs {
- user, err := models.GetUserByID(aID)
- if err != nil {
- ctx.ServerError("GetUserByID", err)
- return nil, nil, 0
- }
-
- perm, err := models.GetUserRepoPermission(repo, user)
- if err != nil {
- ctx.ServerError("GetUserRepoPermission", err)
- return nil, nil, 0
- }
- if !perm.CanWriteIssuesOrPulls(isPull) {
- ctx.ServerError("CanWriteIssuesOrPulls", fmt.Errorf("No permission for %s", user.Name))
- return nil, nil, 0
- }
- }
- }
-
- // Keep the old assignee id thingy for compatibility reasons
- if form.AssigneeID > 0 {
- assigneeIDs = append(assigneeIDs, form.AssigneeID)
- }
-
- return labelIDs, assigneeIDs, milestoneID
- }
-
- // NewIssuePost response for creating new issue
- func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
- ctx.Data["Title"] = ctx.Tr("repo.issues.new")
- ctx.Data["PageIsIssueList"] = true
- ctx.Data["RequireHighlightJS"] = true
- ctx.Data["RequireSimpleMDE"] = true
- ctx.Data["ReadOnly"] = false
- ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
- renderAttachmentSettings(ctx)
-
- var (
- repo = ctx.Repo.Repository
- attachments []string
- )
-
- labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, false)
- if ctx.Written() {
- return
- }
-
- if setting.AttachmentEnabled {
- attachments = form.Files
- }
-
- if ctx.HasError() {
- ctx.HTML(200, tplIssueNew)
- return
- }
-
- if util.IsEmptyString(form.Title) {
- ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplIssueNew, form)
- return
- }
-
- issue := &models.Issue{
- RepoID: repo.ID,
- Title: form.Title,
- PosterID: ctx.User.ID,
- Poster: ctx.User,
- MilestoneID: milestoneID,
- Content: form.Content,
- Ref: form.Ref,
- }
- if err := models.NewIssue(repo, issue, labelIDs, assigneeIDs, attachments); err != nil {
- if models.IsErrUserDoesNotHaveAccessToRepo(err) {
- ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
- return
- }
- ctx.ServerError("NewIssue", err)
- return
- }
-
- notification.NotifyNewIssue(issue)
-
- log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
- ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
- }
-
- // commentTag returns the CommentTag for a comment in/with the given repo, poster and issue
- func commentTag(repo *models.Repository, poster *models.User, issue *models.Issue) (models.CommentTag, error) {
- perm, err := models.GetUserRepoPermission(repo, poster)
- if err != nil {
- return models.CommentTagNone, err
- }
- if perm.IsOwner() {
- return models.CommentTagOwner, nil
- } else if poster.ID == issue.PosterID {
- return models.CommentTagPoster, nil
- } else if perm.CanWrite(models.UnitTypeCode) {
- return models.CommentTagWriter, nil
- }
-
- return models.CommentTagNone, nil
- }
-
- // ViewIssue render issue view page
- func ViewIssue(ctx *context.Context) {
- issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
- if err != nil {
- if models.IsErrIssueNotExist(err) {
- ctx.NotFound("GetIssueByIndex", err)
- } else {
- ctx.ServerError("GetIssueByIndex", err)
- }
- return
- }
-
- // Make sure type and URL matches.
- if ctx.Params(":type") == "issues" && issue.IsPull {
- ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
- return
- } else if ctx.Params(":type") == "pulls" && !issue.IsPull {
- ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
- return
- }
-
- if issue.IsPull {
- MustAllowPulls(ctx)
- if ctx.Written() {
- return
- }
- ctx.Data["PageIsPullList"] = true
- ctx.Data["PageIsPullConversation"] = true
- } else {
- MustEnableIssues(ctx)
- if ctx.Written() {
- return
- }
- ctx.Data["PageIsIssueList"] = true
- }
-
- ctx.Data["RequireHighlightJS"] = true
- ctx.Data["RequireDropzone"] = true
- ctx.Data["RequireTribute"] = true
- renderAttachmentSettings(ctx)
-
- err = issue.LoadAttributes()
- if err != nil {
- ctx.ServerError("GetIssueByIndex", err)
- return
- }
-
- ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
-
- var iw *models.IssueWatch
- var exists bool
- if ctx.User != nil {
- iw, exists, err = models.GetIssueWatch(ctx.User.ID, issue.ID)
- if err != nil {
- ctx.ServerError("GetIssueWatch", err)
- return
- }
- if !exists {
- iw = &models.IssueWatch{
- UserID: ctx.User.ID,
- IssueID: issue.ID,
- IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID),
- }
- }
- }
- ctx.Data["IssueWatch"] = iw
-
- issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink,
- ctx.Repo.Repository.ComposeMetas()))
-
- repo := ctx.Repo.Repository
-
- // Get more information if it's a pull request.
- if issue.IsPull {
- if issue.PullRequest.HasMerged {
- ctx.Data["DisableStatusChange"] = issue.PullRequest.HasMerged
- PrepareMergedViewPullInfo(ctx, issue)
- } else {
- PrepareViewPullInfo(ctx, issue)
- ctx.Data["DisableStatusChange"] = ctx.Data["IsPullRequestBroken"] == true && issue.IsClosed
- }
- if ctx.Written() {
- return
- }
- }
-
- // Metas.
- // Check labels.
- labelIDMark := make(map[int64]bool)
- for i := range issue.Labels {
- labelIDMark[issue.Labels[i].ID] = true
- }
- labels, err := models.GetLabelsByRepoID(repo.ID, "")
- if err != nil {
- ctx.ServerError("GetLabelsByRepoID", err)
- return
- }
- hasSelected := false
- for i := range labels {
- if labelIDMark[labels[i].ID] {
- labels[i].IsChecked = true
- hasSelected = true
- }
- }
- ctx.Data["HasSelectedLabel"] = hasSelected
- ctx.Data["Labels"] = labels
-
- // Check milestone and assignee.
- if ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
- RetrieveRepoMilestonesAndAssignees(ctx, repo)
- if ctx.Written() {
- return
- }
- }
-
- if ctx.IsSigned {
- // Update issue-user.
- if err = issue.ReadBy(ctx.User.ID); err != nil {
- ctx.ServerError("ReadBy", err)
- return
- }
- }
-
- var (
- tag models.CommentTag
- ok bool
- marked = make(map[int64]models.CommentTag)
- comment *models.Comment
- participants = make([]*models.User, 1, 10)
- )
- if ctx.Repo.Repository.IsTimetrackerEnabled() {
- if ctx.IsSigned {
- // Deal with the stopwatch
- ctx.Data["IsStopwatchRunning"] = models.StopwatchExists(ctx.User.ID, issue.ID)
- if !ctx.Data["IsStopwatchRunning"].(bool) {
- var exists bool
- var sw *models.Stopwatch
- if exists, sw, err = models.HasUserStopwatch(ctx.User.ID); err != nil {
- ctx.ServerError("HasUserStopwatch", err)
- return
- }
- ctx.Data["HasUserStopwatch"] = exists
- if exists {
- // Add warning if the user has already a stopwatch
- var otherIssue *models.Issue
- if otherIssue, err = models.GetIssueByID(sw.IssueID); err != nil {
- ctx.ServerError("GetIssueByID", err)
- return
- }
- if err = otherIssue.LoadRepo(); err != nil {
- ctx.ServerError("LoadRepo", err)
- return
- }
- // Add link to the issue of the already running stopwatch
- ctx.Data["OtherStopwatchURL"] = otherIssue.HTMLURL()
- }
- }
- ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(issue, ctx.User)
- } else {
- ctx.Data["CanUseTimetracker"] = false
- }
- if ctx.Data["WorkingUsers"], err = models.TotalTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
- ctx.ServerError("TotalTimes", err)
- return
- }
- }
-
- // Check if the user can use the dependencies
- ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
-
- // Render comments and and fetch participants.
- participants[0] = issue.Poster
- for _, comment = range issue.Comments {
- comment.Issue = issue
-
- if err := comment.LoadPoster(); err != nil {
- ctx.ServerError("LoadPoster", err)
- return
- }
-
- if comment.Type == models.CommentTypeComment {
- if err := comment.LoadAttachments(); err != nil {
- ctx.ServerError("LoadAttachments", err)
- return
- }
-
- comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,
- ctx.Repo.Repository.ComposeMetas()))
-
- // Check tag.
- tag, ok = marked[comment.PosterID]
- if ok {
- comment.ShowTag = tag
- continue
- }
-
- comment.ShowTag, err = commentTag(repo, comment.Poster, issue)
- if err != nil {
- ctx.ServerError("commentTag", err)
- return
- }
- marked[comment.PosterID] = comment.ShowTag
-
- isAdded := false
- for j := range participants {
- if comment.Poster == participants[j] {
- isAdded = true
- break
- }
- }
- if !isAdded && !issue.IsPoster(comment.Poster.ID) {
- participants = append(participants, comment.Poster)
- }
- } else if comment.Type == models.CommentTypeLabel {
- if err = comment.LoadLabel(); err != nil {
- ctx.ServerError("LoadLabel", err)
- return
- }
- } else if comment.Type == models.CommentTypeMilestone {
- if err = comment.LoadMilestone(); err != nil {
- ctx.ServerError("LoadMilestone", err)
- return
- }
- ghostMilestone := &models.Milestone{
- ID: -1,
- Name: ctx.Tr("repo.issues.deleted_milestone"),
- }
- if comment.OldMilestoneID > 0 && comment.OldMilestone == nil {
- comment.OldMilestone = ghostMilestone
- }
- if comment.MilestoneID > 0 && comment.Milestone == nil {
- comment.Milestone = ghostMilestone
- }
- } else if comment.Type == models.CommentTypeAssignees {
- if err = comment.LoadAssigneeUser(); err != nil {
- ctx.ServerError("LoadAssigneeUser", err)
- return
- }
- } else if comment.Type == models.CommentTypeRemoveDependency || comment.Type == models.CommentTypeAddDependency {
- if err = comment.LoadDepIssueDetails(); err != nil {
- ctx.ServerError("LoadDepIssueDetails", err)
- return
- }
- } else if comment.Type == models.CommentTypeCode || comment.Type == models.CommentTypeReview {
- if err = comment.LoadReview(); err != nil && !models.IsErrReviewNotExist(err) {
- ctx.ServerError("LoadReview", err)
- return
- }
- if comment.Review == nil {
- continue
- }
- if err = comment.Review.LoadAttributes(); err != nil {
- if !models.IsErrUserNotExist(err) {
- ctx.ServerError("Review.LoadAttributes", err)
- return
- }
- comment.Review.Reviewer = models.NewGhostUser()
- }
- if err = comment.Review.LoadCodeComments(); err != nil {
- ctx.ServerError("Review.LoadCodeComments", err)
- return
- }
- }
- }
-
- if issue.IsPull {
- pull := issue.PullRequest
- pull.Issue = issue
- canDelete := false
-
- if ctx.IsSigned {
- if err := pull.GetHeadRepo(); err != nil {
- log.Error("GetHeadRepo: %v", err)
- } else if pull.HeadRepo != nil && pull.HeadBranch != pull.HeadRepo.DefaultBranch {
- perm, err := models.GetUserRepoPermission(pull.HeadRepo, ctx.User)
- if err != nil {
- ctx.ServerError("GetUserRepoPermission", err)
- return
- }
- if perm.CanWrite(models.UnitTypeCode) {
- // Check if branch is not protected
- if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch, ctx.User); err != nil {
- log.Error("IsProtectedBranch: %v", err)
- } else if !protected {
- canDelete = true
- ctx.Data["DeleteBranchLink"] = ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index) + "/cleanup"
- }
- }
- }
- }
-
- prUnit, err := repo.GetUnit(models.UnitTypePullRequests)
- if err != nil {
- ctx.ServerError("GetUnit", err)
- return
- }
- prConfig := prUnit.PullRequestsConfig()
-
- ctx.Data["AllowMerge"] = ctx.Repo.CanWrite(models.UnitTypeCode)
- if err := pull.CheckUserAllowedToMerge(ctx.User); err != nil {
- if !models.IsErrNotAllowedToMerge(err) {
- ctx.ServerError("CheckUserAllowedToMerge", err)
- return
- }
- ctx.Data["AllowMerge"] = false
- }
-
- // Check correct values and select default
- if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok ||
- !prConfig.IsMergeStyleAllowed(ms) {
- if prConfig.AllowMerge {
- ctx.Data["MergeStyle"] = models.MergeStyleMerge
- } else if prConfig.AllowRebase {
- ctx.Data["MergeStyle"] = models.MergeStyleRebase
- } else if prConfig.AllowRebaseMerge {
- ctx.Data["MergeStyle"] = models.MergeStyleRebaseMerge
- } else if prConfig.AllowSquash {
- ctx.Data["MergeStyle"] = models.MergeStyleSquash
- } else {
- ctx.Data["MergeStyle"] = ""
- }
- }
- if err = pull.LoadProtectedBranch(); err != nil {
- ctx.ServerError("LoadProtectedBranch", err)
- return
- }
- if pull.ProtectedBranch != nil {
- cnt := pull.ProtectedBranch.GetGrantedApprovalsCount(pull)
- ctx.Data["IsBlockedByApprovals"] = pull.ProtectedBranch.RequiredApprovals > 0 && cnt < pull.ProtectedBranch.RequiredApprovals
- ctx.Data["GrantedApprovals"] = cnt
- }
- ctx.Data["IsPullBranchDeletable"] = canDelete && pull.HeadRepo != nil && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)
-
- ctx.Data["PullReviewersWithType"], err = models.GetReviewersByPullID(issue.ID)
- if err != nil {
- ctx.ServerError("GetReviewersByPullID", err)
- return
- }
- }
-
- // Get Dependencies
- ctx.Data["BlockedByDependencies"], err = issue.BlockedByDependencies()
- if err != nil {
- ctx.ServerError("BlockedByDependencies", err)
- return
- }
- ctx.Data["BlockingDependencies"], err = issue.BlockingDependencies()
- if err != nil {
- ctx.ServerError("BlockingDependencies", err)
- return
- }
-
- ctx.Data["Participants"] = participants
- ctx.Data["NumParticipants"] = len(participants)
- ctx.Data["Issue"] = issue
- ctx.Data["ReadOnly"] = true
- ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + ctx.Data["Link"].(string)
- ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.User.ID)
- ctx.Data["IsIssueWriter"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
- ctx.Data["IsRepoAdmin"] = ctx.IsSigned && (ctx.Repo.IsAdmin() || ctx.User.IsAdmin)
- ctx.Data["IsRepoIssuesWriter"] = ctx.IsSigned && (ctx.Repo.CanWrite(models.UnitTypeIssues) || ctx.User.IsAdmin)
- ctx.Data["LockReasons"] = setting.Repository.Issue.LockReasons
- ctx.HTML(200, tplIssueView)
- }
-
- // GetActionIssue will return the issue which is used in the context.
- func GetActionIssue(ctx *context.Context) *models.Issue {
- issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
- if err != nil {
- ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
- return nil
- }
- issue.Repo = ctx.Repo.Repository
- checkIssueRights(ctx, issue)
- if ctx.Written() {
- return nil
- }
- if err = issue.LoadAttributes(); err != nil {
- ctx.ServerError("LoadAttributes", nil)
- return nil
- }
- return issue
- }
-
- func checkIssueRights(ctx *context.Context, issue *models.Issue) {
- if issue.IsPull && !ctx.Repo.CanRead(models.UnitTypePullRequests) ||
- !issue.IsPull && !ctx.Repo.CanRead(models.UnitTypeIssues) {
- ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
- }
- }
-
- func getActionIssues(ctx *context.Context) []*models.Issue {
- commaSeparatedIssueIDs := ctx.Query("issue_ids")
- if len(commaSeparatedIssueIDs) == 0 {
- return nil
- }
- issueIDs := make([]int64, 0, 10)
- for _, stringIssueID := range strings.Split(commaSeparatedIssueIDs, ",") {
- issueID, err := strconv.ParseInt(stringIssueID, 10, 64)
- if err != nil {
- ctx.ServerError("ParseInt", err)
- return nil
- }
- issueIDs = append(issueIDs, issueID)
- }
- issues, err := models.GetIssuesByIDs(issueIDs)
- if err != nil {
- ctx.ServerError("GetIssuesByIDs", err)
- return nil
- }
- // Check access rights for all issues
- issueUnitEnabled := ctx.Repo.CanRead(models.UnitTypeIssues)
- prUnitEnabled := ctx.Repo.CanRead(models.UnitTypePullRequests)
- for _, issue := range issues {
- if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
- ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
- return nil
- }
- if err = issue.LoadAttributes(); err != nil {
- ctx.ServerError("LoadAttributes", err)
- return nil
- }
- }
- return issues
- }
-
- // UpdateIssueTitle change issue's title
- func UpdateIssueTitle(ctx *context.Context) {
- issue := GetActionIssue(ctx)
- if ctx.Written() {
- return
- }
-
- if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
- ctx.Error(403)
- return
- }
-
- title := ctx.QueryTrim("title")
- if len(title) == 0 {
- ctx.Error(204)
- return
- }
-
- if err := issue.ChangeTitle(ctx.User, title); err != nil {
- ctx.ServerError("ChangeTitle", err)
- return
- }
-
- ctx.JSON(200, map[string]interface{}{
- "title": issue.Title,
- })
- }
-
- // UpdateIssueContent change issue's content
- func UpdateIssueContent(ctx *context.Context) {
- issue := GetActionIssue(ctx)
- if ctx.Written() {
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
- ctx.Error(403)
- return
- }
-
- content := ctx.Query("content")
- if err := issue.ChangeContent(ctx.User, content); err != nil {
- ctx.ServerError("ChangeContent", err)
- return
- }
-
- ctx.JSON(200, map[string]interface{}{
- "content": string(markdown.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
- })
- }
-
- // UpdateIssueMilestone change issue's milestone
- func UpdateIssueMilestone(ctx *context.Context) {
- issues := getActionIssues(ctx)
- if ctx.Written() {
- return
- }
-
- milestoneID := ctx.QueryInt64("id")
- for _, issue := range issues {
- oldMilestoneID := issue.MilestoneID
- if oldMilestoneID == milestoneID {
- continue
- }
- issue.MilestoneID = milestoneID
- if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
- ctx.ServerError("ChangeMilestoneAssign", err)
- return
- }
- }
-
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- })
- }
-
- // UpdateIssueAssignee change issue's assignee
- func UpdateIssueAssignee(ctx *context.Context) {
- issues := getActionIssues(ctx)
- if ctx.Written() {
- return
- }
-
- assigneeID := ctx.QueryInt64("id")
- action := ctx.Query("action")
-
- for _, issue := range issues {
- switch action {
- case "clear":
- if err := models.DeleteNotPassedAssignee(issue, ctx.User, []*models.User{}); err != nil {
- ctx.ServerError("ClearAssignees", err)
- return
- }
- default:
- if err := issue.ChangeAssignee(ctx.User, assigneeID); err != nil {
- ctx.ServerError("ChangeAssignee", err)
- return
- }
- }
- }
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- })
- }
-
- // UpdateIssueStatus change issue's status
- func UpdateIssueStatus(ctx *context.Context) {
- issues := getActionIssues(ctx)
- if ctx.Written() {
- return
- }
-
- var isClosed bool
- switch action := ctx.Query("action"); action {
- case "open":
- isClosed = false
- case "close":
- isClosed = true
- default:
- log.Warn("Unrecognized action: %s", action)
- }
-
- if _, err := models.IssueList(issues).LoadRepositories(); err != nil {
- ctx.ServerError("LoadRepositories", err)
- return
- }
- for _, issue := range issues {
- if issue.IsClosed != isClosed {
- if err := issue.ChangeStatus(ctx.User, isClosed); err != nil {
- if models.IsErrDependenciesLeft(err) {
- ctx.JSON(http.StatusPreconditionFailed, map[string]interface{}{
- "error": "cannot close this issue because it still has open dependencies",
- })
- return
- }
- ctx.ServerError("ChangeStatus", err)
- return
- }
-
- notification.NotifyIssueChangeStatus(ctx.User, issue, isClosed)
- }
- }
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- })
- }
-
- // NewComment create a comment for issue
- func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
- issue := GetActionIssue(ctx)
- if ctx.Written() {
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
- if log.IsTrace() {
- if ctx.IsSigned {
- issueType := "issues"
- if issue.IsPull {
- issueType = "pulls"
- }
- log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
- "User in Repo has Permissions: %-+v",
- ctx.User,
- log.NewColoredIDValue(issue.PosterID),
- issueType,
- ctx.Repo.Repository,
- ctx.Repo.Permission)
- } else {
- log.Trace("Permission Denied: Not logged in")
- }
- }
-
- ctx.Error(403)
- }
-
- if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
- ctx.Flash.Error(ctx.Tr("repo.issues.comment_on_locked"))
- ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
- return
- }
-
- var attachments []string
- if setting.AttachmentEnabled {
- attachments = form.Files
- }
-
- if ctx.HasError() {
- ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
- ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
- return
- }
-
- var comment *models.Comment
- defer func() {
- // Check if issue admin/poster changes the status of issue.
- if (ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))) &&
- (form.Status == "reopen" || form.Status == "close") &&
- !(issue.IsPull && issue.PullRequest.HasMerged) {
-
- // Duplication and conflict check should apply to reopen pull request.
- var pr *models.PullRequest
-
- if form.Status == "reopen" && issue.IsPull {
- pull := issue.PullRequest
- var err error
- pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
- if err != nil {
- if !models.IsErrPullRequestNotExist(err) {
- ctx.ServerError("GetUnmergedPullRequest", err)
- return
- }
- }
-
- // Regenerate patch and test conflict.
- if pr == nil {
- if err = issue.PullRequest.UpdatePatch(); err != nil {
- ctx.ServerError("UpdatePatch", err)
- return
- }
-
- issue.PullRequest.AddToTaskQueue()
- }
- }
-
- if pr != nil {
- ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
- } else {
- isClosed := form.Status == "close"
- if err := issue.ChangeStatus(ctx.User, isClosed); err != nil {
- log.Error("ChangeStatus: %v", err)
-
- if models.IsErrDependenciesLeft(err) {
- if issue.IsPull {
- ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
- ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index), http.StatusSeeOther)
- } else {
- ctx.Flash.Error(ctx.Tr("repo.issues.dependency.issue_close_blocked"))
- ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index), http.StatusSeeOther)
- }
- return
- }
- } else {
-
- if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
- ctx.ServerError("CreateOrStopIssueStopwatch", err)
- return
- }
-
- log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
-
- notification.NotifyIssueChangeStatus(ctx.User, issue, isClosed)
- }
- }
- }
-
- // Redirect to comment hashtag if there is any actual content.
- typeName := "issues"
- if issue.IsPull {
- typeName = "pulls"
- }
- if comment != nil {
- ctx.Redirect(fmt.Sprintf("%s/%s/%d#%s", ctx.Repo.RepoLink, typeName, issue.Index, comment.HashTag()))
- } else {
- ctx.Redirect(fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, typeName, issue.Index))
- }
- }()
-
- // Fix #321: Allow empty comments, as long as we have attachments.
- if len(form.Content) == 0 && len(attachments) == 0 {
- return
- }
-
- comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
- if err != nil {
- ctx.ServerError("CreateIssueComment", err)
- return
- }
-
- notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
-
- log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
- }
-
- // UpdateCommentContent change comment of issue's content
- func UpdateCommentContent(ctx *context.Context) {
- comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
- if err != nil {
- ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
- return
- }
-
- if err := comment.LoadIssue(); err != nil {
- ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
- ctx.Error(403)
- return
- } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
- ctx.Error(204)
- return
- }
-
- oldContent := comment.Content
- comment.Content = ctx.Query("content")
- if len(comment.Content) == 0 {
- ctx.JSON(200, map[string]interface{}{
- "content": "",
- })
- return
- }
- if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
- ctx.ServerError("UpdateComment", err)
- return
- }
-
- notification.NotifyUpdateComment(ctx.User, comment, oldContent)
-
- ctx.JSON(200, map[string]interface{}{
- "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
- })
- }
-
- // DeleteComment delete comment of issue
- func DeleteComment(ctx *context.Context) {
- comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
- if err != nil {
- ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
- return
- }
-
- if err := comment.LoadIssue(); err != nil {
- ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
- ctx.Error(403)
- return
- } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
- ctx.Error(204)
- return
- }
-
- if err = models.DeleteComment(ctx.User, comment); err != nil {
- ctx.ServerError("DeleteCommentByID", err)
- return
- }
-
- notification.NotifyDeleteComment(ctx.User, comment)
-
- ctx.Status(200)
- }
-
- // ChangeIssueReaction create a reaction for issue
- func ChangeIssueReaction(ctx *context.Context, form auth.ReactionForm) {
- issue := GetActionIssue(ctx)
- if ctx.Written() {
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
- if log.IsTrace() {
- if ctx.IsSigned {
- issueType := "issues"
- if issue.IsPull {
- issueType = "pulls"
- }
- log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
- "User in Repo has Permissions: %-+v",
- ctx.User,
- log.NewColoredIDValue(issue.PosterID),
- issueType,
- ctx.Repo.Repository,
- ctx.Repo.Permission)
- } else {
- log.Trace("Permission Denied: Not logged in")
- }
- }
-
- ctx.Error(403)
- return
- }
-
- if ctx.HasError() {
- ctx.ServerError("ChangeIssueReaction", errors.New(ctx.GetErrMsg()))
- return
- }
-
- switch ctx.Params(":action") {
- case "react":
- reaction, err := models.CreateIssueReaction(ctx.User, issue, form.Content)
- if err != nil {
- log.Info("CreateIssueReaction: %s", err)
- break
- }
- // Reload new reactions
- issue.Reactions = nil
- if err = issue.LoadAttributes(); err != nil {
- log.Info("issue.LoadAttributes: %s", err)
- break
- }
-
- log.Trace("Reaction for issue created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, reaction.ID)
- case "unreact":
- if err := models.DeleteIssueReaction(ctx.User, issue, form.Content); err != nil {
- ctx.ServerError("DeleteIssueReaction", err)
- return
- }
-
- // Reload new reactions
- issue.Reactions = nil
- if err := issue.LoadAttributes(); err != nil {
- log.Info("issue.LoadAttributes: %s", err)
- break
- }
-
- log.Trace("Reaction for issue removed: %d/%d", ctx.Repo.Repository.ID, issue.ID)
- default:
- ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.Params(":action")), nil)
- return
- }
-
- if len(issue.Reactions) == 0 {
- ctx.JSON(200, map[string]interface{}{
- "empty": true,
- "html": "",
- })
- return
- }
-
- html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
- "ctx": ctx.Data,
- "ActionURL": fmt.Sprintf("%s/issues/%d/reactions", ctx.Repo.RepoLink, issue.Index),
- "Reactions": issue.Reactions.GroupByType(),
- })
- if err != nil {
- ctx.ServerError("ChangeIssueReaction.HTMLString", err)
- return
- }
- ctx.JSON(200, map[string]interface{}{
- "html": html,
- })
- }
-
- // ChangeCommentReaction create a reaction for comment
- func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
- comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
- if err != nil {
- ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
- return
- }
-
- if err := comment.LoadIssue(); err != nil {
- ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
- return
- }
-
- if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull)) {
- if log.IsTrace() {
- if ctx.IsSigned {
- issueType := "issues"
- if comment.Issue.IsPull {
- issueType = "pulls"
- }
- log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
- "User in Repo has Permissions: %-+v",
- ctx.User,
- log.NewColoredIDValue(comment.Issue.PosterID),
- issueType,
- ctx.Repo.Repository,
- ctx.Repo.Permission)
- } else {
- log.Trace("Permission Denied: Not logged in")
- }
- }
-
- ctx.Error(403)
- return
- } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
- ctx.Error(204)
- return
- }
-
- switch ctx.Params(":action") {
- case "react":
- reaction, err := models.CreateCommentReaction(ctx.User, comment.Issue, comment, form.Content)
- if err != nil {
- log.Info("CreateCommentReaction: %s", err)
- break
- }
- // Reload new reactions
- comment.Reactions = nil
- if err = comment.LoadReactions(); err != nil {
- log.Info("comment.LoadReactions: %s", err)
- break
- }
-
- log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID, reaction.ID)
- case "unreact":
- if err := models.DeleteCommentReaction(ctx.User, comment.Issue, comment, form.Content); err != nil {
- ctx.ServerError("DeleteCommentReaction", err)
- return
- }
-
- // Reload new reactions
- comment.Reactions = nil
- if err = comment.LoadReactions(); err != nil {
- log.Info("comment.LoadReactions: %s", err)
- break
- }
-
- log.Trace("Reaction for comment removed: %d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID)
- default:
- ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.Params(":action")), nil)
- return
- }
-
- if len(comment.Reactions) == 0 {
- ctx.JSON(200, map[string]interface{}{
- "empty": true,
- "html": "",
- })
- return
- }
-
- html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
- "ctx": ctx.Data,
- "ActionURL": fmt.Sprintf("%s/comments/%d/reactions", ctx.Repo.RepoLink, comment.ID),
- "Reactions": comment.Reactions.GroupByType(),
- })
- if err != nil {
- ctx.ServerError("ChangeCommentReaction.HTMLString", err)
- return
- }
- ctx.JSON(200, map[string]interface{}{
- "html": html,
- })
- }
|