code.gitea.io/gitea@v1.21.7/models/organization/team_repo.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package organization
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	"code.gitea.io/gitea/models/perm"
    11  	repo_model "code.gitea.io/gitea/models/repo"
    12  
    13  	"xorm.io/builder"
    14  )
    15  
    16  // TeamRepo represents an team-repository relation.
    17  type TeamRepo struct {
    18  	ID     int64 `xorm:"pk autoincr"`
    19  	OrgID  int64 `xorm:"INDEX"`
    20  	TeamID int64 `xorm:"UNIQUE(s)"`
    21  	RepoID int64 `xorm:"UNIQUE(s)"`
    22  }
    23  
    24  // HasTeamRepo returns true if given repository belongs to team.
    25  func HasTeamRepo(ctx context.Context, orgID, teamID, repoID int64) bool {
    26  	has, _ := db.GetEngine(ctx).
    27  		Where("org_id=?", orgID).
    28  		And("team_id=?", teamID).
    29  		And("repo_id=?", repoID).
    30  		Get(new(TeamRepo))
    31  	return has
    32  }
    33  
    34  type SearchTeamRepoOptions struct {
    35  	db.ListOptions
    36  	TeamID int64
    37  }
    38  
    39  // GetRepositories returns paginated repositories in team of organization.
    40  func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) (repo_model.RepositoryList, error) {
    41  	sess := db.GetEngine(ctx)
    42  	if opts.TeamID > 0 {
    43  		sess = sess.In("id",
    44  			builder.Select("repo_id").
    45  				From("team_repo").
    46  				Where(builder.Eq{"team_id": opts.TeamID}),
    47  		)
    48  	}
    49  	if opts.PageSize > 0 {
    50  		sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
    51  	}
    52  	var repos []*repo_model.Repository
    53  	return repos, sess.OrderBy("repository.name").
    54  		Find(&repos)
    55  }
    56  
    57  // AddTeamRepo adds a repo for an organization's team
    58  func AddTeamRepo(ctx context.Context, orgID, teamID, repoID int64) error {
    59  	_, err := db.GetEngine(ctx).Insert(&TeamRepo{
    60  		OrgID:  orgID,
    61  		TeamID: teamID,
    62  		RepoID: repoID,
    63  	})
    64  	return err
    65  }
    66  
    67  // RemoveTeamRepo remove repository from team
    68  func RemoveTeamRepo(ctx context.Context, teamID, repoID int64) error {
    69  	_, err := db.DeleteByBean(ctx, &TeamRepo{
    70  		TeamID: teamID,
    71  		RepoID: repoID,
    72  	})
    73  	return err
    74  }
    75  
    76  // GetTeamsWithAccessToRepo returns all teams in an organization that have given access level to the repository.
    77  func GetTeamsWithAccessToRepo(ctx context.Context, orgID, repoID int64, mode perm.AccessMode) ([]*Team, error) {
    78  	teams := make([]*Team, 0, 5)
    79  	return teams, db.GetEngine(ctx).Where("team.authorize >= ?", mode).
    80  		Join("INNER", "team_repo", "team_repo.team_id = team.id").
    81  		And("team_repo.org_id = ?", orgID).
    82  		And("team_repo.repo_id = ?", repoID).
    83  		OrderBy("name").
    84  		Find(&teams)
    85  }