func()

in pkg/repo/query.go [87:166]


func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
	logrus.Info("Searching for KEPs...")

	err := r.PrepareQueryOpts(opts)
	if err != nil {
		return nil, fmt.Errorf("invalid query options: %w", err)
	}

	if r.TokenPath != "" {
		logrus.Infof("Setting GitHub token: %v", r.TokenPath)
		if tokenErr := r.SetGitHubToken(r.TokenPath); tokenErr != nil {
			return nil, errors.Wrapf(tokenErr, "setting GitHub token")
		}
	}

	allKEPs := []*api.Proposal{}
	// load the KEPs for each listed SIG
	for _, sig := range opts.Groups {
		// KEPs in the local filesystem
		localKEPs, err := r.LoadLocalKEPs(sig)
		if err != nil {
			return nil, errors.Wrap(err, "loading local KEPs")
		}

		allKEPs = append(allKEPs, localKEPs...)

		// Open PRs; existing KEPs with open PRs will be shown twice
		if opts.IncludePRs {
			prKeps, err := r.LoadPullRequestKEPs(sig)
			if err != nil {
				logrus.Warnf("error searching for KEP PRs from %s: %s", sig, err)
			}
			if prKeps != nil {
				allKEPs = append(allKEPs, prKeps...)
			}
		}
	}

	logrus.Debugf("all KEPs collected: %+v", allKEPs)

	// filter the KEPs by criteria
	allowedStatus := sliceToMap(opts.Status)
	allowedStage := sliceToMap(opts.Stage)
	allowedPRR := sliceToMap(opts.PRRApprover)
	allowedAuthor := sliceToMap(opts.Author)
	allowedApprover := sliceToMap(opts.Approver)
	allowedParticipant := sliceToMap(opts.Participant)

	results := []*api.Proposal{}
	for _, k := range allKEPs {
		if k == nil {
			return nil, errors.New("one of the KEPs in query was nil")
		}

		logrus.Debugf("current KEP: %v", k)

		if len(opts.Status) > 0 && !allowedStatus[string(k.Status)] {
			continue
		}
		if len(opts.Stage) > 0 && !allowedStage[string(k.Stage)] {
			continue
		}
		if len(opts.PRRApprover) > 0 && !atLeastOne(k.PRRApprovers, allowedPRR) {
			continue
		}
		if len(opts.Author) > 0 && !atLeastOne(k.Authors, allowedAuthor) {
			continue
		}
		if len(opts.Approver) > 0 && !atLeastOne(k.Approvers, allowedApprover) {
			continue
		}
		if len(opts.Participant) > 0 && !atLeastOne(k.ParticipatingSIGs, allowedParticipant) {
			continue
		}

		results = append(results, k)
	}

	return results, nil
}