Skip to content

Commit

Permalink
codeowners mapping as top level config
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Celentano <[email protected]>
  • Loading branch information
Stefano Celentano committed Aug 19, 2024
1 parent 2f727d4 commit 766e746
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func serverCmd(configPath, logLevel, logFormat *string) *cobra.Command {
}
daemon, err := daemon.New(checker, providers, projectMetrics, logger, (time.Duration(config.Interval) * time.Second), git, registry)
exit(err)
daemon.Start()
daemon.Start(*configPath)

go func() {
for sig := range signals {
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
Labels []string `validate:"dive"`
Interval int
Git GitConfig
OwnerMap map[string]string `yaml:"ownerMap"`
}

type ProvidersConfig struct {
Expand Down Expand Up @@ -112,4 +113,4 @@ func New(path string) (*Config, []byte, error) {
}

return &config, content, nil
}
}
18 changes: 14 additions & 4 deletions pkg/codeowners/codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"os"

"github.com/hairyhenderson/go-codeowners"
"github.com/qonto/standards-insights/config"
)

type Codeowners struct {
codeowners *codeowners.Codeowners
configPath string
}

func NewCodeowners(path string) (*Codeowners, error) {
func NewCodeowners(path string, configPath string) (*Codeowners, error) {
// open filesystem rooted at current working directory
fsys := os.DirFS(path)

Expand All @@ -21,6 +23,7 @@ func NewCodeowners(path string) (*Codeowners, error) {

return &Codeowners{
codeowners: c,
configPath: configPath,
}, nil
}

Expand All @@ -34,13 +37,20 @@ func (c *Codeowners) GetOwners(path string) (string, bool) {
return "", false
}

// TODO: load ownermap from standard-insights local depoyment file
ownerMap := map[string]string{}
// Attempt to load the configuration
config, _, err := config.New(c.configPath)
var ownerMap map[string]string
if err != nil {
// If config cannot be found, use an empty ownerMap
ownerMap = map[string]string{}
} else {
ownerMap = config.OwnerMap
}

owner := owners[0]
if mappedOwner, found := ownerMap[owner]; found {
return mappedOwner, true
}

return owner, true
}
}
2 changes: 1 addition & 1 deletion pkg/codeowners/codeowners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestCodeowners_FromFile(t *testing.T) {
t.Fatalf("CODEOWNERS file does not exist")
}

co, err := NewCodeowners(".")
co, err := NewCodeowners(".", "")
if err != nil {
t.Fatalf("Error creating codeowners: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func New(checker Checker,
}, nil
}

func (d *Daemon) tick() {
func (d *Daemon) tick(configPath string) {
d.logger.Info("checking projects")
projects := []project.Project{}
subProjects := []project.Project{}
Expand All @@ -117,7 +117,7 @@ func (d *Daemon) tick() {
}
d.gitRequestsCounter.WithLabelValues("success").Inc()

codeowners, err := codeowners.NewCodeowners(proj.Path)
codeowners, err := codeowners.NewCodeowners(proj.Path, configPath)
if err != nil {
d.logger.Warn(fmt.Sprintf("Failed to parse CODEOWNERS for project %s: %s", proj.Name, err.Error()))
}
Expand Down Expand Up @@ -188,18 +188,18 @@ func (d *Daemon) exploreProjectFiles(projectPath string, codeowners *codeowners.
})
}

func (d *Daemon) Start() {
func (d *Daemon) Start(configPath string) {
d.logger.Info("starting daemon")
ticker := time.NewTicker(d.interval)
d.ticker = ticker
go func() {
d.tick()
d.tick(configPath)
for {
select {
case <-d.done:
return
case <-ticker.C:
d.tick()
d.tick(configPath)
}
}
}()
Expand Down

0 comments on commit 766e746

Please sign in to comment.