Skip to content

Commit 67d5281

Browse files
zmotsoSergK
authored andcommitted
feat: Enhance GithubProvider ListUserOrganizations to include current user (#35)
1 parent 3c12241 commit 67d5281

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

internal/services/github/github.go

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
gfgithub "github.com/KubeRocketCI/gitfusion/pkg/github"
1616
"github.com/KubeRocketCI/gitfusion/pkg/pointer"
1717
"github.com/google/go-github/v72/github"
18+
"golang.org/x/sync/errgroup"
1819
)
1920

2021
type GitHubProvider struct{}
@@ -182,46 +183,76 @@ func convertVisibility(isPrivate bool) *models.RepositoryVisibility {
182183
return &visibility
183184
}
184185

185-
// ListUserOrganizations returns organizations for the authenticated user
186+
// ListUserOrganizations returns organizations for the authenticated user.
187+
// Also it adds the current user as an organization.
186188
func (g *GitHubProvider) ListUserOrganizations(
187189
ctx context.Context,
188190
settings krci.GitServerSettings,
189191
) ([]models.Organization, error) {
190192
client := github.NewClient(nil).WithAuthToken(settings.Token)
193+
eg, ctx := errgroup.WithContext(ctx)
191194

192-
it := gfgithub.ScanGitHubList(
193-
func(opt github.ListOptions) ([]*github.Membership, *github.Response, error) {
194-
return client.Organizations.ListOrgMemberships(
195-
ctx,
196-
&github.ListOrgMembershipsOptions{
197-
State: "active",
198-
ListOptions: opt,
199-
},
200-
)
201-
},
202-
)
203-
204-
result := make([]models.Organization, 0)
205-
206-
for membership, err := range it {
195+
var userOrg *models.Organization
196+
// Goroutine 1: Get current user
197+
eg.Go(func() error {
198+
user, _, err := client.Users.Get(ctx, "")
207199
if err != nil {
208-
return nil, fmt.Errorf("failed to list organizations: %w", err)
200+
return fmt.Errorf("failed to get current user: %w", err)
209201
}
210202

211-
org := membership.Organization
212-
if org == nil {
213-
continue
203+
userOrg = &models.Organization{
204+
Id: strconv.FormatInt(user.GetID(), 10),
205+
Name: user.GetLogin(),
206+
AvatarUrl: user.AvatarURL,
214207
}
215208

216-
orgModel := models.Organization{
217-
Id: strconv.FormatInt(org.GetID(), 10),
218-
Name: org.GetLogin(),
219-
}
220-
if org.AvatarURL != nil {
221-
orgModel.AvatarUrl = org.AvatarURL
209+
return nil
210+
})
211+
212+
result := make([]models.Organization, 0, 10)
213+
214+
// Goroutine 2: Get organizations
215+
eg.Go(func() error {
216+
it := gfgithub.ScanGitHubList(
217+
func(opt github.ListOptions) ([]*github.Membership, *github.Response, error) {
218+
return client.Organizations.ListOrgMemberships(
219+
ctx,
220+
&github.ListOrgMembershipsOptions{
221+
State: "active",
222+
ListOptions: opt,
223+
},
224+
)
225+
},
226+
)
227+
228+
for membership, err := range it {
229+
if err != nil {
230+
return fmt.Errorf("failed to list organizations: %w", err)
231+
}
232+
233+
org := membership.Organization
234+
if org == nil {
235+
continue
236+
}
237+
238+
orgModel := models.Organization{
239+
Id: strconv.FormatInt(org.GetID(), 10),
240+
Name: org.GetLogin(),
241+
AvatarUrl: org.AvatarURL,
242+
}
243+
244+
result = append(result, orgModel)
222245
}
223246

224-
result = append(result, orgModel)
247+
return nil
248+
})
249+
250+
if err := eg.Wait(); err != nil {
251+
return nil, err
252+
}
253+
254+
if userOrg != nil {
255+
result = append(result, *userOrg)
225256
}
226257

227258
return result, nil

0 commit comments

Comments
 (0)