...
1 package owners
2
3 import (
4 "context"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9
10 "github.com/peterbourgon/ff/v3"
11 "github.com/peterbourgon/ff/v3/ffcli"
12 "gopkg.in/yaml.v2"
13
14 repoowners "edge-infra.dev/pkg/f8n/devinfra/repo/owners"
15 )
16
17 type update struct {
18 *owners
19 }
20
21 func newUpdate(o *owners) *ffcli.Command {
22 u := &update{owners: o}
23
24 fs := flag.NewFlagSet("hack owners update", flag.ExitOnError)
25 u.owners.RegisterFlags(fs)
26
27 return &ffcli.Command{
28 Name: "update",
29 FlagSet: fs,
30 Exec: u.Exec,
31 Options: []ff.Option{
32 ff.WithEnvVarNoPrefix(),
33 },
34 }
35 }
36
37 func (u *update) Exec(_ context.Context, _ []string) error {
38 data, err := u.generatePBotCfg()
39 if err != nil {
40 return err
41 }
42 return u.createFile(data)
43 }
44
45 func (u *update) generatePBotCfg() ([]byte, error) {
46 keys, ownersFiles, err := u.collectOwners()
47 if err != nil {
48 return nil, fmt.Errorf("failed to collect OWNERS files: %w", err)
49 }
50
51 root, err := u.loadOwnersFile(filepath.Join(u.Paths.RepoRoot, u.rootRulesFile))
52 if err != nil {
53 return nil, err
54 }
55
56 pbotConfig, err := repoowners.GeneratePolicyBotConfig(
57 keys, ownersFiles, root.Rules,
58 )
59 if err != nil {
60 return nil, fmt.Errorf("failed to generate policy-bot config: %w", err)
61 }
62
63 return yaml.Marshal(pbotConfig)
64 }
65
66 func (u *update) createFile(d []byte) error {
67 if err := os.WriteFile(u.policyBotFile, d, 0644); err != nil {
68 return fmt.Errorf("failed to write %s: %w", u.policyBotFile, err)
69 }
70 return nil
71 }
72
View as plain text