Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(re-run): support multi prs #339

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions internal/linters/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,37 @@ func ListPullRequestsFiles(ctx context.Context, gc *github.Client, owner string,
}

// FilterPullRequests filter full request by commit.
func FilterPullRequestsWithCommit(ctx context.Context, gc *github.Client, owner string, repo string, headSha string) ([]*github.PullRequest, error) {
plopt := github.PullRequestListOptions{
func FilterPullRequestsWithCommit(ctx context.Context, gc *github.Client, owner, repo, headSha string) ([]*github.PullRequest, error) {
var allPRs []*github.PullRequest
opt := &github.PullRequestListOptions{
State: "open",
ListOptions: github.ListOptions{
PerPage: 60,
PerPage: 100,
},
}
var repullRequests []*github.PullRequest
pullRequests, response, err := gc.PullRequests.List(ctx, owner, repo, &plopt)
defer response.Body.Close()
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("Filter pull request failed: %v", github.Stringify(response.Body))
}
for _, pullRequest := range pullRequests {
if *pullRequest.Head.SHA == headSha {
repullRequests = append(repullRequests, pullRequest)

for {
prs, resp, err := gc.PullRequests.List(ctx, owner, repo, opt)
if err != nil {
return nil, fmt.Errorf("failed to list pull requests: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to list pull requests: %v", github.Stringify(resp.Body))
}

for _, pr := range prs {
if pr.GetHead().GetSHA() == headSha {
allPRs = append(allPRs, pr)
}
}

if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return repullRequests, nil

return allPRs, nil
}

// ListPullRequestsComments lists all comments on the specified pull request.
Expand Down
76 changes: 54 additions & 22 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
}

func (s *Server) processPullRequestEvent(ctx context.Context, event *github.PullRequestEvent) error {
log := xlog.New(ctx.Value(config.EventGUIDKey).(string))
if event.GetAction() != "opened" && event.GetAction() != "reopened" && event.GetAction() != "synchronize" {
log.Debugf("skipping action %s\n", event.GetAction())
return nil
Expand All @@ -157,53 +158,84 @@
return s.handle(ctx, event)
}

func (s *Server) processCheckRunRequestEvent(ctx context.Context, event *github.CheckRunEvent) error {

Check warning on line 161 in server.go

View check run for this annotation

qiniu-x / golangci-lint

server.go#L161

(*Server).processCheckRunRequestEvent - result 0 (error) is always nil (unparam)
log := xlog.New(ctx.Value(config.EventGUIDKey).(string))
if event.GetAction() != "rerequested" {
log.Debugf("skipping action %s\n", event.GetAction())
log.Debugf("Skipping action %s for check run event", event.GetAction())
return nil
}
headSha := event.GetCheckRun().GetHeadSHA()
org := event.GetRepo().GetOwner().GetLogin()
repo := event.GetRepo().GetName()

headSHA := event.GetCheckRun().GetHeadSHA()
repo := event.GetRepo()
org := repo.GetOwner().GetLogin()
repoName := repo.GetName()
installationID := event.GetInstallation().GetID()
plist, err := linters.FilterPullRequestsWithCommit(ctx, s.GithubClient(installationID), org, repo, headSha)

client := s.GithubClient(installationID)
prs, err := linters.FilterPullRequestsWithCommit(ctx, client, org, repoName, headSHA)
if err != nil {
log.Errorf("Filter pullreqeust fail %v\n", err)
log.Errorf("failed to filter pull requests: %v", err)
return nil
}
if len(plist) == 0 {
log.Errorf("Filter pullreqeust emmpty ")

if len(prs) == 0 {
log.Errorf("No pull requests found for commit SHA: %s", headSHA)
return nil
}
pevent := github.PullRequestEvent{}
pevent.Repo = event.GetRepo()
pevent.PullRequest = plist[0]
pevent.Number = plist[0].Number
pevent.Installation = event.GetInstallation()
return s.handle(ctx, &pevent)

for _, pr := range prs {
log.Infof("try to reprocessing pull request %d, (%v/%v), installationID: %d\n", pr.GetNumber(), org, repo, installationID)
event := &github.PullRequestEvent{
Repo: repo,
PullRequest: pr,
Number: pr.Number,
Installation: event.GetInstallation(),
}

if err := s.handle(ctx, event); err != nil {
log.Errorf("failed to handle pull request event: %v", err)
// continue to handle other pull requests
}
}

return nil
}

func (s *Server) processCheckSuiteEvent(ctx context.Context, event *github.CheckSuiteEvent) error {

Check warning on line 204 in server.go

View check run for this annotation

qiniu-x / golangci-lint

server.go#L204

(*Server).processCheckSuiteEvent - result 0 (error) is always nil (unparam)
log := xlog.New(ctx.Value(config.EventGUIDKey).(string))
if event.GetAction() != "rerequested" {
log.Debugf("skipping action %s\n", event.GetAction())
return nil
}

headSha := event.GetCheckSuite().GetHeadSHA()
event.GetCheckSuite()
org := event.GetRepo().GetOwner().GetLogin()
repo := event.GetRepo().GetName()
installationID := event.GetInstallation().GetID()
plist, err := linters.FilterPullRequestsWithCommit(ctx, s.GithubClient(installationID), org, repo, headSha)
if err != nil {
log.Errorf("Filter pullreqeust fail %v\n", err)
log.Errorf("failed to filter pull requests: %v", err)
return nil
}
pevent := github.PullRequestEvent{}
pevent.Repo = event.GetRepo()
pevent.Number = plist[0].Number
pevent.PullRequest = plist[0]
pevent.Installation = event.GetInstallation()
return s.handle(ctx, &pevent)

if len(plist) == 0 {
log.Errorf("No pull requests found for commit SHA: %s", headSha)
return nil
}
for _, pr := range plist {
log.Infof("try to reprocessing pull request %d, (%v/%v), installationID: %d\n", pr.GetNumber(), org, repo, installationID)
event := github.PullRequestEvent{
Repo: event.GetRepo(),
Number: pr.Number,
PullRequest: pr,
Installation: event.GetInstallation(),
}
if err := s.handle(ctx, &event); err != nil {
log.Errorf("failed to handle pull request event: %v", err)
// continue to handle other pull requests
}
}
return nil
}

func (s *Server) handle(ctx context.Context, event *github.PullRequestEvent) error {
Expand Down
Loading