Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Nov 29, 2024
1 parent 0a8c66d commit 690cc02
Show file tree
Hide file tree
Showing 26 changed files with 396 additions and 286 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
working-directory: contribs/github-bot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: echo "pr-numbers=$(go run . matrix)" >> "$GITHUB_OUTPUT"
run: go run . matrix -matrix-key 'pr-numbers' -verbose

# This job processes each pull request in the matrix individually while ensuring
# that a same PR cannot be processed concurrently by mutliple runners
Expand Down
4 changes: 2 additions & 2 deletions contribs/github-bot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The bot operates by defining a set of rules that are evaluated against each pull
- **Automatic Checks**: These are rules that the bot evaluates automatically. If a pull request meets the conditions specified in the rule, then the corresponding requirements are executed. For example, ensuring that changes to specific directories are reviewed by specific team members.
- **Manual Checks**: These require human intervention. If a pull request meets the conditions specified in the rule, then a checkbox that can be checked only by specified teams is displayed on the bot comment. For example, determining if infrastructure needs to be updated based on changes to specific files.

The bot configuration is defined in Go and is located in the file [config.go](./config.go).
The bot configuration is defined in Go and is located in the file [config.go](./internal/config/config.go).

### GitHub Token

Expand All @@ -31,7 +31,7 @@ For the bot to make requests to the GitHub API, it needs a Personal Access Token
USAGE
github-bot check [flags]

This tool checks if the requirements for a pull request to be merged are satisfied (defined in config.go) and displays PR status checks accordingly.
This tool checks if the requirements for a pull request to be merged are satisfied (defined in ./internal/config/config.go) and displays PR status checks accordingly.
A valid GitHub Token must be provided by setting the GITHUB_TOKEN environment variable.

FLAGS
Expand Down
81 changes: 0 additions & 81 deletions contribs/github-bot/config.go

This file was deleted.

2 changes: 1 addition & 1 deletion contribs/github-bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/google/go-github/v64 v64.0.0
github.com/migueleliasweb/go-github-mock v1.0.1
github.com/sethvargo/go-githubactions v1.3.0
github.com/stretchr/testify v1.10.0
github.com/stretchr/testify v1.9.0
github.com/xlab/treeprint v1.2.0
)

Expand Down
4 changes: 2 additions & 2 deletions contribs/github-bot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/sethvargo/go-githubactions v1.3.0 h1:Kg633LIUV2IrJsqy2MfveiED/Ouo+H2P
github.com/sethvargo/go-githubactions v1.3.0/go.mod h1:7/4WeHgYfSz9U5vwuToCK9KPnELVHAhGtRwLREOQV80=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,38 @@
package main
package check

import (
"context"
"errors"
"fmt"
"github-bot/internal/client"
"github-bot/internal/logger"
"github-bot/internal/utils"
"strings"
"sync"
"sync/atomic"

p "github-bot/internal/params"

"github.com/gnolang/gno/tm2/pkg/commands"
"github-bot/internal/client"
"github-bot/internal/config"
"github-bot/internal/logger"
"github-bot/internal/utils"
"github.com/google/go-github/v64/github"
"github.com/sethvargo/go-githubactions"
"github.com/xlab/treeprint"
)

func newCheckCmd() *commands.Command {
params := &p.Params{}

return commands.NewCommand(
commands.Metadata{
Name: "check",
ShortUsage: "github-bot check [flags]",
ShortHelp: "checks requirements for a pull request to be merged",
LongHelp: "This tool checks if the requirements for a pull request to be merged are satisfied (defined in config.go) and displays PR status checks accordingly.\nA valid GitHub Token must be provided by setting the GITHUB_TOKEN environment variable.",
},
params,
func(_ context.Context, _ []string) error {
params.ValidateFlags()
return execCheck(params)
},
)
}

func execCheck(params *p.Params) error {
func execCheck(flags *checkFlags) error {
// Create context with timeout if specified in the parameters.
ctx := context.Background()
if params.Timeout > 0 {
if flags.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), params.Timeout)
ctx, cancel = context.WithTimeout(context.Background(), flags.Timeout)
defer cancel()
}

// Init GitHub API client.
gh, err := client.New(ctx, params)
gh, err := client.New(ctx, &client.Config{
Owner: flags.Owner,
Repo: flags.Repo,
Verbose: flags.Verbose,
DryRun: flags.DryRun,
})
if err != nil {
return fmt.Errorf("comment update handling failed: %w", err)
}
Expand All @@ -70,16 +55,16 @@ func execCheck(params *p.Params) error {
var prs []*github.PullRequest

// If requested, retrieve all open pull requests.
if params.PRAll {
if flags.PRAll {
prs, err = gh.ListPR(utils.PRStateOpen)
if err != nil {
return fmt.Errorf("unable to list all PR: %w", err)
}
} else {
// Otherwise, retrieve only specified pull request(s)
// (flag or GitHub Action context).
prs = make([]*github.PullRequest, len(params.PRNums))
for i, prNum := range params.PRNums {
prs = make([]*github.PullRequest, len(flags.PRNums))
for i, prNum := range flags.PRNums {
pr, _, err := gh.Client.PullRequests.Get(gh.Ctx, gh.Owner, gh.Repo, prNum)
if err != nil {
return fmt.Errorf("unable to retrieve specified pull request (%d): %w", prNum, err)
Expand All @@ -102,7 +87,7 @@ func processPRList(gh *client.GitHub, prs []*github.PullRequest) error {
}

// Process all pull requests in parallel.
autoRules, manualRules := config(gh)
autoRules, manualRules := config.Config(gh)
var wg sync.WaitGroup

// Used in dry-run mode to log cleanly from different goroutines.
Expand All @@ -123,15 +108,15 @@ func processPRList(gh *client.GitHub, prs []*github.PullRequest) error {
ifDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Condition met", utils.Success))

// Check if conditions of this rule are met by this PR.
if !autoRule.ifC.IsMet(pr, ifDetails) {
if !autoRule.If.IsMet(pr, ifDetails) {
continue
}

c := AutoContent{Description: autoRule.description, Satisfied: false}
c := AutoContent{Description: autoRule.Description, Satisfied: false}
thenDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Requirement not satisfied", utils.Fail))

// Check if requirements of this rule are satisfied by this PR.
if autoRule.thenR.IsSatisfied(pr, thenDetails) {
if autoRule.Then.IsSatisfied(pr, thenDetails) {
thenDetails.SetValue(fmt.Sprintf("%s Requirement satisfied", utils.Success))
c.Satisfied = true
} else {
Expand All @@ -154,24 +139,24 @@ func processPRList(gh *client.GitHub, prs []*github.PullRequest) error {
ifDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Condition met", utils.Success))

// Check if conditions of this rule are met by this PR.
if !manualRule.ifC.IsMet(pr, ifDetails) {
if !manualRule.If.IsMet(pr, ifDetails) {
continue
}

// Get check status from current comment, if any.
checkedBy := ""
check, ok := checks[manualRule.description]
check, ok := checks[manualRule.Description]
if ok {
checkedBy = check.checkedBy
}

commentContent.ManualRules = append(
commentContent.ManualRules,
ManualContent{
Description: manualRule.description,
Description: manualRule.Description,
ConditionDetails: ifDetails.String(),
CheckedBy: checkedBy,
Teams: manualRule.teams,
Teams: manualRule.Teams,
},
)

Expand Down
Loading

0 comments on commit 690cc02

Please sign in to comment.