|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/google/go-github/v68/github" |
| 14 | +) |
| 15 | + |
| 16 | +const allowlistHeader = `# Code generated by githubgen. DO NOT EDIT. |
| 17 | +##################################################### |
| 18 | +# |
| 19 | +# List of components in OpenTelemetry Collector Contrib |
| 20 | +# waiting on owners to be assigned |
| 21 | +# |
| 22 | +##################################################### |
| 23 | +# |
| 24 | +# Learn about membership in OpenTelemetry community: |
| 25 | +# https://github.com/open-telemetry/community/blob/main/guides/contributor/membership.md |
| 26 | +# |
| 27 | +# |
| 28 | +# Learn about CODEOWNERS file format: |
| 29 | +# https://help.github.com/en/articles/about-code-owners |
| 30 | +# |
| 31 | +
|
| 32 | +## |
| 33 | +# NOTE: New components MUST have one or more codeowners. Add codeowners to the component metadata.yaml and run make gengithub |
| 34 | +## |
| 35 | +
|
| 36 | +## COMMON & SHARED components |
| 37 | +internal/common |
| 38 | +
|
| 39 | +` |
| 40 | + |
| 41 | +const unmaintainedHeader = ` |
| 42 | +
|
| 43 | +## UNMAINTAINED components |
| 44 | +
|
| 45 | +` |
| 46 | + |
| 47 | +const codeownersHeader = `# Code generated by githubgen. DO NOT EDIT. |
| 48 | +##################################################### |
| 49 | +# |
| 50 | +# List of codeowners for OpenTelemetry Collector Contrib |
| 51 | +# |
| 52 | +##################################################### |
| 53 | +# |
| 54 | +# Learn about membership in OpenTelemetry community: |
| 55 | +# https://github.com/open-telemetry/community/blob/main/guides/contributor/membership.md |
| 56 | +# |
| 57 | +# |
| 58 | +# Learn about CODEOWNERS file format: |
| 59 | +# https://help.github.com/en/articles/about-code-owners |
| 60 | +# |
| 61 | +
|
| 62 | +* @open-telemetry/collector-contrib-approvers |
| 63 | +` |
| 64 | + |
| 65 | +const distributionCodeownersHeader = ` |
| 66 | +##################################################### |
| 67 | +# |
| 68 | +# List of distribution maintainers for OpenTelemetry Collector Contrib |
| 69 | +# |
| 70 | +##################################################### |
| 71 | +` |
| 72 | + |
| 73 | +type codeownersGenerator struct { |
| 74 | + skipGithub bool |
| 75 | +} |
| 76 | + |
| 77 | +func (cg codeownersGenerator) generate(data *githubData) error { |
| 78 | + allowlistData, err := os.ReadFile(data.allowlistFilePath) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + allowlistLines := strings.Split(string(allowlistData), "\n") |
| 83 | + |
| 84 | + allowlist := make(map[string]struct{}, len(allowlistLines)) |
| 85 | + unusedAllowlist := make(map[string]struct{}, len(allowlistLines)) |
| 86 | + for _, line := range allowlistLines { |
| 87 | + if line == "" { |
| 88 | + continue |
| 89 | + } |
| 90 | + allowlist[line] = struct{}{} |
| 91 | + unusedAllowlist[line] = struct{}{} |
| 92 | + } |
| 93 | + var missingCodeowners []string |
| 94 | + var duplicateCodeowners []string |
| 95 | + members, err := cg.getGithubMembers() |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + for _, codeowner := range data.codeowners { |
| 100 | + _, present := members[codeowner] |
| 101 | + |
| 102 | + if !present { |
| 103 | + _, allowed := allowlist[codeowner] |
| 104 | + delete(unusedAllowlist, codeowner) |
| 105 | + allowed = allowed || strings.HasPrefix(codeowner, "open-telemetry/") |
| 106 | + if !allowed { |
| 107 | + missingCodeowners = append(missingCodeowners, codeowner) |
| 108 | + } |
| 109 | + } else if _, ok := allowlist[codeowner]; ok { |
| 110 | + duplicateCodeowners = append(duplicateCodeowners, codeowner) |
| 111 | + } |
| 112 | + } |
| 113 | + if len(missingCodeowners) > 0 && !cg.skipGithub { |
| 114 | + sort.Strings(missingCodeowners) |
| 115 | + return fmt.Errorf("codeowners are not members: %s", strings.Join(missingCodeowners, ", ")) |
| 116 | + } |
| 117 | + if len(duplicateCodeowners) > 0 { |
| 118 | + sort.Strings(duplicateCodeowners) |
| 119 | + return fmt.Errorf("codeowners members duplicate in allowlist: %s", strings.Join(duplicateCodeowners, ", ")) |
| 120 | + } |
| 121 | + if len(unusedAllowlist) > 0 { |
| 122 | + var unused []string |
| 123 | + for k := range unusedAllowlist { |
| 124 | + unused = append(unused, k) |
| 125 | + } |
| 126 | + sort.Strings(unused) |
| 127 | + return fmt.Errorf("unused members in allowlist: %s", strings.Join(unused, ", ")) |
| 128 | + } |
| 129 | + |
| 130 | + codeowners := codeownersHeader |
| 131 | + deprecatedList := "## DEPRECATED components\n" |
| 132 | + unmaintainedList := "\n## UNMAINTAINED components\n" |
| 133 | + |
| 134 | + unmaintainedCodeowners := unmaintainedHeader |
| 135 | + currentFirstSegment := "" |
| 136 | +LOOP: |
| 137 | + for _, key := range data.folders { |
| 138 | + m := data.components[key] |
| 139 | + for stability := range m.Status.Stability { |
| 140 | + if stability == unmaintainedStatus { |
| 141 | + unmaintainedList += key + "/\n" |
| 142 | + unmaintainedCodeowners += fmt.Sprintf("%s/%s @open-telemetry/collector-contrib-approvers\n", key, strings.Repeat(" ", data.maxLength-len(key))) |
| 143 | + continue LOOP |
| 144 | + } |
| 145 | + if stability == "deprecated" && (m.Status.Codeowners == nil || len(m.Status.Codeowners.Active) == 0) { |
| 146 | + deprecatedList += key + "/\n" |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if m.Status.Codeowners != nil { |
| 151 | + parts := strings.Split(key, string(os.PathSeparator)) |
| 152 | + firstSegment := parts[0] |
| 153 | + if firstSegment != currentFirstSegment { |
| 154 | + currentFirstSegment = firstSegment |
| 155 | + codeowners += "\n" |
| 156 | + } |
| 157 | + owners := "" |
| 158 | + for _, owner := range m.Status.Codeowners.Active { |
| 159 | + owners += " " |
| 160 | + owners += "@" + owner |
| 161 | + } |
| 162 | + codeowners += fmt.Sprintf("%s/%s @open-telemetry/collector-contrib-approvers%s\n", key, strings.Repeat(" ", data.maxLength-len(key)), owners) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + codeowners += distributionCodeownersHeader |
| 167 | + longestName := 0 |
| 168 | + for _, dist := range data.distributions { |
| 169 | + if longestName < len(dist.Name) { |
| 170 | + longestName = len(dist.Name) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + for _, dist := range data.distributions { |
| 175 | + var maintainers []string |
| 176 | + for _, m := range dist.Maintainers { |
| 177 | + maintainers = append(maintainers, fmt.Sprintf("@%s", m)) |
| 178 | + } |
| 179 | + |
| 180 | + distribution := fmt.Sprintf("\nreports/distributions/%s.yaml%s @open-telemetry/collector-contrib-approvers", dist.Name, strings.Repeat(" ", longestName-len(dist.Name))) |
| 181 | + if len(maintainers) > 0 { |
| 182 | + distribution += fmt.Sprintf(" %s", strings.Join(maintainers, " ")) |
| 183 | + } |
| 184 | + |
| 185 | + codeowners += distribution |
| 186 | + } |
| 187 | + |
| 188 | + err = os.WriteFile(filepath.Join(".github", "CODEOWNERS"), []byte(codeowners+unmaintainedCodeowners), 0o600) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + err = os.WriteFile(filepath.Join(".github", "ALLOWLIST"), []byte(allowlistHeader+deprecatedList+unmaintainedList), 0o600) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + return nil |
| 197 | +} |
| 198 | + |
| 199 | +func (cg codeownersGenerator) getGithubMembers() (map[string]struct{}, error) { |
| 200 | + if cg.skipGithub { |
| 201 | + // don't try to get organization members if no token is expected |
| 202 | + return map[string]struct{}{}, nil |
| 203 | + } |
| 204 | + githubToken := os.Getenv("GITHUB_TOKEN") |
| 205 | + if githubToken == "" { |
| 206 | + return nil, fmt.Errorf("Set the environment variable `GITHUB_TOKEN` to a PAT token to authenticate") |
| 207 | + } |
| 208 | + client := github.NewClient(nil).WithAuthToken(githubToken) |
| 209 | + var allUsers []*github.User |
| 210 | + pageIndex := 0 |
| 211 | + for { |
| 212 | + users, resp, err := client.Organizations.ListMembers(context.Background(), "open-telemetry", |
| 213 | + &github.ListMembersOptions{ |
| 214 | + PublicOnly: false, |
| 215 | + ListOptions: github.ListOptions{ |
| 216 | + PerPage: 50, |
| 217 | + Page: pageIndex, |
| 218 | + }, |
| 219 | + }, |
| 220 | + ) |
| 221 | + if err != nil { |
| 222 | + return nil, err |
| 223 | + } |
| 224 | + defer resp.Body.Close() |
| 225 | + if len(users) == 0 { |
| 226 | + break |
| 227 | + } |
| 228 | + allUsers = append(allUsers, users...) |
| 229 | + pageIndex++ |
| 230 | + } |
| 231 | + |
| 232 | + usernames := make(map[string]struct{}, len(allUsers)) |
| 233 | + for _, u := range allUsers { |
| 234 | + usernames[*u.Login] = struct{}{} |
| 235 | + } |
| 236 | + return usernames, nil |
| 237 | +} |
0 commit comments