Skip to content
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
12 changes: 12 additions & 0 deletions pkg/ghclient/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type GH interface {
ApprovePR(prNum int) error
CommentOnPR(prNum int, comment string, args ...interface{})
EnableAutoMerge(prNum int) error
ListFiles(prNum int) ([]*github.CommitFile, error)
ListReviews(prNum int) ([]*github.PullRequestReview, error)
UpdateDependingPRs(prNum int, baseRef string, branchesToDelete []string) error
}
Expand Down Expand Up @@ -168,6 +169,17 @@ func (gh ghClient) EnableAutoMerge(prNum int) error {
return nil
}

func (gh ghClient) ListFiles(prNum int) ([]*github.CommitFile, error) {
files, _, err := gh.client.PullRequests.ListFiles(
context.Background(),
gh.owner,
gh.repo,
prNum,
&github.ListOptions{PerPage: 100})

return files, err
}

func (gh ghClient) ListReviews(prNum int) ([]*github.PullRequestReview, error) {
reviews, _, err := gh.client.PullRequests.ListReviews(
context.Background(),
Expand Down
37 changes: 37 additions & 0 deletions pkg/handler/pullrequest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ func isBotPR(pr *github.PullRequestPayload) bool {
return isDependabotPR(pr) || isKonfluxBotPR(pr) || isSubmarinerBotPR(pr)
}

func isGitHubActionsUpdatePR(pr *github.PullRequestPayload, gh ghclient.GH) bool {
// Check if this is a PR targeting the devel branch
if pr.PullRequest.Base.Ref != "devel" {
return false
}

// Get the list of files changed in the PR
files, err := gh.ListFiles(int(pr.Number))
if err != nil {
klog.Errorf("Error listing files for PR #%d: %s", pr.Number, err)
return false
}

// Check if all changed files are GitHub Actions workflow files
if len(files) == 0 {
return false
}

for _, file := range files {
// Check if the file is in .github/workflows/ directory
if !strings.HasPrefix(file.GetFilename(), ".github/workflows/") {
return false
}
// Check if it's a YAML file
if !strings.HasSuffix(file.GetFilename(), ".yml") && !strings.HasSuffix(file.GetFilename(), ".yaml") {
return false
}
}

return true
}

func openOrSync(gitRepo *git.Git, pr *github.PullRequestPayload, gh ghclient.GH) error {
prNum := int(pr.Number)

Expand All @@ -110,6 +142,11 @@ func openOrSync(gitRepo *git.Git, pr *github.PullRequestPayload, gh ghclient.GH)
}
}

if pr.Action == "opened" && (isSubmarinerBotPR(pr) || (isDependabotPR(pr) && isGitHubActionsUpdatePR(pr, gh))) {
klog.Infof("Triggering CodeRabbit review for PR #%d from %s", prNum, pr.PullRequest.User.Login)
gh.CommentOnPR(prNum, "@coderabbitai review")
}

if pr.Action == "opened" && isBotPR(pr) {
klog.Infof("Enabling auto-merge and approving PR #%d from %s", prNum, pr.PullRequest.User.Login)
err = gh.EnableAutoMerge(prNum)
Expand Down
Loading