Skip to content

Commit

Permalink
refactor(re-run): support multi prs
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Sep 6, 2024
1 parent cef9ba9 commit f62f159
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 38 deletions.
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) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

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 @@ -158,52 +159,83 @@ func (s *Server) processPullRequestEvent(ctx context.Context, event *github.Pull
}

func (s *Server) processCheckRunRequestEvent(ctx context.Context, event *github.CheckRunEvent) error {
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 {
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

0 comments on commit f62f159

Please sign in to comment.