Skip to content

Commit

Permalink
cleanup,refactor: sigs-validator
Browse files Browse the repository at this point in the history
* reduce complexity by converting all groups into a single map
* replace if then with early return
* extract validateLeads, use that in two places

Signed-off-by: Daniel Hiller <[email protected]>
  • Loading branch information
dhiller committed Dec 11, 2024
1 parent fc0631f commit ab42c5f
Showing 1 changed file with 62 additions and 66 deletions.
128 changes: 62 additions & 66 deletions validators/cmd/sigs/sigs-validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,106 +100,102 @@ func main() {
}

func validateGroups(sigsYAML *sigs.Sigs, labelsYAML *labels.LabelsYAML, kubevirtOrg orgs.Org) {
for _, sig := range sigsYAML.Sigs {
validateGroup("sig", sig, labelsYAML, kubevirtOrg)
groupsToValidate := map[string][]*sigs.Group{
"sig": sigsYAML.Sigs,
"wg": sigsYAML.Workinggroups,
"ug": sigsYAML.Usergroups,
"committee": sigsYAML.Committees,
}

for _, wg := range sigsYAML.Workinggroups {
validateGroup("wg", wg, labelsYAML, kubevirtOrg)
}

for _, ug := range sigsYAML.Usergroups {
validateGroup("ug", ug, labelsYAML, kubevirtOrg)
}

for _, committee := range sigsYAML.Committees {
validateGroup("committee", committee, labelsYAML, kubevirtOrg)
for groupType, groups := range groupsToValidate {
for _, group := range groups {
validateGroup(groupType, group, labelsYAML, kubevirtOrg)
}
}
}

func validateGroup(groupType string, groupToValidate *sigs.Group, labelsYAML *labels.LabelsYAML, kubevirtOrg orgs.Org) {
groupLog := log.WithField(groupType, groupToValidate.Name)
validateDirectoryExists(groupToValidate, groupLog)
validateLabelExists(groupToValidate, labelsYAML, groupLog)
validateLeads(groupToValidate, kubevirtOrg, groupLog)
validateGroupLeads(groupToValidate, kubevirtOrg, groupLog)
validateChairs(groupToValidate, kubevirtOrg, groupLog)
validateSubprojects(groupToValidate, groupLog, kubevirtOrg)
}

func validateDirectoryExists(groupToValidate *sigs.Group, groupLog *log.Entry) {
if groupToValidate.Dir != "" {
stat, err := os.Stat(groupToValidate.Dir)
if err != nil {
groupLog.Errorf("dir %q not found: %v", groupToValidate.Dir, err)
groupToValidate.Dir = ""
} else if !stat.IsDir() {
groupLog.Errorf("dir %q is not a directory", groupToValidate.Dir)
groupToValidate.Dir = ""
}
if groupToValidate.Dir == "" {
return
}

stat, err := os.Stat(groupToValidate.Dir)
if err != nil {
groupLog.Errorf("dir %q not found: %v", groupToValidate.Dir, err)
groupToValidate.Dir = ""
} else if !stat.IsDir() {
groupLog.Errorf("dir %q is not a directory", groupToValidate.Dir)
groupToValidate.Dir = ""
}
}

func validateLabelExists(groupToValidate *sigs.Group, labelsYAML *labels.LabelsYAML, groupLog *log.Entry) {
if groupToValidate.Label != "" {
foundLabel := false
for _, label := range labelsYAML.Default.Labels {
if label.Name == groupToValidate.Label {
foundLabel = true
break
}
}
if !foundLabel {
groupLog.Errorf("label %q not found", groupToValidate.Label)
groupToValidate.Label = ""
}
if groupToValidate.Label == "" {
return
}
}

// validateLeads checks that leads github handles are part of org, removes all that don't satisfy this requirement
func validateLeads(groupToValidate *sigs.Group, kubevirtOrg orgs.Org, groupLog *log.Entry) {
var checkedMembers []*sigs.Lead
for _, orgMember := range groupToValidate.Leads {
if !kubevirtOrg.HasMember(orgMember.Github) {
groupLog.Errorf("lead %q not found", orgMember)
} else {
checkedMembers = append(checkedMembers, orgMember)
foundLabel := false
for _, label := range labelsYAML.Default.Labels {
if label.Name == groupToValidate.Label {
foundLabel = true
break
}
}
groupToValidate.Leads = checkedMembers
if !foundLabel {
groupLog.Errorf("label %q not found", groupToValidate.Label)
groupToValidate.Label = ""
}
}

// validateGroupLeads checks that leads github handles are part of org, removes all that don't satisfy this requirement
func validateGroupLeads(groupToValidate *sigs.Group, kubevirtOrg orgs.Org, groupLog *log.Entry) {
groupToValidate.Leads = validateLeads(groupToValidate.Leads, kubevirtOrg, groupLog)
}

// validateLeads checks that chairs github handles are part of org, removes all that don't satisfy this
// validateChairs checks that chairs github handles are part of org, removes all that don't satisfy this
func validateChairs(groupToValidate *sigs.Group, kubevirtOrg orgs.Org, groupLog *log.Entry) {
if groupToValidate.Leadership != nil {
var checkedLeadership []*sigs.Chair
for _, orgMember := range groupToValidate.Leadership.Chairs {
if !kubevirtOrg.HasMember(orgMember.Github) {
groupLog.Errorf("leadership chair %q not found", orgMember)
} else {
checkedLeadership = append(checkedLeadership, orgMember)
}
if groupToValidate.Leadership == nil {
return
}

var validChairs []*sigs.Chair
for _, orgMember := range groupToValidate.Leadership.Chairs {
if !kubevirtOrg.HasMember(orgMember.Github) {
groupLog.Errorf("leadership chair %q not found", orgMember)
} else {
validChairs = append(validChairs, orgMember)
}
groupToValidate.Leadership.Chairs = checkedLeadership
}
groupToValidate.Leadership.Chairs = validChairs
}

func validateSubprojects(groupToValidate *sigs.Group, groupLog *log.Entry, kubevirtOrg orgs.Org) {
for _, subProject := range groupToValidate.SubProjects {
subprojectLog := groupLog.WithField("subproject", subProject.Name)
foundOwners := validateOwnersReferences(subProject, subprojectLog)
subProject.Owners = foundOwners

// check subproject leads - github handles are part of org
var checkedSubprojectChairs []*sigs.Lead
for _, orgMember := range subProject.Leads {
if !kubevirtOrg.HasMember(orgMember.Github) {
subprojectLog.Errorf("lead %q not found", orgMember)
} else {
checkedSubprojectChairs = append(checkedSubprojectChairs, orgMember)
}
subProject.Owners = validateOwnersReferences(subProject, subprojectLog)
subProject.Leads = validateLeads(subProject.Leads, kubevirtOrg, groupLog)
}
}

func validateLeads(leadsToValidate []*sigs.Lead, kubevirtOrg orgs.Org, groupLog *log.Entry) []*sigs.Lead {
var validLeads []*sigs.Lead
for _, orgMember := range leadsToValidate {
if !kubevirtOrg.HasMember(orgMember.Github) {
groupLog.Errorf("lead %q not found", orgMember)
} else {
validLeads = append(validLeads, orgMember)
}
subProject.Leads = checkedSubprojectChairs
}
return validLeads
}

func validateOwnersReferences(subProject *sigs.SubProject, subprojectLog *log.Entry) []string {
Expand Down

0 comments on commit ab42c5f

Please sign in to comment.