func()

in pkg/repo/repo.go [305:456]


func (r *Repo) LoadPullRequestKEPs(sig string) ([]*api.Proposal, error) {
	// Initialize github client
	logrus.Debugf("Initializing github client to load PRs for sig: %v", sig)
	var auth *http.Client
	ctx := context.Background()
	if r.Token != "" {
		ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: r.Token})
		auth = oauth2.NewClient(ctx, ts)
	}
	gh := github.NewClient(auth)

	// Fetch list of all PRs if none exists
	if r.allPRs == nil {
		logrus.Debugf("Initializing list of all PRs for %v/%v", remoteOrg, remoteRepo)
		r.allPRs = []*github.PullRequest{}

		opt := &github.PullRequestListOptions{
			ListOptions: github.ListOptions{
				PerPage: 100,
			},
		}

		for {
			pulls, resp, err := gh.PullRequests.List(
				ctx,
				remoteOrg,
				remoteRepo,
				opt,
			)
			if err != nil {
				return nil, err
			}

			r.allPRs = append(r.allPRs, pulls...)
			if resp.NextPage == 0 {
				break
			}

			opt.Page = resp.NextPage
		}
	}

	// Find KEP PRs for the given sig
	kepPRs := []*github.PullRequest{}
	sigLabel := strings.Replace(sig, "-", "/", 1)
	logrus.Debugf("Searching list of %v PRs for %v/%v with labels: [%v, %v]", len(r.allPRs), remoteOrg, remoteRepo, sigLabel, proposalLabel)
	for _, pr := range r.allPRs {
		foundKind, foundSIG := false, false

		for _, l := range pr.Labels {
			if *l.Name == proposalLabel {
				foundKind = true
			}

			if *l.Name == sigLabel {
				foundSIG = true
			}
		}

		if !foundKind || !foundSIG {
			continue
		}

		logrus.Debugf("Found #%v", pr.GetHTMLURL())

		kepPRs = append(kepPRs, pr)
	}
	logrus.Debugf("Found %v PRs for %v/%v with labels: [%v, %v]", len(kepPRs), remoteOrg, remoteRepo, sigLabel, proposalLabel)

	if len(kepPRs) == 0 {
		return nil, nil
	}

	// Pull a temporary clone of the repo if none already exists
	if r.gitRepo == nil {
		g, err := git.NewClient()
		if err != nil {
			return nil, err
		}

		g.SetCredentials("", func() []byte { return []byte{} })
		g.SetRemote(krgh.GitHubURL)

		r.gitRepo, err = g.Clone(remoteOrg, remoteRepo)
		if err != nil {
			return nil, err
		}
	}

	// read out each PR, and create a Proposal for each KEP that is
	// touched by a PR. This may result in multiple versions of the same KEP.
	var allKEPs []*api.Proposal
	for _, pr := range kepPRs {
		logrus.Debugf("Getting list of files for %v", pr.GetHTMLURL())
		files, _, err := gh.PullRequests.ListFiles(
			context.Background(),
			remoteOrg,
			remoteRepo,
			pr.GetNumber(),
			&github.ListOptions{},
		)
		if err != nil {
			return nil, err
		}

		kepNames := make(map[string]bool, 10)
		for _, file := range files {
			if !strings.HasPrefix(*file.Filename, "keps/"+sig+"/") {
				continue
			}

			kk := strings.Split(*file.Filename, "/")
			if len(kk) < 3 {
				continue
			}

			if strings.HasSuffix(kk[2], ".md") {
				kepNames[kk[2][0:len(kk[2])-3]] = true
			} else {
				kepNames[kk[2]] = true
			}
		}

		if len(kepNames) == 0 {
			continue
		}

		err = r.gitRepo.CheckoutPullRequest(pr.GetNumber())
		if err != nil {
			return nil, err
		}

		// read all these KEPs
		for k := range kepNames {
			kepPath := filepath.Join(
				ProposalPathStub,
				sig,
				k,
				ProposalMetadataFilename,
			)
			kep, err := r.loadKEPFromYaml(r.gitRepo.Directory(), kepPath)
			if err != nil {
				logrus.Warnf("error reading KEP %v: %v", k, err)
			} else {
				kep.PRNumber = strconv.Itoa(pr.GetNumber())
				allKEPs = append(allKEPs, kep)
			}
		}
	}

	return allKEPs, nil
}