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

ci: add debug on github-bot matrix subcommand + fixes #3244

Merged
merged 10 commits into from
Dec 2, 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
4 changes: 3 additions & 1 deletion .github/workflows/bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ 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
process-pr:
name: Process PR
needs: define-prs-matrix
# Just skip this job if PR numbers matrix is empty (prevent failed state)
if: ${{ needs.define-prs-matrix.outputs.pr-numbers != '[]' && needs.define-prs-matrix.outputs.pr-numbers != '' }}
runs-on: ubuntu-latest
strategy:
matrix:
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
51 changes: 0 additions & 51 deletions contribs/github-bot/comment.tmpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package check

import (
"context"
Expand All @@ -9,44 +9,30 @@
"sync/atomic"

"github.com/gnolang/gno/contribs/github-bot/internal/client"
"github.com/gnolang/gno/contribs/github-bot/internal/config"
"github.com/gnolang/gno/contribs/github-bot/internal/logger"
p "github.com/gnolang/gno/contribs/github-bot/internal/params"
"github.com/gnolang/gno/contribs/github-bot/internal/utils"
"github.com/gnolang/gno/tm2/pkg/commands"
"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 {

Check warning on line 20 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L20

Added line #L20 was not covered by tests
// Create context with timeout if specified in the parameters.
ctx := context.Background()
if params.Timeout > 0 {
if flags.Timeout > 0 {

Check warning on line 23 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L23

Added line #L23 was not covered by tests
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), params.Timeout)
ctx, cancel = context.WithTimeout(context.Background(), flags.Timeout)

Check warning on line 25 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L25

Added line #L25 was not covered by tests
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,
})

Check warning on line 35 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L30-L35

Added lines #L30 - L35 were not covered by tests
if err != nil {
return fmt.Errorf("comment update handling failed: %w", err)
}
Expand All @@ -69,19 +55,19 @@
var prs []*github.PullRequest

// If requested, retrieve all open pull requests.
if params.PRAll {
if flags.PRAll {

Check warning on line 58 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L58

Added line #L58 was not covered by tests
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 {
pr, _, err := gh.Client.PullRequests.Get(gh.Ctx, gh.Owner, gh.Repo, prNum)
prs = make([]*github.PullRequest, len(flags.PRNums))
for i, prNum := range flags.PRNums {
pr, err := gh.GetOpenedPullRequest(prNum)

Check warning on line 68 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L66-L68

Added lines #L66 - L68 were not covered by tests
if err != nil {
return fmt.Errorf("unable to retrieve specified pull request (%d): %w", prNum, err)
return fmt.Errorf("unable to process PR list: %w", err)

Check warning on line 70 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L70

Added line #L70 was not covered by tests
}
prs[i] = pr
}
Expand All @@ -101,7 +87,7 @@
}

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

Check warning on line 90 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L90

Added line #L90 was not covered by tests
var wg sync.WaitGroup

// Used in dry-run mode to log cleanly from different goroutines.
Expand All @@ -122,15 +108,15 @@
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) {

Check warning on line 111 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L111

Added line #L111 was not covered by tests
continue
}

c := AutoContent{Description: autoRule.description, Satisfied: false}
c := AutoContent{Description: autoRule.Description, Satisfied: false}

Check warning on line 115 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L115

Added line #L115 was not covered by tests
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) {

Check warning on line 119 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L119

Added line #L119 was not covered by tests
thenDetails.SetValue(fmt.Sprintf("%s Requirement satisfied", utils.Success))
c.Satisfied = true
} else {
Expand All @@ -153,24 +139,24 @@
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) {

Check warning on line 142 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L142

Added line #L142 was not covered by tests
continue
}

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

Check warning on line 148 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L148

Added line #L148 was not covered by tests
if ok {
checkedBy = check.checkedBy
}

commentContent.ManualRules = append(
commentContent.ManualRules,
ManualContent{
Description: manualRule.description,
Description: manualRule.Description,

Check warning on line 156 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L156

Added line #L156 was not covered by tests
ConditionDetails: ifDetails.String(),
CheckedBy: checkedBy,
Teams: manualRule.teams,
Teams: manualRule.Teams,

Check warning on line 159 in contribs/github-bot/internal/check/check.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/check.go#L159

Added line #L159 was not covered by tests
},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,118 +1,131 @@
package params
package check

import (
"context"
"flag"
"fmt"
"os"
"time"

"github.com/gnolang/gno/contribs/github-bot/internal/utils"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/sethvargo/go-githubactions"
)

type Params struct {
type checkFlags struct {
Owner string
Repo string
PRAll bool
PRNums PRList
Verbose bool
PRNums utils.PRList
Verbose *bool
DryRun bool
Timeout time.Duration
flagSet *flag.FlagSet
}

func (p *Params) RegisterFlags(fs *flag.FlagSet) {
func NewCheckCmd(verbose *bool) *commands.Command {
flags := &checkFlags{Verbose: verbose}

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 ./internal/config/config.go) and displays PR status checks accordingly.\nA valid GitHub Token must be provided by setting the GITHUB_TOKEN environment variable.",
},
flags,
func(_ context.Context, _ []string) error {
flags.validateFlags()
return execCheck(flags)
},

Check warning on line 40 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L26-L40

Added lines #L26 - L40 were not covered by tests
)
}

func (flags *checkFlags) RegisterFlags(fs *flag.FlagSet) {

Check warning on line 44 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L44

Added line #L44 was not covered by tests
fs.StringVar(
&p.Owner,
&flags.Owner,

Check warning on line 46 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L46

Added line #L46 was not covered by tests
"owner",
"",
"owner of the repo to process, if empty, will be retrieved from GitHub Actions context",
)

fs.StringVar(
&p.Repo,
&flags.Repo,

Check warning on line 53 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L53

Added line #L53 was not covered by tests
"repo",
"",
"repo to process, if empty, will be retrieved from GitHub Actions context",
)

fs.BoolVar(
&p.PRAll,
&flags.PRAll,

Check warning on line 60 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L60

Added line #L60 was not covered by tests
"pr-all",
false,
"process all opened pull requests",
)

fs.TextVar(
&p.PRNums,
&flags.PRNums,

Check warning on line 67 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L67

Added line #L67 was not covered by tests
"pr-numbers",
PRList(nil),
utils.PRList(nil),

Check warning on line 69 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L69

Added line #L69 was not covered by tests
"pull request(s) to process, must be a comma separated list of PR numbers, e.g '42,1337,7890'. If empty, will be retrieved from GitHub Actions context",
)

fs.BoolVar(
&p.Verbose,
"verbose",
false,
"set logging level to debug",
)

fs.BoolVar(
&p.DryRun,
&flags.DryRun,

Check warning on line 74 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L74

Added line #L74 was not covered by tests
"dry-run",
false,
"print if pull request requirements are satisfied without updating anything on GitHub",
)

fs.DurationVar(
&p.Timeout,
&flags.Timeout,

Check warning on line 81 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L81

Added line #L81 was not covered by tests
"timeout",
0,
"timeout after which the bot execution is interrupted",
)

p.flagSet = fs
flags.flagSet = fs

Check warning on line 87 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L87

Added line #L87 was not covered by tests
}

func (p *Params) ValidateFlags() {
func (flags *checkFlags) validateFlags() {

Check warning on line 90 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L90

Added line #L90 was not covered by tests
// Helper to display an error + usage message before exiting.
errorUsage := func(err string) {
fmt.Fprintf(p.flagSet.Output(), "Error: %s\n\n", err)
p.flagSet.Usage()
fmt.Fprintf(flags.flagSet.Output(), "Error: %s\n\n", err)
flags.flagSet.Usage()

Check warning on line 94 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L93-L94

Added lines #L93 - L94 were not covered by tests
os.Exit(1)
}

// Check if flags are coherent.
if p.PRAll && len(p.PRNums) != 0 {
if flags.PRAll && len(flags.PRNums) != 0 {

Check warning on line 99 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L99

Added line #L99 was not covered by tests
errorUsage("You can specify only one of the '-pr-all' and '-pr-numbers' flags.")
}

// If one of these values is empty, it must be retrieved
// from GitHub Actions context.
if p.Owner == "" || p.Repo == "" || (len(p.PRNums) == 0 && !p.PRAll) {
if flags.Owner == "" || flags.Repo == "" || (len(flags.PRNums) == 0 && !flags.PRAll) {

Check warning on line 105 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L105

Added line #L105 was not covered by tests
actionCtx, err := githubactions.Context()
if err != nil {
errorUsage(fmt.Sprintf("Unable to get GitHub Actions context: %v.", err))
}

if p.Owner == "" {
if p.Owner, _ = actionCtx.Repo(); p.Owner == "" {
if flags.Owner == "" {
if flags.Owner, _ = actionCtx.Repo(); flags.Owner == "" {

Check warning on line 112 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L111-L112

Added lines #L111 - L112 were not covered by tests
errorUsage("Unable to retrieve owner from GitHub Actions context, you may want to set it using -onwer flag.")
}
}
if p.Repo == "" {
if _, p.Repo = actionCtx.Repo(); p.Repo == "" {
if flags.Repo == "" {
if _, flags.Repo = actionCtx.Repo(); flags.Repo == "" {

Check warning on line 117 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L116-L117

Added lines #L116 - L117 were not covered by tests
errorUsage("Unable to retrieve repo from GitHub Actions context, you may want to set it using -repo flag.")
}
}

if len(p.PRNums) == 0 && !p.PRAll {
if len(flags.PRNums) == 0 && !flags.PRAll {

Check warning on line 122 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L122

Added line #L122 was not covered by tests
prNum, err := utils.GetPRNumFromActionsCtx(actionCtx)
if err != nil {
errorUsage(fmt.Sprintf("Unable to retrieve pull request number from GitHub Actions context: %s\nYou may want to set it using -pr-numbers flag.", err.Error()))
}

p.PRNums = PRList{prNum}
flags.PRNums = utils.PRList{prNum}

Check warning on line 128 in contribs/github-bot/internal/check/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/check/cmd.go#L128

Added line #L128 was not covered by tests
}
}
}
Loading
Loading