GetUsers returns the users for a given org. Upon first call, it lazily updates the Organization with the user information
(ctx context.Context)
| 190 | // GetUsers returns the users for a given org. Upon first call, |
| 191 | // it lazily updates the Organization with the user information |
| 192 | func (org *Organization) GetUsers(ctx context.Context) ( |
| 193 | map[types.UserLogin]types.User, error) { |
| 194 | |
| 195 | if len(org.Users) > 0 { |
| 196 | return org.Users, nil |
| 197 | } |
| 198 | |
| 199 | log.Logger.Debugf("Fetching users for %s", *org.info.Login) |
| 200 | opt := &github.ListMembersOptions{ |
| 201 | ListOptions: github.ListOptions{PerPage: org.paginationSize}, |
| 202 | } |
| 203 | users, err := utils.GetPaginatedResult( |
| 204 | ctx, |
| 205 | org.backoff, |
| 206 | &opt.ListOptions, |
| 207 | func(opts *github.ListOptions) ([]*github.User, *github.Response, error) { |
| 208 | return org.client.Organizations.ListMembers( |
| 209 | ctx, |
| 210 | *org.info.Login, |
| 211 | opt, |
| 212 | ) |
| 213 | }, |
| 214 | func(ghUsers []*github.User) []types.User { |
| 215 | var users []types.User |
| 216 | for _, m := range ghUsers { |
| 217 | existingUser, ok := org.Users[*m.Login] |
| 218 | if ok { |
| 219 | users = append(users, existingUser) |
| 220 | continue |
| 221 | } |
| 222 | |
| 223 | // XXX information from listing collborators is incomplete |
| 224 | // we need to explicitly fetch user info |
| 225 | u, _, err := org.client.Users.Get(ctx, *m.Login) |
| 226 | if err != nil { |
| 227 | log.Logger.Error(err) |
| 228 | continue |
| 229 | } |
| 230 | user := types.User{} |
| 231 | // FIXME use reflection |
| 232 | userJson, _ := json.Marshal(u) |
| 233 | _ = json.Unmarshal(userJson, &user) |
| 234 | users = append(users, user) |
| 235 | } |
| 236 | return users |
| 237 | }, |
| 238 | ) |
| 239 | if err != nil { |
| 240 | log.Logger.Error(err) |
| 241 | } |
| 242 | |
| 243 | members := make(map[types.UserLogin]types.User, len(users)) |
| 244 | for _, u := range users { |
| 245 | members[types.UserLogin(*u.Login)] = u |
| 246 | } |
| 247 | org.Users = members |
| 248 | return members, nil |
| 249 | } |
no test coverage detected