Skip to content

Commit

Permalink
update: function to merge approvers
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
  • Loading branch information
leodido committed Dec 11, 2020
1 parent 3518ea8 commit 8c037ed
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions approvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"reflect"

"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/repoowners"
)
Expand Down Expand Up @@ -58,19 +57,18 @@ func processApprovers(v reflect.Value, out *map[string][]string) error {
return nil
}

func getApprovers(ownersClient *repoowners.Client, org, repo string, dedupe bool) error {
func getApprovers(ownersClient *repoowners.Client, org, repo string, dedupe bool) (map[string][]string, error) {
owners, err := ownersClient.LoadRepoOwners(org, repo, "master")
if err != nil {
logrus.WithError(err).WithField("organization", org).WithField("repository", repo).Fatal("Unable to fetch OWNERS.")
}

approvers := getUnexportedValue(reflect.ValueOf(owners).Elem().FieldByName("approvers"))

spew.Dump(approvers)
result := map[string][]string{}
err = processApprovers(approvers, &result)
if err != nil {
return err
return nil, err
}

for k, values := range result {
Expand All @@ -88,7 +86,21 @@ func getApprovers(ownersClient *repoowners.Client, org, repo string, dedupe bool
result[k] = newvalues
}

spew.Dump(result)
return result, nil
}

return nil
// Merge two approvers maps.
//
// This function never replaces any key that already exists in the left map (lx).
func mergeApprovers(lx, rx map[string][]string) map[string][]string {
for key, rv := range rx {
if lv, present := lx[key]; present {
// Then we don't want to replace it, append
lx[key] = append(lv, rv...)
} else {
// Key not in the left map so we can just shove it in
lx[key] = rv
}
}
return lx
}

0 comments on commit 8c037ed

Please sign in to comment.