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