forked from open-telemetry/opentelemetry-collector-contrib
/
main.go
188 lines (167 loc) · 4.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"sort"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"gopkg.in/yaml.v3"
)
const unmaintainedStatus = "unmaintained"
type generator interface {
generate(data *githubData) error
}
// Generates files specific to Github according to status metadata:
// .github/CODEOWNERS
// .github/ALLOWLIST
// .github/ISSUE_TEMPLATES/*.yaml (list of components)
// reports/distributions/*
func main() {
folder := flag.String("folder", ".", "folder investigated for codeowners")
allowlistFilePath := flag.String("allowlist", "cmd/githubgen/allowlist.txt", "path to a file containing an allowlist of members outside the OpenTelemetry organization")
flag.Parse()
var generators []generator
for _, arg := range flag.Args() {
switch arg {
case "issue-templates":
generators = append(generators, issueTemplatesGenerator{})
case "codeowners":
generators = append(generators, codeownersGenerator{})
case "distributions":
generators = append(generators, distributionsGenerator{})
default:
panic(fmt.Sprintf("Unknown generator: %s", arg))
}
}
if len(generators) == 0 {
generators = []generator{issueTemplatesGenerator{}, codeownersGenerator{}}
}
if err := run(*folder, *allowlistFilePath, generators); err != nil {
log.Fatal(err)
}
}
type codeowners struct {
// Active codeowners
Active []string `mapstructure:"active"`
// Emeritus codeowners
Emeritus []string `mapstructure:"emeritus"`
}
type Status struct {
Stability map[string][]string `mapstructure:"stability"`
Distributions []string `mapstructure:"distributions"`
Class string `mapstructure:"class"`
Warnings []string `mapstructure:"warnings"`
Codeowners *codeowners `mapstructure:"codeowners"`
}
type metadata struct {
// Type of the component.
Type string `mapstructure:"type"`
// Type of the parent component (applicable to subcomponents).
Parent string `mapstructure:"parent"`
// Status information for the component.
Status *Status `mapstructure:"status"`
}
type distributionData struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Maintainers []string `yaml:"maintainers,omitempty"`
}
type githubData struct {
folders []string
codeowners []string
allowlistFilePath string
maxLength int
components map[string]metadata
distributions []distributionData
}
func loadMetadata(filePath string) (metadata, error) {
cp, err := fileprovider.NewWithSettings(confmap.ProviderSettings{}).Retrieve(context.Background(), "file:"+filePath, nil)
if err != nil {
return metadata{}, err
}
conf, err := cp.AsConf()
if err != nil {
return metadata{}, err
}
md := metadata{}
if err := conf.Unmarshal(&md, confmap.WithIgnoreUnused()); err != nil {
return md, err
}
return md, nil
}
func run(folder string, allowlistFilePath string, generators []generator) error {
components := map[string]metadata{}
var foldersList []string
maxLength := 0
allCodeowners := map[string]struct{}{}
err := filepath.Walk(folder, func(path string, info fs.FileInfo, err error) error {
if info.Name() == "metadata.yaml" {
m, err := loadMetadata(path)
if err != nil {
return err
}
if m.Status == nil {
return nil
}
currentFolder := filepath.Dir(path)
key := currentFolder
components[key] = m
foldersList = append(foldersList, key)
for stability := range m.Status.Stability {
if stability == unmaintainedStatus {
// do not account for unmaintained status to change the max length of the component line.
return nil
}
}
if m.Status == nil || m.Status.Codeowners == nil {
return fmt.Errorf("component %q has no status or codeowners section", key)
}
for _, id := range m.Status.Codeowners.Active {
allCodeowners[id] = struct{}{}
}
if len(key) > maxLength {
maxLength = len(key)
}
}
return nil
})
if err != nil {
return err
}
sort.Strings(foldersList)
codeownersList := make([]string, 0, len(allCodeowners))
for c := range allCodeowners {
codeownersList = append(codeownersList, c)
}
sort.Strings(codeownersList)
var distributions []distributionData
dd, err := os.ReadFile(filepath.Join(folder, "distributions.yaml"))
if err != nil {
return err
}
err = yaml.Unmarshal(dd, &distributions)
if err != nil {
return err
}
data := &githubData{
folders: foldersList,
codeowners: codeownersList,
allowlistFilePath: allowlistFilePath,
maxLength: maxLength,
components: components,
distributions: distributions,
}
for _, g := range generators {
if err = g.generate(data); err != nil {
return err
}
}
return nil
}