code.gitea.io/gitea@v1.21.7/services/repository/collaboration.go (about)

     1  // Copyright 2016 The Gogs Authors. All rights reserved.
     2  // Copyright 2020 The Gitea Authors.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package repository
     6  
     7  import (
     8  	"context"
     9  
    10  	"code.gitea.io/gitea/models"
    11  	"code.gitea.io/gitea/models/db"
    12  	access_model "code.gitea.io/gitea/models/perm/access"
    13  	repo_model "code.gitea.io/gitea/models/repo"
    14  )
    15  
    16  // DeleteCollaboration removes collaboration relation between the user and repository.
    17  func DeleteCollaboration(ctx context.Context, repo *repo_model.Repository, uid int64) (err error) {
    18  	collaboration := &repo_model.Collaboration{
    19  		RepoID: repo.ID,
    20  		UserID: uid,
    21  	}
    22  
    23  	ctx, committer, err := db.TxContext(ctx)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	defer committer.Close()
    28  
    29  	if has, err := db.GetEngine(ctx).Delete(collaboration); err != nil {
    30  		return err
    31  	} else if has == 0 {
    32  		return committer.Commit()
    33  	}
    34  	if err = access_model.RecalculateAccesses(ctx, repo); err != nil {
    35  		return err
    36  	}
    37  
    38  	if err = repo_model.WatchRepo(ctx, uid, repo.ID, false); err != nil {
    39  		return err
    40  	}
    41  
    42  	if err = models.ReconsiderWatches(ctx, repo, uid); err != nil {
    43  		return err
    44  	}
    45  
    46  	// Unassign a user from any issue (s)he has been assigned to in the repository
    47  	if err := models.ReconsiderRepoIssuesAssignee(ctx, repo, uid); err != nil {
    48  		return err
    49  	}
    50  
    51  	return committer.Commit()
    52  }