Skip to content

Commit

Permalink
feat(github-bot): add a fork condition and handle PR reviews (gnolang…
Browse files Browse the repository at this point in the history
…#3303)

This PR improves the bot on two points:
- it now handle `pull_request_review` events to address [this
concern](gnolang#3238 (comment))
gnolang@4f7b0b8
- a new condition allows to check if a PR was created from a fork to
address [this
concern](gnolang#3238 (comment))
gnolang@f491d95
  • Loading branch information
aeddi authored and r3v4s committed Dec 10, 2024
1 parent 985c262 commit a9e3e36
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ on:
- converted_to_draft
- ready_for_review

# Watch for changes on PR reviews
pull_request_review:
types: [submitted, edited, dismissed]

# Watch for changes on PR comment
issue_comment:
types: [created, edited, deleted]
Expand Down
27 changes: 27 additions & 0 deletions contribs/github-bot/internal/conditions/fork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package conditions

import (
"fmt"

"github.com/gnolang/gno/contribs/github-bot/internal/utils"

"github.com/google/go-github/v64/github"
"github.com/xlab/treeprint"
)

// CreatedFromFork Condition.
type createdFromFork struct{}

var _ Condition = &createdFromFork{}

func (b *createdFromFork) IsMet(pr *github.PullRequest, details treeprint.Tree) bool {
return utils.AddStatusNode(
pr.GetHead().GetRepo().GetFullName() != pr.GetBase().GetRepo().GetFullName(),
fmt.Sprintf("The pull request was created from a fork (head branch repo: %s)", pr.GetHead().GetRepo().GetFullName()),
details,
)
}

func CreatedFromFork() Condition {
return &createdFromFork{}
}
31 changes: 31 additions & 0 deletions contribs/github-bot/internal/conditions/fork_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package conditions

import (
"testing"

"github.com/gnolang/gno/contribs/github-bot/internal/utils"
"github.com/stretchr/testify/assert"

"github.com/google/go-github/v64/github"
"github.com/xlab/treeprint"
)

func TestCreatedFromFork(t *testing.T) {
t.Parallel()

var (
repo = &github.PullRequestBranch{Repo: &github.Repository{Owner: &github.User{Login: github.String("main")}, Name: github.String("repo"), FullName: github.String("main/repo")}}
fork = &github.PullRequestBranch{Repo: &github.Repository{Owner: &github.User{Login: github.String("fork")}, Name: github.String("repo"), FullName: github.String("fork/repo")}}
)

prFromMain := &github.PullRequest{Base: repo, Head: repo}
prFromFork := &github.PullRequest{Base: repo, Head: fork}

details := treeprint.New()
assert.False(t, CreatedFromFork().IsMet(prFromMain, details))
assert.True(t, utils.TestLastNodeStatus(t, false, details), "condition details should have a status: false")

details = treeprint.New()
assert.True(t, CreatedFromFork().IsMet(prFromFork, details))
assert.True(t, utils.TestLastNodeStatus(t, true, details), "condition details should have a status: true")
}
2 changes: 1 addition & 1 deletion contribs/github-bot/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Config(gh *client.GitHub) ([]AutomaticCheck, []ManualCheck) {
auto := []AutomaticCheck{
{
Description: "Maintainers must be able to edit this pull request ([more info](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork))",
If: c.Always(),
If: c.CreatedFromFork(),
Then: r.MaintainerCanModify(),
},
{
Expand Down
2 changes: 1 addition & 1 deletion contribs/github-bot/internal/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func getPRListFromEvent(gh *client.GitHub, actionCtx *githubactions.GitHubContex

// Event triggered by an issue / PR comment being created / edited / deleted
// or any update on a PR.
case utils.EventIssueComment, utils.EventPullRequest, utils.EventPullRequestTarget:
case utils.EventIssueComment, utils.EventPullRequest, utils.EventPullRequestReview, utils.EventPullRequestTarget:
// For these events, retrieve the number of the associated PR from the context.
prNum, err := utils.GetPRNumFromActionsCtx(actionCtx)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions contribs/github-bot/internal/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func TestProcessEvent(t *testing.T) {
prs,
utils.PRList{1},
false,
}, {
"valid pull_request_review event",
&githubactions.GitHubContext{
EventName: utils.EventPullRequestReview,
Event: map[string]any{"pull_request": map[string]any{"number": 1.}},
},
prs,
utils.PRList{1},
false,
}, {
"valid pull_request_target event",
&githubactions.GitHubContext{
Expand Down
2 changes: 1 addition & 1 deletion contribs/github-bot/internal/utils/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GetPRNumFromActionsCtx(actionCtx *githubactions.GitHubContext) (int, error)
switch actionCtx.EventName {
case EventIssueComment:
firstKey = "issue"
case EventPullRequest, EventPullRequestTarget:
case EventPullRequest, EventPullRequestReview, EventPullRequestTarget:
firstKey = "pull_request"
default:
return 0, fmt.Errorf("unsupported event: %s", actionCtx.EventName)
Expand Down
1 change: 1 addition & 0 deletions contribs/github-bot/internal/utils/github_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
// GitHub Actions Event Names.
EventIssueComment = "issue_comment"
EventPullRequest = "pull_request"
EventPullRequestReview = "pull_request_review"
EventPullRequestTarget = "pull_request_target"
EventWorkflowDispatch = "workflow_dispatch"

Expand Down

0 comments on commit a9e3e36

Please sign in to comment.