GetCollaborators returns the outside collaborators for a given org. Upon first call, it lazily updates the Organization with the user information
(ctx context.Context)
| 251 | // GetCollaborators returns the outside collaborators for a given org. Upon first call, |
| 252 | // it lazily updates the Organization with the user information |
| 253 | func (org *Organization) GetCollaborators(ctx context.Context) ( |
| 254 | map[types.UserLogin]types.User, error) { |
| 255 | |
| 256 | if len(org.Collaborators) > 0 { |
| 257 | return org.Collaborators, nil |
| 258 | } |
| 259 | |
| 260 | log.Logger.Debugf( |
| 261 | "Fetching external collaborators for %s", |
| 262 | *org.info.Login, |
| 263 | ) |
| 264 | opt := &github.ListOutsideCollaboratorsOptions{ |
| 265 | ListOptions: github.ListOptions{PerPage: org.paginationSize}, |
| 266 | } |
| 267 | users, err := utils.GetPaginatedResult( |
| 268 | ctx, |
| 269 | org.backoff, |
| 270 | &opt.ListOptions, |
| 271 | func(opts *github.ListOptions) ([]*github.User, *github.Response, error) { |
| 272 | return org.client.Organizations.ListOutsideCollaborators( |
| 273 | ctx, |
| 274 | *org.info.Login, |
| 275 | opt, |
| 276 | ) |
| 277 | }, |
| 278 | func(ghUsers []*github.User) []types.User { |
| 279 | var users []types.User |
| 280 | for _, m := range ghUsers { |
| 281 | existingUser, ok := org.Collaborators[*m.Login] |
| 282 | if ok { |
| 283 | users = append(users, existingUser) |
| 284 | continue |
| 285 | } |
| 286 | // XXX information from listing collborators is incomplete |
| 287 | // we meed tp explicitly fetch user info |
| 288 | u, _, err := org.client.Users.Get(ctx, *m.Login) |
| 289 | if err != nil { |
| 290 | log.Logger.Error(err) |
| 291 | continue |
| 292 | } |
| 293 | user := types.User{} |
| 294 | // FIXME use reflection |
| 295 | userJson, _ := json.Marshal(u) |
| 296 | _ = json.Unmarshal(userJson, &user) |
| 297 | users = append(users, user) |
| 298 | } |
| 299 | return users |
| 300 | }, |
| 301 | ) |
| 302 | if err != nil { |
| 303 | log.Logger.Error(err) |
| 304 | } |
| 305 | |
| 306 | collaborators := make(map[types.UserLogin]types.User, len(users)) |
| 307 | for _, u := range users { |
| 308 | collaborators[types.UserLogin(*u.Login)] = u |
| 309 | } |
| 310 | org.Collaborators = collaborators |
no test coverage detected