|
| 1 | +package userlist |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/shurcooL/githubv4" |
| 8 | + "golang.org/x/oauth2" |
| 9 | + "log/slog" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func (c *UserListConfig) loadCollaborators() error { |
| 14 | + slog.Info("Loading collaborators", "enterprise", c.enterprise) |
| 15 | + c.userList = UserList{ |
| 16 | + // updated as RFC3339 string |
| 17 | + Updated: time.Now().Format(time.RFC3339), |
| 18 | + } |
| 19 | + ctx := context.Background() |
| 20 | + src := oauth2.StaticTokenSource( |
| 21 | + &oauth2.Token{AccessToken: c.githubToken}, |
| 22 | + ) |
| 23 | + httpClient := oauth2.NewClient(ctx, src) |
| 24 | + client := githubv4.NewClient(httpClient) |
| 25 | + |
| 26 | + /* |
| 27 | + { |
| 28 | + enterprise(slug: "prodyna") { |
| 29 | + slug |
| 30 | + name |
| 31 | + organizations (first:100) { |
| 32 | + nodes { |
| 33 | + login |
| 34 | + name |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + */ |
| 40 | + var organizations struct { |
| 41 | + Enterprise struct { |
| 42 | + Slug string |
| 43 | + Name string |
| 44 | + Organizations struct { |
| 45 | + Nodes []struct { |
| 46 | + Login string |
| 47 | + Name string |
| 48 | + } |
| 49 | + } `graphql:"organizations(first:100)"` |
| 50 | + } `graphql:"enterprise(slug: $slug)"` |
| 51 | + } |
| 52 | + |
| 53 | + variables := map[string]interface{}{ |
| 54 | + "slug": githubv4.String(c.enterprise), |
| 55 | + } |
| 56 | + |
| 57 | + slog.Info("Loading organizations", "enterprise", c.enterprise) |
| 58 | + err := client.Query(ctx, &organizations, variables) |
| 59 | + if err != nil { |
| 60 | + slog.ErrorContext(ctx, "Unable to query", "error", err) |
| 61 | + return err |
| 62 | + } |
| 63 | + slog.Info("Loaded organizations", "organization.count", len(organizations.Enterprise.Organizations.Nodes)) |
| 64 | + |
| 65 | + /* |
| 66 | + { |
| 67 | + organization(login:"prodyna") { |
| 68 | + repositories(first:100) { |
| 69 | + pageInfo { |
| 70 | + hasNextPage |
| 71 | + startCursor |
| 72 | + } |
| 73 | + nodes { |
| 74 | + name |
| 75 | + collaborators(first:100,affiliation:OUTSIDE) { |
| 76 | + pageInfo { |
| 77 | + hasNextPage |
| 78 | + startCursor |
| 79 | + } |
| 80 | + nodes { |
| 81 | + login |
| 82 | + name |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + */ |
| 90 | + c.userList.Enterprise.Slug = organizations.Enterprise.Slug |
| 91 | + c.userList.Enterprise.Name = organizations.Enterprise.Name |
| 92 | + |
| 93 | + userNumber := 0 |
| 94 | + slog.Info("Iterating organizatons", "organization.count", len(organizations.Enterprise.Organizations.Nodes)) |
| 95 | + for _, org := range organizations.Enterprise.Organizations.Nodes { |
| 96 | + if org.Login != "PRODYNA" { |
| 97 | + continue |
| 98 | + } |
| 99 | + slog.Info("Loading repositories and external collaborators", "organization", org.Login) |
| 100 | + var query struct { |
| 101 | + Organization struct { |
| 102 | + Repositories struct { |
| 103 | + Nodes []struct { |
| 104 | + Name string |
| 105 | + Collaborators struct { |
| 106 | + Nodes []struct { |
| 107 | + Login string |
| 108 | + Name string |
| 109 | + ContributionsCollection struct { |
| 110 | + ContributionCalendar struct { |
| 111 | + TotalContributions int |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } `graphql:"collaborators(first:100,affiliation:OUTSIDE)"` |
| 116 | + } |
| 117 | + } `graphql:"repositories(first:100)"` |
| 118 | + } `graphql:"organization(login: $organization)"` |
| 119 | + } |
| 120 | + |
| 121 | + variables := map[string]interface{}{ |
| 122 | + "organization": githubv4.String(org.Login), |
| 123 | + } |
| 124 | + |
| 125 | + err := client.Query(ctx, &query, variables) |
| 126 | + if err != nil { |
| 127 | + slog.WarnContext(ctx, "Unable to query - will skip this organization", "error", err, "organization", org.Login) |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + // count the collaborators |
| 132 | + collaboratorCount := 0 |
| 133 | + for _, repo := range query.Organization.Repositories.Nodes { |
| 134 | + collaboratorCount += len(repo.Collaborators.Nodes) |
| 135 | + } |
| 136 | + if collaboratorCount == 0 { |
| 137 | + slog.DebugContext(ctx, "No collaborators found", "organization", org.Login) |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + for _, repo := range query.Organization.Repositories.Nodes { |
| 142 | + slog.DebugContext(ctx, "Processing repository", "repository", repo.Name, "collaborator.count", len(repo.Collaborators.Nodes)) |
| 143 | + for _, collaborator := range repo.Collaborators.Nodes { |
| 144 | + slog.DebugContext(ctx, "Processing collaborator", "login", collaborator.Login, "name", collaborator.Name, "contributions", collaborator.ContributionsCollection.ContributionCalendar.TotalContributions) |
| 145 | + user := c.userList.findUser(collaborator.Login) |
| 146 | + if user == nil { |
| 147 | + user = c.userList.createUser(userNumber+1, collaborator.Login, collaborator.Name, "", collaborator.ContributionsCollection.ContributionCalendar.TotalContributions) |
| 148 | + userNumber++ |
| 149 | + } else { |
| 150 | + slog.Info("Found existing user", "login", user.Login) |
| 151 | + } |
| 152 | + organization := Organization{ |
| 153 | + Name: org.Name, |
| 154 | + Repositories: new([]Repository), |
| 155 | + } |
| 156 | + user.upsertOrganization(organization) |
| 157 | + repository := Repository{ |
| 158 | + Name: repo.Name, |
| 159 | + } |
| 160 | + organization.upsertRepository(repository) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + slog.InfoContext(ctx, "Loaded repositories", |
| 165 | + "repository.count", len(query.Organization.Repositories.Nodes), |
| 166 | + "organization", org.Login, |
| 167 | + "collaborator.count", collaboratorCount) |
| 168 | + |
| 169 | + if collaboratorCount == 0 { |
| 170 | + continue |
| 171 | + } |
| 172 | + |
| 173 | + output, err := json.MarshalIndent(c.userList, "", " ") |
| 174 | + if err != nil { |
| 175 | + slog.ErrorContext(ctx, "Unable to marshal json", "error", err) |
| 176 | + return err |
| 177 | + } |
| 178 | + fmt.Printf("%s\n", output) |
| 179 | + |
| 180 | + slog.InfoContext(ctx, "Adding collaborators", "organization", org.Login) |
| 181 | + } |
| 182 | + |
| 183 | + c.loaded = true |
| 184 | + return nil |
| 185 | +} |
0 commit comments