From 77bdf7649a0a961fd2e27b3c9c29c0d789e061c4bba08285256e4cea6b2ce44c Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Tue, 1 Oct 2024 12:03:50 +0200 Subject: [PATCH] common: parse all PR for associated PrjGit PR we may have more than 1 page of PR to parse --- bots-common/gitea_utils.go | 54 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 0005c69..f61c74f 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -401,37 +401,41 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode } func (gitea *GiteaTransport) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) { - var page, maxSize int64 - page = 1 - maxSize = 10000 + var page int64 state := "open" - prs, err := gitea.client.Repository.RepoListPullRequests( - repository. - NewRepoListPullRequestsParams(). - WithDefaults(). - WithOwner(pr.Repository.Owner.Username). - WithRepo(DefaultGitPrj). - WithState(&state). - WithLimit(&maxSize). - WithPage(&page), - gitea.transport.DefaultAuthentication) + for { + page++ + prs, err := gitea.client.Repository.RepoListPullRequests( + repository. + NewRepoListPullRequestsParams(). + WithDefaults(). + WithOwner(pr.Repository.Owner.Username). + WithRepo(DefaultGitPrj). + WithState(&state). + WithPage(&page), + gitea.transport.DefaultAuthentication) - if err != nil { - return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err) - } + if err != nil { + return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err) + } - prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number) - // h.StdLogger.Printf("attemping to match line: '%s'\n", prLine) + prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number) + // h.StdLogger.Printf("attemping to match line: '%s'\n", prLine) - // payload_processing: - for _, pr := range prs.Payload { - lines := strings.Split(pr.Body, "\n") + // payload_processing: + for _, pr := range prs.Payload { + lines := strings.Split(pr.Body, "\n") - for _, line := range lines { - if strings.TrimSpace(line) == prLine { - return pr, nil + for _, line := range lines { + if strings.TrimSpace(line) == prLine { + return pr, nil + } } } + + if len(prs.Payload) < 10 { + break + } } return nil, nil @@ -494,7 +498,7 @@ func (gitea *GiteaTransport) GetRecentPullRequests(org, repo string) ([]*models. } // if pr is closed for more than a week, assume that we are done too - if time.Since(time.Time(res.Payload[n-1].Updated)) > 7 * 24 * time.Hour { + if time.Since(time.Time(res.Payload[n-1].Updated)) > 7*24*time.Hour { break }