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

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package models
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	access_model "code.gitea.io/gitea/models/perm/access"
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  )
    14  
    15  // GetYamlFixturesAccess returns a string containing the contents
    16  // for the access table, as recalculated using repo.RecalculateAccesses()
    17  func GetYamlFixturesAccess() (string, error) {
    18  	repos := make([]*repo_model.Repository, 0, 50)
    19  	if err := db.GetEngine(db.DefaultContext).Find(&repos); err != nil {
    20  		return "", err
    21  	}
    22  
    23  	for _, repo := range repos {
    24  		repo.MustOwner(db.DefaultContext)
    25  		if err := access_model.RecalculateAccesses(db.DefaultContext, repo); err != nil {
    26  			return "", err
    27  		}
    28  	}
    29  
    30  	var b strings.Builder
    31  
    32  	accesses := make([]*access_model.Access, 0, 200)
    33  	if err := db.GetEngine(db.DefaultContext).OrderBy("user_id, repo_id").Find(&accesses); err != nil {
    34  		return "", err
    35  	}
    36  
    37  	for i, a := range accesses {
    38  		fmt.Fprintf(&b, "-\n")
    39  		fmt.Fprintf(&b, "  id: %d\n", i+1)
    40  		fmt.Fprintf(&b, "  user_id: %d\n", a.UserID)
    41  		fmt.Fprintf(&b, "  repo_id: %d\n", a.RepoID)
    42  		fmt.Fprintf(&b, "  mode: %d\n", a.Mode)
    43  		if i < len(accesses)-1 {
    44  			fmt.Fprintf(&b, "\n")
    45  		}
    46  	}
    47  
    48  	return b.String(), nil
    49  }