github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/git/teamer.go (about) 1 // Copyright 2017 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package git 5 6 import ( 7 "errors" 8 "fmt" 9 10 "golang.org/x/net/context" 11 12 "github.com/keybase/client/go/libkb" 13 "github.com/keybase/client/go/protocol/keybase1" 14 "github.com/keybase/client/go/teams" 15 ) 16 17 type TeamerImpl struct { 18 libkb.Contextified 19 } 20 21 func NewTeamer(g *libkb.GlobalContext) Teamer { 22 return &TeamerImpl{ 23 Contextified: libkb.NewContextified(g), 24 } 25 } 26 27 func (t *TeamerImpl) LookupOrCreate(ctx context.Context, folder keybase1.FolderHandle) (res keybase1.TeamIDWithVisibility, err error) { 28 defer t.G().CTrace(ctx, fmt.Sprintf("git.Teamer#LookupOrCreate(%s, ftyp:%v)", folder.Name, folder.FolderType), &err)() 29 30 switch folder.FolderType { 31 case keybase1.FolderType_PRIVATE: 32 return t.lookupOrCreateImplicitTeam(ctx, folder) 33 case keybase1.FolderType_PUBLIC: 34 return t.lookupOrCreateImplicitTeam(ctx, folder) 35 case keybase1.FolderType_TEAM: 36 return t.lookupTeam(ctx, folder) 37 default: 38 return res, fmt.Errorf("unrecognized folder type: %v", folder.FolderType) 39 } 40 } 41 42 func (t *TeamerImpl) lookupTeam(ctx context.Context, folder keybase1.FolderHandle) (res keybase1.TeamIDWithVisibility, err error) { 43 if folder.FolderType == keybase1.FolderType_PUBLIC { 44 return res, fmt.Errorf("public team git repos not supported") 45 } 46 team, err := teams.Load(ctx, t.G(), keybase1.LoadTeamArg{ 47 Name: folder.Name, 48 Public: folder.FolderType == keybase1.FolderType_PUBLIC, 49 ForceRepoll: false, // if subteams get renamed in a racy way, just let this fail 50 }) 51 if err != nil { 52 return res, err 53 } 54 if (folder.FolderType == keybase1.FolderType_PUBLIC) != team.IsPublic() { 55 return res, fmt.Errorf("team publicity mismatch folder:%v != team:%v", folder.FolderType, team.IsPublic()) 56 } 57 visibility := keybase1.TLFVisibility_PRIVATE 58 if folder.FolderType == keybase1.FolderType_PUBLIC { 59 visibility = keybase1.TLFVisibility_PUBLIC 60 } 61 return keybase1.TeamIDWithVisibility{ 62 TeamID: team.ID, 63 Visibility: visibility, 64 }, nil 65 } 66 67 func (t *TeamerImpl) lookupOrCreateImplicitTeam(ctx context.Context, folder keybase1.FolderHandle) (res keybase1.TeamIDWithVisibility, err error) { 68 visibility := keybase1.TLFVisibility_PRIVATE 69 if folder.FolderType == keybase1.FolderType_PUBLIC { 70 visibility = keybase1.TLFVisibility_PUBLIC 71 } else if folder.FolderType != keybase1.FolderType_PRIVATE { 72 return res, errors.New("bad folder") 73 } 74 75 impName, err := libkb.ParseImplicitTeamTLFName(t.G().MakeAssertionContext(t.MetaContext(ctx)), "/keybase/"+folder.ToString()) 76 if err != nil { 77 return res, err 78 } 79 lookupName, err := teams.FormatImplicitTeamDisplayName(ctx, t.G(), impName) 80 if err != nil { 81 return res, err 82 } 83 84 isPublic := folder.FolderType == keybase1.FolderType_PUBLIC 85 86 team, _, _, err := teams.LookupOrCreateImplicitTeam(ctx, t.G(), lookupName, isPublic) 87 if err != nil { 88 return res, err 89 } 90 return keybase1.TeamIDWithVisibility{ 91 TeamID: team.ID, 92 Visibility: visibility, 93 }, nil 94 }