code.gitea.io/gitea@v1.21.7/routers/web/repo/issue_pin.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http"
     8  
     9  	issues_model "code.gitea.io/gitea/models/issues"
    10  	"code.gitea.io/gitea/modules/context"
    11  	"code.gitea.io/gitea/modules/json"
    12  	"code.gitea.io/gitea/modules/log"
    13  )
    14  
    15  // IssuePinOrUnpin pin or unpin a Issue
    16  func IssuePinOrUnpin(ctx *context.Context) {
    17  	issue := GetActionIssue(ctx)
    18  	if ctx.Written() {
    19  		return
    20  	}
    21  
    22  	// If we don't do this, it will crash when trying to add the pin event to the comment history
    23  	err := issue.LoadRepo(ctx)
    24  	if err != nil {
    25  		ctx.Status(http.StatusInternalServerError)
    26  		log.Error(err.Error())
    27  		return
    28  	}
    29  
    30  	err = issue.PinOrUnpin(ctx, ctx.Doer)
    31  	if err != nil {
    32  		ctx.Status(http.StatusInternalServerError)
    33  		log.Error(err.Error())
    34  		return
    35  	}
    36  
    37  	ctx.JSONRedirect(issue.Link())
    38  }
    39  
    40  // IssueUnpin unpins a Issue
    41  func IssueUnpin(ctx *context.Context) {
    42  	issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
    43  	if err != nil {
    44  		ctx.Status(http.StatusInternalServerError)
    45  		log.Error(err.Error())
    46  		return
    47  	}
    48  
    49  	// If we don't do this, it will crash when trying to add the pin event to the comment history
    50  	err = issue.LoadRepo(ctx)
    51  	if err != nil {
    52  		ctx.Status(http.StatusInternalServerError)
    53  		log.Error(err.Error())
    54  		return
    55  	}
    56  
    57  	err = issue.Unpin(ctx, ctx.Doer)
    58  	if err != nil {
    59  		ctx.Status(http.StatusInternalServerError)
    60  		log.Error(err.Error())
    61  		return
    62  	}
    63  
    64  	ctx.Status(http.StatusNoContent)
    65  }
    66  
    67  // IssuePinMove moves a pinned Issue
    68  func IssuePinMove(ctx *context.Context) {
    69  	if ctx.Doer == nil {
    70  		ctx.JSON(http.StatusForbidden, "Only signed in users are allowed to perform this action.")
    71  		return
    72  	}
    73  
    74  	type movePinIssueForm struct {
    75  		ID       int64 `json:"id"`
    76  		Position int   `json:"position"`
    77  	}
    78  
    79  	form := &movePinIssueForm{}
    80  	if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
    81  		ctx.Status(http.StatusInternalServerError)
    82  		log.Error(err.Error())
    83  		return
    84  	}
    85  
    86  	issue, err := issues_model.GetIssueByID(ctx, form.ID)
    87  	if err != nil {
    88  		ctx.Status(http.StatusInternalServerError)
    89  		log.Error(err.Error())
    90  		return
    91  	}
    92  
    93  	if issue.RepoID != ctx.Repo.Repository.ID {
    94  		ctx.Status(http.StatusNotFound)
    95  		log.Error("Issue does not belong to this repository")
    96  		return
    97  	}
    98  
    99  	err = issue.MovePin(ctx, form.Position)
   100  	if err != nil {
   101  		ctx.Status(http.StatusInternalServerError)
   102  		log.Error(err.Error())
   103  		return
   104  	}
   105  
   106  	ctx.Status(http.StatusNoContent)
   107  }