Best Testkube code snippet using config.getRepository
config.go
Source:config.go
1package repository2import (3 "context"4 "fmt"5 "io"6 "os"7 "path/filepath"8 "gitlab.com/gitlab-org/gitaly/v14/internal/command"9 "gitlab.com/gitlab-org/gitaly/v14/internal/git"10 "gitlab.com/gitlab-org/gitaly/v14/internal/git2go"11 "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/rubyserver"12 "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/transaction"13 "gitlab.com/gitlab-org/gitaly/v14/internal/helper"14 "gitlab.com/gitlab-org/gitaly/v14/internal/metadata/featureflag"15 "gitlab.com/gitlab-org/gitaly/v14/internal/transaction/txinfo"16 "gitlab.com/gitlab-org/gitaly/v14/internal/transaction/voting"17 "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"18 "gitlab.com/gitlab-org/gitaly/v14/streamio"19 "google.golang.org/grpc/codes"20 "google.golang.org/grpc/status"21)22// GetConfig reads the repository's gitconfig file and returns its contents.23func (s *server) GetConfig(24 request *gitalypb.GetConfigRequest,25 stream gitalypb.RepositoryService_GetConfigServer,26) error {27 repoPath, err := s.locator.GetPath(request.GetRepository())28 if err != nil {29 return err30 }31 configPath := filepath.Join(repoPath, "config")32 gitconfig, err := os.Open(configPath)33 if err != nil {34 if os.IsNotExist(err) {35 return status.Errorf(codes.NotFound, "opening gitconfig: %v", err)36 }37 return helper.ErrInternalf("opening gitconfig: %v", err)38 }39 writer := streamio.NewWriter(func(p []byte) error {40 return stream.Send(&gitalypb.GetConfigResponse{41 Data: p,42 })43 })44 if _, err := io.Copy(writer, gitconfig); err != nil {45 return helper.ErrInternalf("sending config: %v", err)46 }47 return nil48}49func (s *server) DeleteConfig(ctx context.Context, req *gitalypb.DeleteConfigRequest) (*gitalypb.DeleteConfigResponse, error) {50 /*51 * We need to vote both before and after the change because we don't have proper commit52 * semantics: it's not easily feasible to lock the config manually, vote on it and only53 * commit the change if the vote was successful. Git automatically does this for us for ref54 * updates via the reference-transaction hook, but here we'll need to use an approximation.55 *56 * As an approximation, we thus vote both before and after the change. Praefect requires the57 * vote up front because if an RPC failed and no vote exists, it assumes no change was58 * performed, and that's bad for us if we fail _after_ the modification but _before_ the59 * vote on changed data. And the second vote is required such that we can assert that all60 * Gitaly nodes actually did perform the same change.61 */62 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {63 return nil, helper.ErrInternal(fmt.Errorf("preimage vote on config: %w", err))64 }65 for _, k := range req.Keys {66 // We assume k does not contain any secrets; it is leaked via 'ps'.67 cmd, err := s.gitCmdFactory.New(ctx, req.Repository, git.SubCmd{68 Name: "config",69 Flags: []git.Option{git.Flag{Name: "--unset-all"}},70 Args: []string{k},71 })72 if err != nil {73 return nil, err74 }75 if err := cmd.Wait(); err != nil {76 if code, ok := command.ExitStatus(err); ok && code == 5 {77 // Status code 5 means 'key not in config', see 'git help config'78 continue79 }80 return nil, status.Errorf(codes.Internal, "command failed: %v", err)81 }82 }83 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {84 return nil, helper.ErrInternal(fmt.Errorf("postimage vote on config: %w", err))85 }86 return &gitalypb.DeleteConfigResponse{}, nil87}88func (s *server) setConfigGit2Go(ctx context.Context, req *gitalypb.SetConfigRequest) (*gitalypb.SetConfigResponse, error) {89 reqRepo := req.GetRepository()90 if reqRepo == nil {91 return nil, status.Error(codes.InvalidArgument, "no repository")92 }93 path, err := s.locator.GetRepoPath(reqRepo)94 if err != nil {95 return nil, err96 }97 entries := make(map[string]git2go.ConfigEntry)98 for _, el := range req.Entries {99 switch el.GetValue().(type) {100 case *gitalypb.SetConfigRequest_Entry_ValueStr:101 entries[el.Key] = git2go.ConfigEntry{Value: el.GetValueStr()}102 case *gitalypb.SetConfigRequest_Entry_ValueInt32:103 entries[el.Key] = git2go.ConfigEntry{Value: el.GetValueInt32()}104 case *gitalypb.SetConfigRequest_Entry_ValueBool:105 entries[el.Key] = git2go.ConfigEntry{Value: el.GetValueBool()}106 default:107 return nil, status.Error(codes.InvalidArgument, "unknown entry type")108 }109 }110 /*111 * We're voting twice, once on the preimage and once on the postimage. Please refer to the112 * comment in DeleteConfig() for the reason.113 */114 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {115 return nil, helper.ErrInternalf("preimage vote on config: %v", err)116 }117 if err := s.git2go.SetConfig(ctx, reqRepo, git2go.SetConfigCommand{Repository: path, Entries: entries}); err != nil {118 return nil, status.Errorf(codes.Internal, "SetConfig git2go error")119 }120 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {121 return nil, helper.ErrInternalf("postimage vote on config: %v", err)122 }123 return &gitalypb.SetConfigResponse{}, nil124}125func (s *server) SetConfig(ctx context.Context, req *gitalypb.SetConfigRequest) (*gitalypb.SetConfigResponse, error) {126 // We use gitaly-ruby here because in gitaly-ruby we can use Rugged, and127 // Rugged lets us set config values without leaking secrets via 'ps'. We128 // can't use `git config foo.bar secret` because that leaks secrets.129 // Also we can use git2go implementation of SetConfig130 if featureflag.GoSetConfig.IsEnabled(ctx) {131 return s.setConfigGit2Go(ctx, req)132 }133 client, err := s.ruby.RepositoryServiceClient(ctx)134 if err != nil {135 return nil, err136 }137 clientCtx, err := rubyserver.SetHeaders(ctx, s.locator, req.GetRepository())138 if err != nil {139 return nil, err140 }141 /*142 * We're voting twice, once on the preimage and once on the postimage. Please refer to the143 * comment in DeleteConfig() for the reason.144 */145 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {146 return nil, helper.ErrInternalf("preimage vote on config: %v", err)147 }148 //nolint:staticcheck149 response, err := client.SetConfig(clientCtx, req)150 if err != nil {151 return nil, err152 }153 if err := s.voteOnConfig(ctx, req.GetRepository()); err != nil {154 return nil, helper.ErrInternalf("postimage vote on config: %v", err)155 }156 return response, nil157}158func (s *server) voteOnConfig(ctx context.Context, repo *gitalypb.Repository) error {159 return transaction.RunOnContext(ctx, func(tx txinfo.Transaction) error {160 repoPath, err := s.locator.GetPath(repo)161 if err != nil {162 return fmt.Errorf("get repo path: %w", err)163 }164 var vote voting.Vote165 config, err := os.Open(filepath.Join(repoPath, "config"))166 switch {167 case err == nil:168 hash := voting.NewVoteHash()169 if _, err := io.Copy(hash, config); err != nil {170 return fmt.Errorf("seeding vote: %w", err)171 }172 vote, err = hash.Vote()173 if err != nil {174 return fmt.Errorf("computing vote: %w", err)175 }176 case os.IsNotExist(err):177 vote = voting.VoteFromData([]byte("notfound"))178 default:179 return fmt.Errorf("open repo config: %w", err)180 }181 if err := s.txManager.Vote(ctx, tx, vote); err != nil {182 return fmt.Errorf("casting vote: %w", err)183 }184 return nil185 })186}...
repository_suite.go
Source:repository_suite.go
...8 "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"9 "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"10)11// TestRepository tests an implementation of Repository.12func TestRepository(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {13 for _, tc := range []struct {14 desc string15 test func(*testing.T, config.Cfg, func(testing.TB, *gitalypb.Repository) git.Repository)16 }{17 {18 desc: "ResolveRevision",19 test: testRepositoryResolveRevision,20 },21 {22 desc: "HasBranches",23 test: testRepositoryHasBranches,24 },25 } {26 t.Run(tc.desc, func(t *testing.T) {27 tc.test(t, cfg, getRepository)28 })29 }30}31func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {32 ctx, cancel := testhelper.Context()33 defer cancel()34 pbRepo, _ := CloneRepo(t, cfg, cfg.Storages[0])35 for _, tc := range []struct {36 desc string37 revision string38 expected git.ObjectID39 }{40 {41 desc: "unqualified master branch",42 revision: "master",43 expected: "1e292f8fedd741b75372e19097c76d327140c312",44 },45 {46 desc: "fully qualified master branch",47 revision: "refs/heads/master",48 expected: "1e292f8fedd741b75372e19097c76d327140c312",49 },50 {51 desc: "typed commit",52 revision: "refs/heads/master^{commit}",53 expected: "1e292f8fedd741b75372e19097c76d327140c312",54 },55 {56 desc: "extended SHA notation",57 revision: "refs/heads/master^2",58 expected: "c1c67abbaf91f624347bb3ae96eabe3a1b742478",59 },60 {61 desc: "nonexistent branch",62 revision: "refs/heads/foobar",63 },64 {65 desc: "SHA notation gone wrong",66 revision: "refs/heads/master^3",67 },68 } {69 t.Run(tc.desc, func(t *testing.T) {70 oid, err := getRepository(t, pbRepo).ResolveRevision(ctx, git.Revision(tc.revision))71 if tc.expected == "" {72 require.Equal(t, err, git.ErrReferenceNotFound)73 return74 }75 require.NoError(t, err)76 require.Equal(t, tc.expected, oid)77 })78 }79}80func testRepositoryHasBranches(t *testing.T, cfg config.Cfg, getRepository func(testing.TB, *gitalypb.Repository) git.Repository) {81 ctx, cancel := testhelper.Context()82 defer cancel()83 pbRepo, repoPath := InitRepo(t, cfg, cfg.Storages[0])84 repo := getRepository(t, pbRepo)85 emptyCommit := text.ChompBytes(Exec(t, cfg, "-C", repoPath, "commit-tree", git.EmptyTreeOID.String()))86 Exec(t, cfg, "-C", repoPath, "update-ref", "refs/headsbranch", emptyCommit)87 hasBranches, err := repo.HasBranches(ctx)88 require.NoError(t, err)89 require.False(t, hasBranches)90 Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/branch", emptyCommit)91 hasBranches, err = repo.HasBranches(ctx)92 require.NoError(t, err)93 require.True(t, hasBranches)94}...
context.go
Source:context.go
1package container2import (3 "github.com/ybkuroki/go-webapp-sample/config"4 "github.com/ybkuroki/go-webapp-sample/logger"5 "github.com/ybkuroki/go-webapp-sample/repository"6)7// Container represents a interface for accessing the data which sharing in overall application.8type Container interface {9 GetRepository() repository.Repository10 GetConfig() *config.Config11 GetLogger() *logger.Logger12 GetEnv() string13}14// container struct is for sharing data which such as database setting, the setting of application and logger in overall this application.15type container struct {16 rep repository.Repository17 config *config.Config18 logger *logger.Logger19 env string20}21// NewContainer is constructor.22func NewContainer(rep repository.Repository, config *config.Config, logger *logger.Logger, env string) Container {23 return &container{rep: rep, config: config, logger: logger, env: env}24}25// GetRepository returns the object of repository.26func (c *container) GetRepository() repository.Repository {27 return c.rep28}29// GetConfig returns the object of configuration.30func (c *container) GetConfig() *config.Config {31 return c.config32}33// GetLogger returns the object of logger.34func (c *container) GetLogger() *logger.Logger {35 return c.logger36}37// GetEnv returns the running environment.38func (c *container) GetEnv() string {39 return c.env40}...
getRepository
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(config.GetRepository())5}6func GetRepository() string {
getRepository
Using AI Code Generation
1import "github.com/CloudyKit/jet/v6"2import "github.com/CloudyKit/jet/v6/jettest"3config := jettest.NewConfig()4repo := config.GetRepository()5import "github.com/CloudyKit/jet/v6"6import "github.com/CloudyKit/jet/v6/jettest"7config := jettest.NewConfig()8repo := config.GetRepository()9import "github.com/CloudyKit/jet/v6"10import "github.com/CloudyKit/jet/v6/jettest"11config := jettest.NewConfig()12repo := config.GetRepository()13import "github.com/CloudyKit/jet/v6"14import "github.com/CloudyKit/jet/v6/jettest"15config := jettest.NewConfig()16repo := config.GetRepository()17import "github.com/CloudyKit/jet/v6"18import "github.com/CloudyKit/jet/v6/jettest"19config := jettest.NewConfig()20repo := config.GetRepository()21import "github.com/CloudyKit/jet/v6"22import "github.com/CloudyKit/jet/v6/jettest"23config := jettest.NewConfig()24repo := config.GetRepository()25import "github.com/CloudyKit/jet/v6"26import "github.com/CloudyKit/jet/v6/jettest"
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!