code.gitea.io/gitea@v1.21.7/models/issues/issue_lock.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package issues 5 6 import ( 7 "code.gitea.io/gitea/models/db" 8 user_model "code.gitea.io/gitea/models/user" 9 ) 10 11 // IssueLockOptions defines options for locking and/or unlocking an issue/PR 12 type IssueLockOptions struct { 13 Doer *user_model.User 14 Issue *Issue 15 Reason string 16 } 17 18 // LockIssue locks an issue. This would limit commenting abilities to 19 // users with write access to the repo 20 func LockIssue(opts *IssueLockOptions) error { 21 return updateIssueLock(opts, true) 22 } 23 24 // UnlockIssue unlocks a previously locked issue. 25 func UnlockIssue(opts *IssueLockOptions) error { 26 return updateIssueLock(opts, false) 27 } 28 29 func updateIssueLock(opts *IssueLockOptions, lock bool) error { 30 if opts.Issue.IsLocked == lock { 31 return nil 32 } 33 34 opts.Issue.IsLocked = lock 35 var commentType CommentType 36 if opts.Issue.IsLocked { 37 commentType = CommentTypeLock 38 } else { 39 commentType = CommentTypeUnlock 40 } 41 42 ctx, committer, err := db.TxContext(db.DefaultContext) 43 if err != nil { 44 return err 45 } 46 defer committer.Close() 47 48 if err := UpdateIssueCols(ctx, opts.Issue, "is_locked"); err != nil { 49 return err 50 } 51 52 opt := &CreateCommentOptions{ 53 Doer: opts.Doer, 54 Issue: opts.Issue, 55 Repo: opts.Issue.Repo, 56 Type: commentType, 57 Content: opts.Reason, 58 } 59 if _, err := CreateComment(ctx, opt); err != nil { 60 return err 61 } 62 63 return committer.Commit() 64 }